×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Socket Connectivity

Socket Connectivity

Socket Connectivity

(OP)
Just start using  Visual C++.
I detect socket connectivity with Host using 'Select' before sending a data from client (act as Middleware) Client-M-Host,
   
    FD_ZERO( &f_set );
    FD_SET( sock, &f_set );
    if( (res = select( sock + 1, NULL, &f_set,   
         NULL, &timeout )) != 1 )
       return 1;   // Socket Closed
    return 0;      // Socket still Connect

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?
Without send the data first

Thanks

RE: Socket Connectivity

Dear JCst,

In your code:
FD_SET( sock, &f_set );

where did you get the variable 'sock' and how did you initialize it?

-pete

RE: Socket Connectivity

(OP)
Thanks for replying me,
I init the variable sock like below :
  Socket Sock;

  sock = socket( AF_INET, SOCK_STREAM, 0 );
  if (sock == INVALID_SOCKET) {
        *retErrVal = SGetLastError();
        return( 0 );
  }

  memset( &addr, 0, sizeof( addr ));
  addr.sin_family = AF_INET;
  addr.sin_port = htons( (unsigned short) port );
  addr.sin_addr.s_addr = inet_addr( IPAddress );
                                                              res = connect( sock, (struct sockaddr *) &addr,
                  sizeof( addr ) );
  if (res == SOCKET_ERROR) {
    *retErrVal = SGetLastError();
    closesocket( sock );
    return( 0 );
  }
  ....  
   timeout.tv_sec = 0;
   timeout.tv_usec = 1;
   FD_ZERO( &f_set );
   FD_SET( sock, &f_set );
   if( (res = select( sock + 1, NULL, &f_set,   
       NULL, &timeout )) != 1 )
       return 1;   // Socket Closed
   return 0;      // Socket still Connect

sorry, I write down not the complete version , it'll be long.

But do I use the correct statement :
  * 'select' the socket for the write_set

on purpose to simulate 'ping' socket just like ping an address without send any data to the client/server

Thanks a lot

RE: Socket Connectivity

Dear JCst,

In this code:

if( (res = select( sock + 1, NULL, &f_set,   
       NULL, &timeout )) != 1 )
       return 1;   // Socket Closed
   return 0;      // Socket still Connect


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.

-pete

RE: Socket Connectivity


Hello,

Here is a tentative suggestion: it seems to me you did not add the socket to the "exception" fds (it is NULL in your code). Perhaps you could change that, and chek, via FD_ISSET the "exception" fds?

All the Best,

Ami Tavory

RE: Socket Connectivity

(OP)
Thanks Pete (Palbano)
Thanks Ami Tavory

I'll try your suggestion first.

By the way, anyone know Socket programming books or any link that provide a lot of stuffs about socket programming.

PS I'm not a single 'fighter' know, I've tek-tips.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close