winsock problem

This is a discussion on winsock problem within the C++ Programming forums, part of the General Programming Boards category; Heres the code, for a basic client/server app which i got from a tutorial as a base. Code: #include <iostream.h> ...

  1. #1
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186

    Question winsock problem

    Heres the code, for a basic client/server app which i got from a tutorial as a base.

    Code:
    #include <iostream.h>
    #include <winsock2.h>
    
    const unsigned int maxchars = 200;
    
    void client()
    {
        char dest_ip[15];
    
        cout << "Destination IP Address: ";
        cin >> dest_ip;
        cout << endl;
    
        SOCKET exp;
    
        if (exp = socket(AF_INET,SOCK_STREAM,0))
    	   cout << "--- Sending socket created successfully." << endl;
        else
        {
    	   cout << "Can't create sending socket. Aborting." << endl;
    	   WSACleanup();
    	   exit(0);
        }
    
        sockaddr_in target;
    
        target.sin_family = AF_INET;
        target.sin_port = htons(5656);
        target.sin_addr.s_addr = inet_addr(dest_ip);
    
        if (connect(exp,(LPSOCKADDR)&target,sizeof(target)) == SOCKET_ERROR)
        {
    	   cout << "Can't connect. Aborting." << endl;
    	   WSACleanup();
    	   exit(0);
        }
        else
        {
    	   cout << "--- Connected successfully." << endl;
    	   cout << " -| Enter 666 when you want to end the session." << endl;
    
    	   char buff[maxchars];
    
    	   while(buff[0] != '6' || buff[1] != '6' || buff[2] != '6')
    	   {
    		  cin.getline(buff,maxchars);
    		  cout << "Enter data: ";
    
    		  if (buff[0] != '6' || buff[1] != '6' || buff[2] != '6')
    			 send(exp,buff,sizeof(buff),0);
    		  else
    		  {
    			 send(exp,buff,sizeof(buff),0);
    			 closesocket(exp);
    			 cout << "--- Session closed." << endl;
    			 WSACleanup();
    			 exit(0);
    		  }
    	   }
        }
    }
    
    void server()
    {
        SOCKET imp;
    
        if (imp = socket(AF_INET,SOCK_STREAM,0))
    	   cout << "--- Receiving socket created successfully." << endl;
        else
        {
    	   cout << "Can't create receiving socket. Aborting." << endl;
    	   WSACleanup();
    	   exit(0);
        }
    
        sockaddr_in addr;
    
        addr.sin_family = AF_INET;
        addr.sin_port = htons(5656);
        addr.sin_addr.s_addr = htonl(INADDR_ANY);
    
        if (bind(imp,(LPSOCKADDR)&addr,sizeof(addr)) == SOCKET_ERROR)
        {
    	   cout << "Error binding. Aborting." << endl;
    	   WSACleanup();
    	   exit(0);
        }
    
        if (listen(imp,1) == SOCKET_ERROR)
        {
    	   cout << "Error listening. Aborting." << endl;
    	   closesocket(imp);
    	   WSACleanup();
    	   exit(0);
        }
        else  
        {
    	   SOCKET client;
    
    	   client == accept(imp,NULL,NULL);
    
    	   if (client == INVALID_SOCKET)
    	   {
    		  cout << "Can't accept connection. Aborting." << endl;
    		  closesocket(imp);
    		  WSACleanup();
    		  exit(0);
    	   }
    
    	   else 
    	   {
    		  cout << endl;
    		  char buffer[maxchars];
    
    		  while(buffer[0] != '6' || buffer[1] != '6' || buffer[2] != '6')
    		  {	 
    			 buffer[0] = 6;
    			 buffer[1] = 6;
    			 buffer[2] = 6;
    			   
    			 recv(client,buffer,sizeof(buffer),0);
    		 
    			 if (buffer[0] != '6' || buffer[1] != '6' || buffer[2] != '6')
    			 {
    				if(strlen(buffer) > 0)
    				    cout << "Message Received: " << buffer << endl;
    			 }
    			 else
    			 {
    				closesocket(imp);
    				cout << "--- Session closed." << endl;
    				WSACleanup();
    				exit(0);
    			 } 
    		  }
    	   }
    
        }
        
    }
    
    void main()
    {
        
        WSADATA w;
    
        if(WSAStartup(0x0202,&w))
        {
    	   cout << "Can't initialize WinSock. Aborting." << endl;
    	   exit(0);
        }
        else
    	   cout << "--- WinSock initialized successfully." << endl;
    
        if (w.wVersion != 0x0202)
        {
    	   cout << "Wrong version. Aborting." << endl;
    	   WSACleanup();
    	   exit(0);
        }
        else
    	   cout << "--- Version 2.2 accepted." << endl << endl;
    
    
        int o = 1;
        while (o != 1 && o != 2 && o != 3);
        {	   
        	   cout << "1 - Run Server" << endl;
    	   cout << "2 - Run Client" << endl;
    	   cout << "3 - Exit" << endl;
    	   cout << "Enter 1,2 or 3: ";
    	   cin >> o;
        }
    		   
        switch(o)
        {
    	   case 1:
    		  server();
    		  break;
    	   case 2:
    		  client();
    		  break;
    	   case 3:
    		  exit(0);
    		  break;
        };		
    }
    When I connect to the server with the client the only message thats repeated constantly in the server is this:

    Message Received: ♠♠♠⌂≈
    the best things in life are simple.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Outline the design via pseudo. You posted arbitrary code.

    Kuphryn

  3. #3
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Code:
    <client function>
    	create object of SOCKET, exp
    	determine socket descriptor, assign to exp
    	create object sockaddr_in, target
    	assign target values
    	connect
    	enter data to be sent
    	send data to server(exp)
    
    <server function>
    	create object of SOCKET, come
    	determine socket descriptor, assign to come
    	create object of sockaddr_in, addr
    	assign addr values
    	bind
    	listen
    	create object of SOCKET, client
    	assign client value returned by accept
    	recv message
    	display message
    
    <main function>
    	Initialize WinSock2.2
    	Display list: client,server,or exit
    	enter data, then run appropriate function
    the best things in life are simple.

  4. #4
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Thanks, so its an error with recv...

    Gives me a start, thanks again.
    the best things in life are simple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 11:11 AM
  2. Winsock packet problem
    By Rare177 in forum Windows Programming
    Replies: 7
    Last Post: 10-05-2004, 04:53 PM
  3. Winsock Problem
    By Noxir in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2004, 10:50 AM
  4. WinSock Problem
    By loobian in forum C++ Programming
    Replies: 1
    Last Post: 02-09-2002, 10:25 AM
  5. Small Winsock problem...
    By SyntaxBubble in forum C++ Programming
    Replies: 0
    Last Post: 02-09-2002, 09:09 AM

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