CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2007
    Posts
    102

    Socket connection and select

    Hi all,

    I am trying to develop an asynchronous socket class that would work in a similar way that CAsyncSocket on windows (MFC). My target is to use it also under Linux.

    My first problem is this one:

    When I call the "Connect" function to connect my socket to as server using an ip address and a port number, the connect function returns immediately with EINPROGRESS error code. This is normal.
    Now I want to detect when the socket will be connected and also when there will be some data available for reading and enough buffer space for writing data. I read a lot of documentation and found at that I should use the "select" function. Normally, this function is blocking until an event on my socket will occur (or optionally a timeout). So, for a simple test, just after the connect function call, I enter an infinite loop that contain a select. As soon as my socket succeeds to connect, the select function returns and I can detect that the socket was connected. Great! But from now, event the socket is not receiving or sending data, the select function (which is an infinite loop) is always returning immediately. I thought that it should block because there are no new event since the connection.

    Can someone explain me why this happen?

    If it can help, I put some code here:

    void Test() {
    CTestClientSocket *up_Client = new CTestClientSocket;
    if (up_Client->Create()) {
    if (up_Client->Connect("192.168.1.2", 108)) NSLog(@"CONNECTION TO THE SERVER OK");
    else {

    if (errno == EINPROGRESS) {
    NSLog(@"THE CONNECTION TO THE SERVER IS PENDING");

    while(1) {
    struct timeval tv;
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    fd_set myset;
    FD_ZERO(&myset);
    FD_SET(up_Client->mu_Socket, &myset);
    int res = select(up_Client->mu_Socket+1, NULL, &myset, NULL, NULL);

    if (res < 0 && errno != EINTR) {
    NSLog(@"ERROR CONNECTING");
    break;
    }
    else
    if (res > 0) {
    socklen_t lon = sizeof(int);
    int valopt;
    if (getsockopt(up_Client->mu_Socket, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) {
    NSLog(@"Error in getsockopt()");
    break;
    }
    // Check the value returned...
    if (valopt) {
    NSLog(@"Error in delayed connection");
    break;
    }
    else {
    NSLog(@"CONNECTED!");
    }
    //break;
    }
    else {
    NSLog(@"TIME OUT IN SELECT");
    //break;
    }
    }

    }
    else NSLog(@"CONNECTION TO THE SERVER FAILED");
    }
    }
    }

  2. #2
    Join Date
    Aug 2004
    Location
    Bucuresti
    Posts
    38

    Re: Socket connection and select

    This is the signature of the select function:
    Code:
    int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout);
    Based on this, you are using this function to know when your socket is ready for writting (only writtable set is not null) . So
    - in case of asynchronous connect, when the connection procedure is completed, the socket is put into writtable set - you used this feature and it worked well
    - after that, using the same select call, is telling you that the socket is ready to be used for writting, which is almost always true , so there is no surprise that the function returns immedialtely.
    I suggest you to search the internet for samples about how select is used to check if there is some data to read/write on a socket.
    Regards,
    Cosmin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured