Thread: receiving problems in network chat client

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    84

    receiving problems in network chat client

    i am coding a network chat client, an di am having trouble with the receive part.

    i'm not sure how to get whats received and print it out onto the screen, i think i have to get whats received and place it in the receive buffer then print it to the screen and clare the buffer. but i am not sure how to do this

    Code:
    	for (;;) 
    	{
    		gets(sendBuffer);								//takes input from keyboard
    		
    		if (strcmp(sendBuffer, "quit") == 0)			//if quit was typed then end client
    		{				
    			break;
    		}
    
    		unsigned long messagelength = strlen(sendBuffer);		//get size of message
    
    		messagelength = htonl(messagelength);					//fix byte ordering
    
    		if ((nbytes = send(mysocket, (char*)&messagelength, sizeof(messagelength), 0)) == SOCKET_ERROR) 
    		{
    			cout << "Send Failed!" << endl;						//send the message size
    		}
    
    		messagelength = ntohl(messagelength);					//refix byte ordering
    
    		if ((nbytes = send(mysocket, sendBuffer, messagelength, 0)) == SOCKET_ERROR) 
    		{
    			cout << "Send Failed!" << endl;						//send actual message
    		}
    
    		
    		gets(receiveBuffer);
    	
    		unsigned long messagesize = strlen(receiveBuffer);
    
    		messagesize = htonl(messagesize);
    
    		if ((nbytes = recv(mysocket, (char*)&messagesize, sizeof(messagesize), 0)) == SOCKET_ERROR) 
    		{
    			cout << "Receive Failed!" << endl;						
    		}
    
    		messagesize = ntohl(messagesize);
    
    		if ((nbytes = recv(mysocket, receiveBuffer, messagesize, 0)) == SOCKET_ERROR) 
    		{
    			cout << "Receive Failed!" << endl;						
    		}
    	}
    please give me an idea

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    84
    ok got it to kinda work now, but i can't get the buffer to empty after it prints out the message so i end up with a load of crap printed out

    Code:
    	for (;;) 
    	{
    		gets(sendBuffer);								//takes input from keyboard
    		
    		if (strcmp(sendBuffer, "quit") == 0)			//if quit was typed then end client
    		{				
    			break;
    		}
    
    		unsigned long messagelength = strlen(sendBuffer);		//get size of message
    
    		messagelength = htonl(messagelength);					//fix byte ordering
    
    		if ((nbytes = send(mysocket, (char*)&messagelength, sizeof(messagelength), 0)) == SOCKET_ERROR) 
    		{
    			cout << "Send Failed!" << endl;						//send the message size
    		}
    
    		messagelength = ntohl(messagelength);					//refix byte ordering
    
    		if ((nbytes = send(mysocket, sendBuffer, messagelength, 0)) == SOCKET_ERROR) 
    		{
    			cout << "Send Failed!" << endl;						//send actual message
    		}
    
    		recv(mysocket,receiveBuffer,messagelength,0);
    		cout << " " << receiveBuffer << endl;
    		memset( (char *) &receiveBuffer, 0,sizeof(sendBuffer));
    my send and receive loop as it is atm

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is to allocate a temporary buffer and copy only the data received. Output the temporary buffer and then deallocate. messagelength is the size.

    Kuphryn

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    84
    i have figured the problem is that i don't get the full message printed out from the buffer, not sure if its reading too fast or what

    any ideas at all?

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    I don't think you need the byte ordering calls - you could remove all of those.

    One problem is that you're treating receiveBuffer as null-terminated string, which it isn't. The send call sends strlen(input) bytes, i.e. it doesn't send the null terminator. You'll need to manually terminate your received string.

    Also, your code isn't reliable. When you call recv you might not receive the number of bytes you specify as the buffer size - recv is allowed to return when there's anything from a single byte available to read. Read up on TCP/IP.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    84
    can u reccomend any good places to go for for articles or tutorials on it at all?

    edit - just found out my receive function dosen't work until i send either, so looks like i gotta re-write em
    Last edited by chris285; 01-11-2005 at 07:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. network problems
    By lucy in forum Tech Board
    Replies: 6
    Last Post: 01-01-2003, 03:33 PM
  2. More Problems with OpenGL and the Client Area
    By Niytaan in forum Windows Programming
    Replies: 2
    Last Post: 11-06-2002, 03:24 PM
  3. Problems Drawing OpenGL to the Client Area
    By Niytaan in forum Windows Programming
    Replies: 3
    Last Post: 10-27-2002, 07:15 PM
  4. Network Problems
    By MethodMan in forum Tech Board
    Replies: 12
    Last Post: 09-13-2002, 04:09 PM
  5. Socket Problems
    By (TNT) in forum Windows Programming
    Replies: 4
    Last Post: 08-18-2001, 06:59 AM