Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket Connectivity

Status
Not open for further replies.

JCst

Programmer
Joined
Jun 14, 2000
Messages
3
Location
KR
Just start using&nbsp;&nbsp;Visual C++.<br>I detect socket connectivity with Host using 'Select' before sending a data from client (act as Middleware) Client-M-Host, <br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;FD_ZERO( &f_set );<br>&nbsp;&nbsp;&nbsp;&nbsp;FD_SET( sock, &f_set );<br>&nbsp;&nbsp;&nbsp;&nbsp;if( (res = select( sock + 1, NULL, &f_set,&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL, &timeout )) != 1 ) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;&nbsp;&nbsp;&nbsp;// Socket Closed <br>&nbsp;&nbsp;&nbsp;&nbsp;return 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Socket still Connect<br><br>but the result is not good; it can't detect although remote host already closed the socket. Is there any other way to detect connectivity beetwen M-Host?<br>Without send the data first <br><br>Thanks
 
Dear JCst,<br><br>In your code:<br>FD_SET( sock, &f_set );<br><br>where did you get the variable 'sock' and how did you initialize it?<br><br>-pete
 
Thanks for replying me,<br>I init the variable sock like below :<br>&nbsp;&nbsp;Socket Sock;<br><br>&nbsp;&nbsp;sock = socket( AF_INET, SOCK_STREAM, 0 );<br>&nbsp;&nbsp;if (sock == INVALID_SOCKET) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*retErrVal = SGetLastError();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return( 0 );<br>&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;memset( &addr, 0, sizeof( addr ));<br>&nbsp;&nbsp;addr.sin_family = AF_INET;<br>&nbsp;&nbsp;addr.sin_port = htons( (unsigned short) port );<br>&nbsp;&nbsp;addr.sin_addr.s_addr = inet_addr( IPAddress );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res = connect( sock, (struct sockaddr *) &addr, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sizeof( addr ) );<br>&nbsp;&nbsp;if (res == SOCKET_ERROR) {<br>&nbsp;&nbsp;&nbsp;&nbsp;*retErrVal = SGetLastError();<br>&nbsp;&nbsp;&nbsp;&nbsp;closesocket( sock );<br>&nbsp;&nbsp;&nbsp;&nbsp;return( 0 );<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;....&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;timeout.tv_sec = 0;<br>&nbsp;&nbsp;&nbsp;timeout.tv_usec = 1;<br>&nbsp;&nbsp;&nbsp;FD_ZERO( &f_set );<br>&nbsp;&nbsp;&nbsp;FD_SET( sock, &f_set );<br>&nbsp;&nbsp;&nbsp;if( (res = select( sock + 1, NULL, &f_set,&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL, &timeout )) != 1 ) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;&nbsp;&nbsp;&nbsp;// Socket Closed <br>&nbsp;&nbsp;&nbsp;return 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Socket still Connect<br><br>sorry, I write down not the complete version , it'll be long. <br><br>But do I use the correct statement :<br>&nbsp;&nbsp;* 'select' the socket for the write_set<br><br>on purpose to simulate 'ping' socket just like ping an address without send any data to the client/server<br><br>Thanks a lot
 
Dear JCst,<br><br>In this code:<br><br>if( (res = select( sock + 1, NULL, &f_set,&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL, &timeout )) != 1 ) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;&nbsp;&nbsp;&nbsp;// Socket Closed <br>&nbsp;&nbsp;&nbsp;return 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Socket still Connect<br><br><br>select() can return the number of sockets that are ready to read or write or it can return SOCKET_ERROR. So you should be checking for that and when found use WSAGetLastError() to find out the specific error that is occuring.<br><br>-pete
 
<br>Hello,<br><br>Here is a tentative suggestion: it seems to me you did not add the socket to the &quot;exception&quot; fds (it is NULL in your code). Perhaps you could change that, and chek, via FD_ISSET the &quot;exception&quot; fds?<br><br>All the Best,<br><br>Ami Tavory <br>
 
Thanks Pete (Palbano) <br>Thanks Ami Tavory<br><br>I'll try your suggestion first.<br><br>By the way, anyone know Socket programming books or any link that provide a lot of stuffs about socket programming.<br><br>PS I'm not a single 'fighter' know, I've tek-tips.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top