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

    Asynchronous sockets

    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
    Apr 2006
    Posts
    21

    Re: Asynchronous sockets

    http://www.codeguru.com/forum/showthread.php?t=501393

    Read this one and if you can handle waiting minutes for the .exe to be removed from system you are welcome to my source code.
    I have a multithread version in assembler if of interest.
    Last edited by turnbui; August 13th, 2010 at 07:58 AM.

  3. #3
    Join Date
    Feb 2007
    Posts
    102

    Re: Asynchronous sockets

    Thank you for your reply.

    The problem is not (yet) about the deletion of the socket but about the connection.
    I have never used the "select" function. If a socket is connected, does the select function always return when check for writing event (because there are always some space available for writing)?

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