problem with UDP WSAAsyncSelect socket, no callback message received???

This is a discussion on problem with UDP WSAAsyncSelect socket, no callback message received??? within the Networking/Device Communication forums, part of the General Programming Boards category; Code: my dialog procedure: BOOL CALLBACK MainDiaProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_INITDIALOG: { ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    114

    problem with UDP WSAAsyncSelect socket, no callback message received???

    Code:
    my dialog procedure:
    
    
    BOOL CALLBACK MainDiaProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	switch(Message)
    	{				
    
    		case WM_INITDIALOG:
    			{
    				start_listen(dia_hwnd);
    
    
    
    			}
    		break;
    
    		
    		case SOCKET_READY: 
    			{
    				MessageBox(dia_hwnd, "I GOT MSG!!", "YOU ARE FLOODING!",MB_ICONEXCLAMATION | MB_OK);
    			switch (LOWORD(wParam)) 
    			{
    			case FD_READ:
    				recv(listen_socket, recv_buff, sizeof(recv_buff),0);
    				break;}
    			}
    
    // and so on








    the following is the function i use to create a listening socket.
    i called the function "start_listen(dia_hwnd);" on initialize of the dialog box to get a socket listening.

    Code:
    char recv_buff[600];
    	
    SOCKET listen_socket ;
    
    char *start_listen(HWND what_hwnd)
    	
    {		
    
    	// Declaring a buffer on the stack
    //char *buffer = new char[256];	// or on the heap
    	
    		WSAAsyncSelect(listen_socket, what_hwnd,  SOCKET_READY,FD_READ|FD_WRITE|FD_ACCEPT|FD_CONNECT ); // i put this into WSAAsyncSelect mode)
    
    
    WSADATA wsaData;
    WORD	sockVersion = MAKEWORD(1, 1);
    	int nret;
    	SOCKADDR_IN serverInfo ;
    
    	ZeroMemory(recv_buff, 600);
    		
    	WSAStartup(sockVersion, &wsaData);
    
    		
    	serverInfo.sin_family = AF_INET;
    	serverInfo.sin_addr.s_addr = INADDR_ANY;
    	serverInfo.sin_port = htons(9000);
    
    	
    
    	listen_socket = socket(AF_INET,			// Go over TCP/IP
    					SOCK_DGRAM,		
    					IPPROTO_UDP);// Use UDP
    
    	nret = bind(listen_socket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
    				
    	if (listen_socket == INVALID_SOCKET) 
    				
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "socket()");
    		WSACleanup();
    		return "ERR:Bind";
    	}
    	nret = listen(listen_socket, 30);
    
    
      
    	if (nret == SOCKET_ERROR) 
    	{
    		return "ERR:C NO RECV";
    	}
    				
    	ZeroMemory(recv_buff, 503);
    }



    here is the function i use to send UDP packet
    Code:
    char *enterhost(char *hostname , char *full_msg, HWND hwnd)
    	
    {		
    
    	// Declaring a buffer on the stack
    //char *buffer = new char[256];	// or on the heap
    char buffer[600];	
    
    
    WSADATA wsaData;
    WORD	sockVersion = MAKEWORD(1, 1);
    	LPHOSTENT hostEntry ;
    	int nret;
    	SOCKADDR_IN serverInfo ;
    	SOCKET sending_socket ;
    
    	ZeroMemory(buffer, 600);
    	strcpy(buffer, full_msg);
    		
    	WSAStartup(sockVersion, &wsaData);
    
    		
    	hostEntry = gethostbyname(hostname); // put hostname here!
    	if (!hostEntry) {
    		nret = WSAGetLastError();
    		ReportError(nret, "gethostbyname()");	// Report the error as before
    		WSACleanup();
    		return "ERR:hostbyname";
    	}
    
    	serverInfo.sin_family = AF_INET;
    	serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
    	serverInfo.sin_port = htons(9000);
    
    	
    	sending_socket = socket(AF_INET,			
    					SOCK_DGRAM,		
    					IPPROTO_UDP);		// Use TCP rather than UDP
    
    	
    	nret = bind(sending_socket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr) );
    			
    	if (nret == INVALID_SOCKET) 
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "socket()");
    		WSACleanup();
    		return "ERR:Bind";
    	}
    
    
    
    //	nret = send(sending_socket, buffer, strlen(buffer),  0);
    	sendto(sending_socket, buffer,600,0, (SOCKADDR *) &serverInfo, sizeof(serverInfo)); // now which of these to use?
    							
    
    	if (nret == SOCKET_ERROR) 
    	{
    		return "ERR:C NO RECV";
    	}
    				
    	ZeroMemory(buffer, 503);
    	shutdown(sending_socket,3);
    	closesocket(sending_socket);
    	WSACleanup();
    
    }
    i call this function using "enterhost("localhost","hello! testing", hwnd);" on a test button on the dialog

    now the problem is it seems like there is no callback message received when i press the test button

    using
    vc++6.0
    win98
    API style of coding( dunno MFC)
    and have #include winsock2 with it's lib
    #pragma comment(lib, "Ws2_32.lib")

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    First, make sure the call to WSAAsyncSelect() returns true.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Listening socket and thread problem
    By esaptonor in forum Windows Programming
    Replies: 6
    Last Post: 06-19-2010, 04:04 AM
  2. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-06-2009, 12:05 AM
  3. Socket Select problem
    By saipkjai in forum Networking/Device Communication
    Replies: 4
    Last Post: 02-08-2008, 10:57 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM

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