eclipse-paho / eclipse-paho/paho.mqtt.java

Supplying bind address for socket creation in multi connection environment.

Open
#780 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
2.3k
Forks
919
PR merge metrics
No merged PRs in 30d

Description

Hello ,

We are using Paho client V1.2.4 under multi connection environment where MQTT client connection shall be restricted to be used only with a specific INet adapter when multiple connections exists in the system.

With Paho Python , we have seen possibility of passing the bind address as per user choice for connection using api:
`connect(host, port=1883, keepalive=60, bind_address="")`

Are there any similar apis which can be used in java lib for both TCP / SSL connection types so that user has an option to bind the Ip address for the socket creation of their choice.

Sample TCP Network module creation in MQTT java which we thought would suit our requirements (Local address and port is supplied from connect api ) :

```java
// File - TCPNetworkModule.java

//Default constructor
public TCPNetworkModule(SocketFactory factory, String host, int port, String resourceContext) {
log.setResourceName(resourceContext);
this.factory = factory;
this.host = host;
this.port = port;

}

//Modified constructor for localAddress / port
public TCPNetworkModule(SocketFactory factory, String host, int port,
String resourceContext, InetAddress localAddress, int localPort) {
log.setResourceName(resourceContext);
this.factory = factory;
this.host = host;
this.port = port;
this.localAddress = localAddress;
this.localPort = localPort;
}
public void start() throws IOException, MqttException {
final String methodName = "start";
try {
....
if (localAddress == null) { // Default code of socket creation
socket = factory.createSocket();
socket.connect(sockaddr, conTimeout*1000);
socket.setSoTimeout(1000);
}else {
//Modified code of socket creation
....
try {
socket = factory.createSocket(host, port, localAddress,
localPort);
}catch(BindException | UnknownHostException ex) {
....
}
catch (Exception e) {
....
}
}
}
catch (ConnectException ex) {
....
}
}
```
Appreciate your Help,

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.