Problem mit Socket



  • Hi, hab das PRoblem, dass das FD_CONNECT evnet und das FD_READ event nicht funktionieren! (Beim Clienten)

    SERVER:

    #include <windows.h>
    #include <winsock2.h>
    
    #define WM_SOCKET_NOTIFY (WM_USER + 1)
    
    #define Bu 1
    
    SOCKET listen_socket,listen_socket2;
    
    void initwinsock()
    {
    
    	WORD wVersionRequested;
    	WSADATA wsaData;
    
    	wVersionRequested = MAKEWORD( 2, 0 );
    
    	WSAStartup( wVersionRequested, &wsaData );
    
    	}
    
    void winsockconnect(HWND hwnd)
    {
    	listen_socket = socket(AF_INET,SOCK_STREAM,0);
    
    	struct sockaddr_in addy2;
    
    	addy2.sin_family = AF_INET;
        addy2.sin_port = htons(1234);
    
    	addy2.sin_addr.s_addr = inet_addr("192.168.178.11");
    		bind(listen_socket,(struct sockaddr*)&addy2, sizeof(addy2));
    	   listen(listen_socket, 5);
     	MessageBox(hwnd,"Bind ist erfolgreich! Listen()... ","Hinweis!",MB_ICONINFORMATION);
    }
    
    void closewinsock()
    {
    	WSASendDisconnect(listen_socket, NULL);
    	closesocket(listen_socket);
    	WSACleanup();
    }
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               200,                 /* The programs width */
               200,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
     HWND hwbu;
     char data[]="Hallo Client!";
     int bytes;
        switch (message)                  /* handle the messages */
        {
    
                case WM_CREATE :
    
    		hwbu = CreateWindow ("button", "Listen()...",
    				  WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
    				  0, 0, 60, 20, hwnd, (HMENU) Bu,
    				  ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;//Button "=" bzw. Berechne
    
                    return 0 ; 
    
              case WM_COMMAND:
    			  switch (HIWORD(wParam))
    			  {
    			  case BN_CLICKED:
    				  switch(LOWORD(wParam))
    				  {
    				  case Bu://Button but (hwndbut)
    					  initwinsock();
    					  winsockconnect(hwnd);
    					  	WSAAsyncSelect(listen_socket,hwnd,WM_SOCKET_NOTIFY,FD_READ|FD_CLOSE|FD_ACCEPT);
    
    					  break;
    				  }
    				  break;
    			  }
    			  return 0;
    
            	case WM_SOCKET_NOTIFY:
    			switch(LOWORD(lParam))
    			{
    			case FD_READ:
    
    			return 0;
    
    			case FD_ACCEPT:
    
         			listen_socket = accept(listen_socket,0,0);
    
         			if(listen_socket == INVALID_SOCKET)
         			MessageBox(hwnd,"Verbindung fehlgeschlagen!","Hinweis!",MB_ICONINFORMATION);    
                     else      
                     MessageBox(hwnd,"Sie sind nun verbunden!","Hinweis!",MB_ICONINFORMATION);          
    				return 0;
    
                   bytes = send(listen_socket,data,strlen(data),0);
    
        if (bytes == -1)
    	{
    		 MessageBox(hwnd,"Zeichen konnten nicht gesendet werden!","Hinweis!",MB_ICONINFORMATION);    
    		return -1;
    	}
    
    			case FD_CLOSE:
                    closewinsock();
    				MessageBox(hwnd,"Verbindung beendet!","Hinweis!",MB_ICONINFORMATION);      
    
    				return 0;
    			} 
    
            case WM_DESTROY:
    
                closewinsock();
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    

    CLIENT :

    #include <windows.h>
    #include <winsock2.h>
    
    #define WM_SOCKET_NOTIFY (WM_USER + 1)
    
    #define Bu 1
    
    SOCKET connect_socket;
    
    void initwinsock()
    {
    
    	WORD wVersionRequested;
    	WSADATA wsaData;
    
    	wVersionRequested = MAKEWORD( 2, 0 );
    
    	WSAStartup( wVersionRequested, &wsaData );
    
    	}
    
    void winsockconnect(HWND hwnd)
    {
    	connect_socket = socket(AF_INET,SOCK_STREAM,0);
    
    	struct sockaddr_in addy2;
    
    	addy2.sin_family = AF_INET;
        addy2.sin_port = htons(1234);
    
    	addy2.sin_addr.s_addr = inet_addr("192.168.178.11");
    		bind(connect_socket,(struct sockaddr*)&addy2, sizeof(addy2));
    
    		if(connect(connect_socket,(struct sockaddr*)&addy2,sizeof(addy2))==SOCKET_ERROR){
    	MessageBox(0,"Bei connect()","Socket Error",0);}
    
    else {                        
    
         WSAAsyncSelect(connect_socket,hwnd,WM_SOCKET_NOTIFY,FD_CONNECT|FD_READ|FD_CLOSE); }
    
    }
    
    void closewinsock()
    {
    	WSASendDisconnect(connect_socket, NULL);
    	closesocket(connect_socket);
    	WSACleanup();
    }
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               200,                 /* The programs width */
               200,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    { HWND hwbu;
    				int ilen;
                    char data[255];
        switch (message)                  /* handle the messages */
        {
    
                case WM_CREATE :
    
    		hwbu = CreateWindow ("button", "Connect()...",
    				  WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
    				  0, 0, 80, 20, hwnd, (HMENU) Bu,
    				  ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;//Button "=" bzw. Berechne
    
                    return 0 ; 
    
              case WM_COMMAND:
    			  switch (HIWORD(wParam))
    			  {
    			  case BN_CLICKED:
    				  switch(LOWORD(wParam))
    				  {
    				  case Bu://Button but (hwndbut)
    					  initwinsock();
    					  winsockconnect(hwnd);
    
    					  break;
    				  }
    				  break;
    			  }
    			  return 0;
    
            	case WM_SOCKET_NOTIFY:
    			switch(LOWORD(lParam))
    			{
    			case FD_READ:
    
                    ilen = recv(connect_socket,data,255,0);
                    data[ilen]='\0';
    
                    MessageBox(hwnd,data,"Nachricht vom Server :",MB_ICONINFORMATION);   
    
    				return 0;
    
                case FD_CONNECT:
    
                     MessageBox(hwnd,"Verbindung hergestellt!","Hinweis!",MB_ICONINFORMATION);   
    
                     return 0;
    
    			case FD_CLOSE:
                    closewinsock(); 
    				MessageBox(hwnd,"Verbindung beendet!","Hinweis!",MB_ICONINFORMATION);   
    
    				return 0;
    			} 
    
            case WM_DESTROY:
    
                 closewinsock();
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    

    Bei beiden Projekten ist natürlich noch die libws2.a gelinkt...
    Der CLient connected zwar richtig auf den Server allerdings wird nicht das event FD_CONNECT abgegeben ??? Und wenn ich ne Text Message schicke funktioniert das leider auch nicht 😞 Könnt ihr helfen ???

    mfg FoX


Anmelden zum Antworten