 |
|
 |
| Author |
Message |
|
|
Post subject: About socket connection..
Posted: 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. |
|
|
| |
|
|
|
 |
|
|
Post subject: RE: About socket connection..
Posted: 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 |
|
|
| |
|
|
|
 |
|
|
Post subject: RE: About socket connection..
Posted: 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? |
|
|
| |
|
|
|
 |
|
|
Post subject: RE: About socket connection..
Posted: 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. |
|
|
| |
|
|
|
 |
|
|
Post subject: RE: About socket connection..
Posted: 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) |
|
|
| |
|
|
|
 |
|
|
Post subject: RE: About socket connection..
Posted: 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 |
|
|
| |
|
|
|
 |
|
|
Post subject: RE: About socket connection..
Posted: Nov 02, 2007 - 01:54 PM
|
|
Senior Member
Joined: Mar 03, 2004
Posts: 184
|
|
|
|
|
 |
|
|
Post subject: RE: About socket connection..
Posted: 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~! |
|
|
| |
|
|
|
 |
|
|
Powered by PNphpBB2 © 2003-2007 The PNphpBB Group Credits |
|
|
|
 |