Jump to content
  • Advertisement
Sign in to follow this  
MirekMalysz

trouble in winsock

This topic is 4868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)&target, 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;

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!