OpenQNX :: The QNX Community Portal

May 18, 2008 - 12:09 AM
Google
  Web openqnx.com   
     Create an account Home · Submit News · QNX Forums · QNX Download · Search   
_
Main Menu
Who's Online
There are 41 unlogged users and 1 registered user online.

You can log-in or register for a user account here.

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
ripipia
Post subject: About socket connection..  PostPosted: Oct 31, 2007 - 01:06 AM
New Member


Joined: Oct 31, 2007
Posts: 4

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.
 
 View user's profile Send private message  
Reply with quote Back to top
mezek
Post subject: RE: About socket connection..  PostPosted: Oct 31, 2007 - 07:41 AM
Senior Member


Joined: Mar 03, 2004
Posts: 184

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
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
mario
Post subject: RE: About socket connection..  PostPosted: Oct 31, 2007 - 12:15 PM
QNX Master


Joined: Sep 01, 2002
Posts: 2675

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?
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
ripipia
Post subject: RE: About socket connection..  PostPosted: Nov 01, 2007 - 12:50 AM
New Member


Joined: Oct 31, 2007
Posts: 4

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.
 
 View user's profile Send private message  
Reply with quote Back to top
mezek
Post subject: RE: About socket connection..  PostPosted: Nov 01, 2007 - 08:08 AM
Senior Member


Joined: Mar 03, 2004
Posts: 184

- 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)
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
ripipia
Post subject: RE: About socket connection..  PostPosted: Nov 01, 2007 - 07:42 PM
New Member


Joined: Oct 31, 2007
Posts: 4

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
 
 View user's profile Send private message  
Reply with quote Back to top
mezek
Post subject: RE: About socket connection..  PostPosted: Nov 02, 2007 - 01:54 PM
Senior Member


Joined: Mar 03, 2004
Posts: 184

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) !

http://www.developerweb.net/forum/showthread.php?t=2941
http://www.faqs.org/rfcs/rfc1337.html
http://www.tools.ietf.org/html/draft-faber-time-wait-avoidance-00

PS: you have to reset the sockaddr_in structure before you use it, eg: memset(&server_2, 0, sizeof(server_2));
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
ripipia
Post subject: RE: About socket connection..  PostPosted: Nov 07, 2007 - 04:48 AM
New Member


Joined: Oct 31, 2007
Posts: 4

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

I solved this problem.

Have a nice day~!
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2007 The PNphpBB Group
Credits
All logos and trademarks in this site are property of their respective owners. The comments are property of their posters.
Powered by OpenQNX: The QNX Community Portal Site
QNX and the QNX logo are registered trademarks of QNX Software Systems.