Advertisement

trouble in winsock

Started by July 22, 2005 01:47 PM
1 comment, last by Omid Ghavami 19 years, 7 months ago
im having alot of trouble in winsock. it connects and everything but when i try to send data when i press the right arrow key, nothing happens. im completly lost in how im supposed to send and receive data. heres my code if it is any help:

//          ----data thats being sent wen the right arrow key is pressed------

	if (keys[VK_RIGHT])						
	{	
		char buffer[11];  
	        sprintf (buffer, "Whatever…");
		send (s, buffer, sizeof(buffer), 0);
	}


//                        ----setting up the server--------------

if (keys[VK_HOME]){
	if (only_once ==0){
		WSAStartup(sockVersion, &wsaData);
		listeningSocket = socket(AF_INET,
					SOCK_STREAM,   	
					0);					
		SOCKADDR_IN addr; // the address structure for a TCP socket

		addr.sin_family = AF_INET;     
		addr.sin_addr.s_addr = INADDR_ANY;  
		addr.sin_port = htons(6300);   
                if (bind(listeningSocket, (LPSOCKADDR)&addr, sizeof(struct sockaddr)) == SOCKET_ERROR)
		{ // error
		  retv = WSAGetLastError();
		  ReportError(retv,"listen()");
		  WSACleanup ();  // unload WinSock
		  return NETWORK_ERROR;         // quit
		}
	retv = listen(listeningSocket, 10);   
                                              
        if (retv == SOCKET_ERROR) {
           retv = WSAGetLastError();
           ReportError(retv, "listen()");
           WSACleanup();
           return NETWORK_ERROR;
        }
   only_once = 1;
	}
}

// --------------setting up the client--------------------

if (keys[VK_SPACE])
{
					 
	if (only_once1 ==0){
	     WSAStartup(sockVersion, &wsaData);
             s = socket (AF_INET, SOCK_STREAM, 0); // Create socket

             sockaddr_in target;

             target.sin_family = AF_INET;          
             target.sin_port = htons (6300);        
             target.sin_addr.s_addr = inet_addr ("27.36.35.285"); 

         if (connect(s, (LPSOCKADDR)⌖, sizeof(target)) == SOCKET_ERROR)
         {				
                retv = WSAGetLastError();
	        ReportError(retv,"connect()");
                WSACleanup ();
                return FALSE;
         }
WSAAsyncSelect (s,hWnd,WM_ONSOCKET,(FD_CLOSE | FD_CONNECT | FD_READ));
only_once1 = 1;
              }
}

// ----------------------in my WndProc---------------------------

case WM_ONSOCKET:
{
    if (WSAGETSELECTERROR(lParam))
    { 
           sprintf (txtbuffer,"Error2: %d",WSAGetLastError());
	   MessageBox (hWnd,txtbuffer,"Error",MB_OK);
           WSACleanup ();
	   PostQuitMessage (0);
	   active = FALSE;
           return 0;				
    }

    switch (WSAGETSELECTEVENT(lParam))
    {
      case FD_READ:

      char buffer[80]; // buffer that is 80 characters big

      recv (s, buffer, sizeof(buffer), 0);
      MessageBox (hWnd,"works","Error",MB_OK); //no message box appears telling me this case isnt even getting called..

      case FD_CONNECT:

      case FD_CLOSE:
		  
		  ;
    }
  } break;
First thing I see without looking any further is that while you issue a listen() which sets up the socket to listen for incoming connections, I never actually see any accept() anywhere to accept an incoming connection.

Robert
Advertisement
Indeed as rmsimpson already noted, you have to call accept() on the server to actually accept incoming connection attempts.
The SOCKET variable 's' used in the send function is not set anywhere in your code at the moment.
Best regards, Omid

This topic is closed to new replies.

Advertisement