Thread: Sockets switching data?

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    102

    Sockets switching data?

    Hello, I've made a simple program that connects to an IRC server. I'm now working on the ident server bit, that listens on port 113. I've come across something rather strange though.
    I have two sockets, one that connects to the server on port 6667 and another that listens on port 113. When the socket on port 113 recieves data, it has the correct data at the beginning, but after that it seems to have data from the other socket also..
    I'm using asynchronous sockets, here's the problem code:

    Code:
    case WM_IDENTSOCK:
            {
                switch(WSAGETSELECTEVENT(lParam))
                {
                case FD_ACCEPT:
                    {
                        if (WSAGETSELECTERROR(lParam) != 0)
                        {
                            return(FALSE);
                        }
                        IdentSocket= accept(wParam,0,0);
                        return 0 ;
                    }
                case FD_READ:
                    {
                        char iData[1000];
                        if (WSAGETSELECTERROR(lParam) != 0)
                        {
                            int error    = WSAGetLastError();
                            wsprintf(iData,"%d",error);
                            MessageBox(0,iData,"",0);
                            return false;
                        }
                        int bytes = recv(IdentSocket, iData, sizeof(iData)-1, 0);
                        if(bytes == SOCKET_ERROR)MessageBox(hwnd,"Error in FD_READ, bytes read == SOCKET_ERROR","Error",0);
                        else{
                            MessageBox(0,iData,"",0); //this string seems to have data from the other socket after the correct data
                            iData[bytes] = '\0';
                            MessageBox(0,iData,"",0); //this data is the correct data
                        }
                        return 0;
                    }
                case FD_CLOSE:
                    {
                        if (WSAGETSELECTERROR(lParam) != 0)
                            return(FALSE);
                        MessageBox(0,"Connection Closed","Ident Server",0);
                        shutdown(IdentSocket,0);
                        WSACleanup();
                        return 0 ;
                    }
                }
                return 0;
            }
        case WM_CLIENTSOCK:
            {
                switch(WSAGETSELECTEVENT(lParam))
                {
                case FD_READ:
                    {
                        char data[1000];
                        if (WSAGETSELECTERROR(lParam) != 0)
                        {
                            int error    = WSAGetLastError();
                            wsprintf(data,"%d",error);
                            MessageBox(0,data,"",0);
                            return false;
                        }
                        int bytes = recv(wParam, data, sizeof(data)-1, 0);
                        if(bytes == SOCKET_ERROR)MessageBox(hwnd,"Error in FD_READ, bytes read == SOCKET_ERROR","Error",0);
                        else{
                            data[bytes] = '\0';
                            if(string(data).substr(0,4) == "PING")
                            {
                                strncpy(data,"PONG",4);
                                AddData("Ping?");
                                bytes = send(wParam,data, strlen(data),0);
                                if(bytes == SOCKET_ERROR)
                                {
                                    int error    = WSAGetLastError();
                                    wsprintf(data,"%d",error);
                                    MessageBox(0,data,"send() Error",0);
                                }
                                else AddData(" Pong!\r\n");
                            }
                            else AddData(data);
                        }
                        return 0;
                    }
                case FD_CLOSE:
                    {
                        if (!WSAGETSELECTERROR(lParam))
                            return(FALSE);
                        MessageBox(0,"Connection Closed","",0);
                        shutdown(wParam,0);
                        WSACleanup();
                        return 0 ;
                    }
                }
            }
    These are my two messages that handle each socket. Any explanation would be good.

    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    MessageBox(0,iData,"",0); //this string seems to have data from the other socket after the correct data
    iData[bytes] = '\0';
    MessageBox(0,iData,"",0); //this data is the correct data

    That's because recv() doesn't terminate your buffer with a \0 - you have to do that (which you do).
    Just delete the first MessageBox() call, and you're good
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    But what confuses me is where has that other data come from. I'm not using the same buffer or socket. where is that data coming from?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char iData[1000];
    > char data[1000];
    Since these are in different blocks of code (non-overlapping scope), my guess is your compiler assigned them the same part of stack memory.
    So writing data into one array will appear as uninitialised data in the other.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    Thanks Salem. If when I declare iData, I do char iData[1000] = ""; It solves the problem.
    Thank you for your explanation.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    No, iData[bytes] = '\0'; solves the problem.

    iData[1000] = ""; just covers up the problem by filling the array with \0, but you still need the iData[bytes] = '\0'; to be correct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Replies: 1
    Last Post: 08-01-2002, 08:26 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM