Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusable address #13

Closed
matheusmorett2 opened this issue Dec 13, 2019 · 4 comments
Closed

Reusable address #13

matheusmorett2 opened this issue Dec 13, 2019 · 4 comments
Labels
feature request New feature or request

Comments

@matheusmorett2
Copy link

How can i set reusable address to avoid Address already in use?

@Rapsssito
Copy link
Owner

Rapsssito commented Dec 14, 2019

Currently there is no possibility for reusable address. Sorry about that.

@Rapsssito Rapsssito added the feature request New feature or request label Dec 14, 2019
@matheusmorett2
Copy link
Author

Maybe i can help you with this issue. How do you recreate the socket connection?

@Rapsssito
Copy link
Owner

Rapsssito commented Dec 15, 2019

@matheusmorett2 feel free to open a PR with this feature! Look at the Obj-C and Java code for more information about the socket creation.

public TcpSocketClient(final TcpReceiverTask.OnDataReceivedListener receiverListener, final Integer id,
final String address, final Integer port, final String localAddress, final int localPort, final Network network)
throws IOException {
this.id = id;
// Get the addresses
InetAddress localInetAddress = InetAddress.getByName(localAddress);
InetAddress remoteInetAddress = InetAddress.getByName(address);
// Create the socket
socket = new Socket();
if (network != null)
network.bindSocket(socket);
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(localInetAddress, localPort));
socket.connect(new InetSocketAddress(remoteInetAddress, port));
receiverTask = new TcpReceiverTask();
receiverTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Pair<>(this, receiverListener));
}

- (BOOL)connect:(NSString *)host port:(int)port withOptions:(NSDictionary *)options error:(NSError **)error
{
if (_tcpSocket) {
if (error) {
*error = [self badInvocationError:@"this client's socket is already connected"];
}
return false;
}
_tcpSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:[self methodQueue]];
[_tcpSocket setUserData: _id];
BOOL result = false;
NSString *localAddress = (options?options[@"localAddress"]:nil);
NSNumber *localPort = (options?options[@"localPort"]:nil);
if (!localAddress && !localPort) {
result = [_tcpSocket connectToHost:host onPort:port error:error];
} else {
NSMutableArray *interface = [NSMutableArray arrayWithCapacity:2];
[interface addObject: localAddress?localAddress:@""];
if (localPort) {
[interface addObject:[localPort stringValue]];
}
result = [_tcpSocket connectToHost:host
onPort:port
viaInterface:[interface componentsJoinedByString:@":"]
withTimeout:-1
error:error];
}
return result;
}

@Rapsssito
Copy link
Owner

@matheusmorett2, I have created a PR (#17) for this feature. Feel free to contribute there!

Rapsssito added a commit that referenced this issue Jan 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants