Hi.
I’m very new to QNX. So, this question could be very simple… and i hope that…;
I want to re-connect to a device.
So, I closed a socket and intialized the socket again.
To re-connect to the device, however, I have to wait for a minute after I close the socket. If I initailize the socket right away after closing socket, It’s failed to connect to the device.
Is there any method to make a waiting time shorter?
Please help me…
problem is not in sockets but either in the device or in your code
it would help if you provide the code or at least what function fails and the error description
After colsing socket, if I use function “send”, I get error “bad file descriptor”
If I initailize the socket right away after closing socket,I get error “broken pipe” from fucntion “send”
The function “close” and “initialize_ethernet”, I made, are in different thread…
and sock is a global variable.
you have to check each call for failure, not only send() including cheking of socket(), inet_addr(), bind(), send(), recv(), setsockopt() … well, just check everything … if you did you would have found out that bind() fails with errno==EADDRINUSE
basically you have two choices
a) you do not need to bind() your local socket to a specific port, if you do not bind it then the connect() will assign your socket a free available port
b) to reuse the same local client port you can add this code anywhere between socket() and bind() calls
int reuse=1;
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));
i recommend to use option a) unless you have a strong reason to re-use the same local port all the time
(hint: for homework you can search the documentation why the send() fails with “broken pipe” or rather search for SIGPIPE and fix your code properly)
Thank you mezek~!!
I changed my code as you said.
Then I got other error massages.
In case of option a)
I erased only this line “server.sin_port = htons(10020);”
And I checked failure of these “socket(), inet_addr(), bind(), setsockopt(), connect()” functions.
For the first connection, there are no failure of these functions.
but I got error “broken pipe” from send().
I think, it’s because of setting of the other device.
the other device is set to use port “10020” and ip address “192.168.1.100” of PC.(I can change the port and ip.)
In case of option b)
I insert this line “setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))” above bind()
For the first connection, there are no failure of every functions including send(), recv()…
If I initailize the socket right away after closing socket,I get error “Address already in use” from connect()
To use this socket, I have to wait for 60 seconds exactly.
And homework…^^;
If the program try to send messages with abnormal socket connetion, the program will get signal"SIGPIPE" and It will be terminated.
To ignore this signal, I have to use the function “signal(SIGPIPE, SIG_IGN)”
Is this right…?
b) I have tried to use SO_REUSEADDR and also SO_REUSEPORT, but with no success, it seems to me that as long as the client’s connection remains in TIME_WAIT state then you cannot “reuse” the same address:port until MSL expires