About socket connection..

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…

NamKug.

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

What error do you get ? Please be more precise.

How do you create the socket, what type is it. Maye you need to set SO_REUSEPORT on the socket?

thank you for your advices
my code is as following


sock = socket(AF_INET,SOCK_STREAM,0);

initialize_ethernet(&sock);

send( sock, &lan_data, sizeof(lan_data_t), 0 );
recv( sock, &lan_data_read, sizeof(lan_data_t),0 );

close(sock);

… // ← I have to wait here for 60 seconds.

sock = socket(AF_INET,SOCK_STREAM,0);

initialize_ethernet(&sock);

send( sock, &lan_data, sizeof(lan_data_t), 0 );
recv( sock, &lan_data_read, sizeof(lan_data_t),0 );

int initialize_ethernet(int *socket)
{
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(“192.168.1.100”);
server.sin_port = htons(10020); //port …
server_2.sin_family = AF_INET;
server_2.sin_addr.s_addr = inet_addr(“192.168.1.101”);//INADDR_ANY;
server_2.sin_port = htons(10010);
bind(*socket, (struct sockaddr *)&server, sizeof(server) );
length = sizeof(server);
if (0 == connect(*socket, (struct sockaddr *)&server_2, sizeof(server_2)) )
{
return 1;
}
else{
return 0;
}
}

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…?

I need more help…T_T

a) erase these lines
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(“192.168.1.100”);
server.sin_port = htons(10020); //port …
bind(*socket, (struct sockaddr *)&server, sizeof(server) );

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

I recommend to use option a) !

developerweb.net/forum/showthread.php?t=2941
faqs.org/rfcs/rfc1337.html
tools.ietf.org/html/draft-fa … oidance-00

PS: you have to reset the sockaddr_in structure before you use it, eg: memset(&server_2, 0, sizeof(server_2));

Thank you very much mezek~!!
I was too busy to reply.

I solved this problem.

Have a nice day~!