WSAAsyncSelect

This is a discussion on WSAAsyncSelect within the Networking/Device Communication forums, part of the General Programming Boards category; This is a server with WSAAsyncSelect. I get 10093 error even before calling WSAStartup and the rest of the socket ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    WSAAsyncSelect

    This is a server with WSAAsyncSelect.

    I get 10093 error even before calling WSAStartup and the rest of the socket initialization functions.

    The error comes from WSAGetLastError() from here:

    Code:
    if(WSAGETSELECTERROR(lParam))
                       {      // If an error occurred,
                           cout<<WSAGetLastError()<<"\r\n";
    How could it be possible, i didnt even call WM_SERVER yet?

    Code:
    #define WIN32_LEAN_AND_MEAN
    
    #include <winsock2.h>
    #include <windows.h>
    #include <Windowsx.h>
    #include <string>
    #include <vector>
    #include <process.h>
    #include <iostream>
    using namespace std;
    
    #pragma comment(lib,"user32.lib")
    #pragma comment(lib,"Gdi32.lib")
    
    #define IDB_TEXT1 101
    #define IDB_TEXT2 102
    #define Send      103
    #define Server    104
    #define Client    105
    #define Ip        106
    
    #define WM_SERVER (WM_USER + 108)
    #define DATA_BUFSIZE 1024
    
    HWND hwnd, hWin_1, hWin_2, hSend, hServer, hClient, hIp;
    HANDLE h;
    MSG Msg;
    HDC hdc;
    PAINTSTRUCT ps;
    unsigned int port = 666, i;
    
    string buffer, buffer2 = "You > ", buffer3;
    SOCKET sock, Accept;
    sockaddr_in sockad;
    WPARAM wParam;
    DWORD RecvBytes;
    DWORD SendBytes;
    DWORD Flags;
    
    void Bind_and_Listen( int port )
    {
         WSADATA wsaData;
         if(WSAStartup( MAKEWORD(2, 2), &wsaData ) != 0 ){
                    cout <<"WSAStartup() failed with error " << WSAGetLastError(); exit(1);
         }
    
    
         //Create a socket
         if((sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )) == INVALID_SOCKET ){
                    cout <<"socket() failed with error " << WSAGetLastError(); WSACleanup(); exit(1);
         }
    
          if(WSAAsyncSelect(sock, hwnd, WM_SERVER, FD_ACCEPT|FD_CLOSE) == 0)
            cout<<"WSAAsyncSelect() is OK\n";
         else
            cout<<"WSAAsyncSelect() failed with error code "<< WSAGetLastError() << "\n";
    
         sockad.sin_family = AF_INET;
         sockad.sin_addr.s_addr = htonl(INADDR_ANY);//inet_addr( "0.0.0.0" );
         sockad.sin_port = htons( port );
    
    
         if ( bind (sock, (SOCKADDR*) &sockad, sizeof( sockad) ) == SOCKET_ERROR )
         {
             cerr << "ServerSocket: Failed to bind "<< WSAGetLastError();
             WSACleanup(); exit(1);
         }
    
    
         if (listen(sock, 5))
         {
           cout <<"listen() failed with error " << WSAGetLastError();
           WSACleanup(); exit(1);
         }
    
         SendMessage(hWin_1, LB_INSERTSTRING, -1, (LPARAM)"WAITING FOR CLIENT...");
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
      switch(msg){
                  case WM_CREATE:
                       hWin_1 = CreateWindowEx(0,"ListBox", 0,
                             WS_CHILD | WS_VISIBLE | WS_BORDER | ES_WANTRETURN |
                             LBS_NOTIFY | WS_VSCROLL,
                             20, 20, 550, 460, hwnd, (HMENU)IDB_TEXT1, 0, NULL);
                       hWin_2 = CreateWindowEx(0,"Edit","Hi!",
                             WS_VISIBLE | WS_CHILD |  WS_VSCROLL |
                             ES_MULTILINE ,
                             20, 500, 550, 180,hwnd,(HMENU) IDB_TEXT2, 0,NULL);
                       hSend  = CreateWindowEx(0,"Button","Send",
                             WS_CHILD | WS_VISIBLE | WS_BORDER,
                             20, 700, 550, 20,hwnd,(HMENU)Send,0,NULL);
                       hServer  = CreateWindowEx(0,"Button","Server",
                             WS_CHILD | WS_VISIBLE | WS_BORDER,
                             600, 20, 150, 20,hwnd,(HMENU)Server,0,NULL);
                       hClient  = CreateWindowEx(0,"Button","Connect To Server",
                             WS_CHILD | WS_VISIBLE | WS_BORDER,
                             600, 50, 150, 20,hwnd,(HMENU)Client,0,NULL);
                       hIp = CreateWindowEx(0,"Edit","127.0.0.1",
                             WS_CHILD | WS_VISIBLE | WS_BORDER,
                             600, 80, 150, 20,hwnd,(HMENU) Ip,
                             ((LPCREATESTRUCT)lParam)->hInstance,NULL);
    
                       SetFocus(hWin_2);
                       break;
    
                  case WM_COMMAND:
                       switch(wParam)
                       {
                        case Send:
                             //SendData(buffer);
                             break;
                        case Server:
                             Bind_and_Listen( port );
                             cout <<"server ok\r\n";
                             EnableWindow(hServer, FALSE);
                             EnableWindow(hClient, FALSE);
                             break;
                        case Client:
                             cout <<"client ok\r\n";
                           //  Connect();
                             break;
                       }
    //////////////////////////SERVER///////////////////////////////////////////
                  case WM_SERVER:
                       if(WSAGETSELECTERROR(lParam))
                       {      // If an error occurred,
                           cout<<WSAGetLastError()<<"\r\n";
                           closesocket (sock);   WSACleanup ();        // Shutdown Winsock
                           return 0;
                       }
                       switch(WSAGETSELECTEVENT(lParam))
                       {        // What happened, exactly?
                             cout<<lParam<<endl;
                        case FD_ACCEPT:
                             if ((Accept = accept(wParam, NULL, NULL)) == INVALID_SOCKET)
                             {
                                cout<<"accept() failed with error " << WSAGetLastError()<<"\n";
                                break;
                             }
                             else
                                cout<<"accept() is OK!\n";
                             SendMessage(hWin_1, LB_INSERTSTRING, -1, (LPARAM)"CLIENT CONNECTED.");
                             cout << "Socket number: " << Accept << " connected\n";
    
                             WSAAsyncSelect(Accept, hwnd, WM_SERVER, FD_READ|FD_CLOSE);
                             break;
                        case FD_READ:
                             //Receive data
                             cout <<"fdread\r\n";
                                                     //RecvData();
                             break;
                        case FD_CLOSE:
                             closesocket (Accept);
                             WSACleanup(); exit(1);
                             break;
                       }
                       break;
    
    
                  case WM_CLOSE:
                       DestroyWindow(hwnd);
                       break;
    
                  case WM_DESTROY:
                       PostQuitMessage(0);
                       break;
                  default:
                       return DefWindowProc(hwnd,msg,wParam,lParam);
           }
           return 0;
    }
    ////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////WINMAIN/////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    //int main()
    {
        WNDCLASSEX wc;
        wc.hInstance  = hInstance;
        wc.cbClsExtra = 0;
        wc.cbSize     = sizeof(WNDCLASSEX);
        wc.cbWndExtra = 0;
        wc.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(200,300,200));
        wc.hCursor       = LoadCursor(NULL,IDC_ARROW);
        wc.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
        wc.lpfnWndProc   =  WndProc;
        wc.lpszClassName = "Class";
        wc.lpszMenuName  = NULL;
        wc.style         = 0;
        wc.hIconSm       = NULL;
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        hwnd = CreateWindowEx(WS_EX_LEFT|WS_EX_LTRREADING|WS_EX_WINDOWEDGE,
                               "Class",
                               " The_Evil_Chatter",
                               WS_OVERLAPPEDWINDOW,
                               CW_USEDEFAULT, CW_USEDEFAULT, 900, 800,
                               HWND_DESKTOP, NULL, hInstance, NULL);
    
        if(hwnd == NULL){
           MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
              TranslateMessage(&Msg);
              DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You are calling WSAAsyncSelect before you bind the socket. That might be your problem.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    This is how its done in a working example and notice that i call it a second time in FD_ACCEPT:

    And btw i get error even before i call anything.

    You call it with the "server" button.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    And btw i get error even before i call anything.
    What do you mean by that? You mean before you click the "server" button?
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Yes before i click the server button i already got:

    0
    10093
    0
    Compiler MSVC++ 2013 with Code::Blocks.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Ducky View Post
    I get 10093 error even before calling WSAStartup and the rest of the socket initialization functions.
    You have to call WSAStartup() before you call any other socket related functions. If you do not, any information they return is utterly useless.

    Quote Originally Posted by MSDN
    WSANOTINITIALISED
    10093
    Successful WSAStartup not yet performed.
    Either the application has not called WSAStartup or WSAStartup failed. The application may be accessing a socket that the current active task does not own (that is, trying to share a socket between tasks), or WSACleanup has been called too many times.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Yes, thanks Abachler but the point was that i DID NOT call any socket related function yet and i got already an error.

    But now i dont get anymore error i dont know why i changed nothing.

    The thing is that i can connect to it but it apparently never enters FD_ACCEPT so i dont know how it is connected.

    The things in FD_ACCEPT never get printed out but things in Bind_and_Listen( port ); do.
    Last edited by Ducky; 12-16-2009 at 04:39 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    It's because you don't have a break for your WM_COMMAND handler. Therefore, when you get a WM_COMMAND message, it falls through to the WM_SERVER handler.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Oh God, thanks a million Bithub!
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WSAAsyncSelect Socket Model. She's Not Hot.
    By Tonto in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-24-2007, 08:34 AM
  2. Winsock WSAAsyncSelect
    By High in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 12:20 AM
  3. problem with UDP WSAAsyncSelect socket, no callback message received???
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-29-2004, 10:59 AM
  4. WSAAsyncSelect I/O & Multithreading :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 4
    Last Post: 09-27-2002, 02:28 AM
  5. WSAAsyncSelect I/O Mode :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-12-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21