Thread: Winsock packet problem

  1. #1
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214

    Winsock packet problem

    hi im making a chat program in C/API and when i send data to the server it doesnt show as it was when it left the client

    for example i have a user name say "testuser" and i the text that will show on the server as say hello, but some times it will show hell and others it will show hello plus all types of messy stuff.
    here is how i am doing it.

    Code:
    int ConnectToHost()
    {
    	
    	struct sockaddr_in ClientSAddr;
    	struct hostent *hp;
    
    	char ServerIP[15];
    
    	int RetValue;
    	int Address;
    	int status;
    
    	WSADATA wsaClient;
    
    	RetValue = WSAStartup(0x202, &wsaClient);
    
    	if(RetValue != 0)
    	{
    		AppendWindowText(hwndEditOut , "Cannot Load Winsock");
    		AppendWindowText(hwndEditOut , "\r\n");
    	}
    
    	SendMessage(hwndEditIPC , WM_GETTEXT , (WPARAM)sizeof(ServerIP) , (LPARAM)ServerIP);
    	SendMessage(hwndEditNickC , WM_GETTEXT , (WPARAM)sizeof(NicknameC) , (LPARAM)NicknameC);
    
    	Address = inet_addr(ServerIP);
    	hp = gethostbyaddr((const char*)&Address , 4 , AF_INET);
    
    	if(hp == NULL)
    	{
    		AppendWindowText(hwndEditOut , "Cannot Resolve the IP Address");
    		AppendWindowText(hwndEditOut , "\r\n");
    
    		return 0;
    	}
    	
    	ClientSock = socket(AF_INET , SOCK_STREAM , 0);
    	memset(&ClientSAddr , sizeof(ClientSAddr) , 0);
    	ClientSAddr.sin_family = AF_INET;
    	ClientSAddr.sin_addr.s_addr = inet_addr(ServerIP);
    	ClientSAddr.sin_port = htons(1337);
    
    	status = connect(ClientSock, (struct sockaddr*) &ClientSAddr, sizeof(ClientSAddr));
    	
    	if(status != 0)
    	{
    		AppendWindowText(hwndEditOut , "Not Connected");
    		AppendWindowText(hwndEditOut , "\r\n");
    
    		return 0;
    	}
    	return 0;
    }
    
    int SendPacket(char SendData[512])
    {
    	send(ClientSock , (const char *) SendData , sizeof(SendData) , 0);
    
    	MessageBox(0 , SendData , "test" , 0);
    	return 0;
    }
    
    int HostServer(void)
    {
    	struct sockaddr_in ServerSAddr , User;
    	char RecvData[512];
    	unsigned Userlen = 0;
    	int RecvVal;
    
    	SOCKET AcceptData;
    	WSADATA wsaData;
    
    	WSAStartup(0x202, &wsaData);
    
    	ServerSock = socket(AF_INET , SOCK_STREAM , 0);
    	memset(&ServerSAddr , sizeof(ServerSAddr) , 0);
    	ServerSAddr.sin_family = AF_INET;
    	ServerSAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    	ServerSAddr.sin_port = htons(1337);
    
    	if(bind(ServerSock , (struct sockaddr*)&ServerSAddr , sizeof(ServerSAddr)) != 0)
    	{
    		AppendWindowText(hwndEditOut , "Could not bind the Socket");
    		AppendWindowText(hwndEditOut , "\r\n");
    		return 0;
    		
    	}
    
    	listen(ServerSock , 5);
    
    	while(1)
    	{
    		Userlen = sizeof(User);
    		AcceptData = accept(ServerSock , (struct sockaddr*)&User , &Userlen);
    		RecvVal = recv(AcceptData , (const char *) RecvData , sizeof(RecvData) , 0);
    
    		if(RecvData[0] == 'd')
    		{
    			char NameData[20];
    			char TextData[256];
    			char Command[2];
    			int i;
    
    			sscanf(RecvData , "%s %s %s" , Command , NameData , TextData);
    
    			for(i = 0; i<strlen(TextData); i++)
    			{
    				if(TextData[i] == '_')
    					TextData[i] = ' ';
    			}
    
    			MessageBox(0 , NameData , "test" , 0);
    			MessageBox(0 , TextData , "test" , 0);
    
    			cf.crTextColor = RGB(NameColor1,NameColor2,NameColor3);
    			AppendWindowText(hwndEditOut , NameData);
    			AppendWindowText(hwndEditOut , "> ");
    
    			cf.crTextColor = RGB(TextColor1,TextColor2,TextColor3);
    			AppendWindowText(hwndEditOut , TextData);
    			AppendWindowText(hwndEditOut , "\r\n");
    		}
    
    	}
    	return 0;
    
    }
    the way im sending the actual data is like this.

    Code:
    			if((HWND)lParam == hwndButtonSend)
    			{
    				char Data[512];
    				char NicknameC[20];
    
    				int i;
    
    				GetWindowText(hwndEditIn , EditInText , 300);
    				GetWindowText(hwndEditNickC , NicknameC ,  20);
    
    				SetWindowText(hwndEditIn , "");
    
    				for(i = 0; i<strlen(EditInText); i++)
    				{
    					if(EditInText[i] == ' ')
    						EditInText[i] = '_';
    				}
    
    				sprintf(Data , "d %s %s" , NicknameC , EditInText);
    				MessageBox(0 , Data , "test" , 0);
    				SendPacket(Data);
    			}
    the messagebox shows it is attempting to send the right data but when it gets there it is not right so im guessing there is a problem with how i am sending it.
    i would really appreciate some help on this because it has caused a really big headache, thanks

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    On a quick look.....
    Send only the message, not the whole buffer or fill the unused buffer with a known character.

    Test the return from the call to recv(). Are you getting all the message in one read? ( use a messsagebox not a breakpoint )
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    thanks, the problem was how i was sending, to fix this i sent only the text not the whole buffer, but i still have 1 problem. when i use send once i cant use it again and i need to reconnect, does any one know away around this.
    thanks

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    132
    did you design the server side of this chat software or is it someone else's code, or even not a server run by yourself but by a third party and all your trying to do is use their protocol and communicate with their server? Cause if it's your own coded server then the problem might lye in the fact that you aren't continuously reading from the socket until a disconnection of some kind is found, and thus the server is most likely disconnecting the client. If this is the case then let me know and I can post a short snippet of code for continuously reading from a socket for a server side application. But let me know what the exact case is and I will try to help you work out the problem.

    Hope this helps,
    Tyouk

  5. #5
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    this is my own code and no it doesnt shut it off, the connection stays on it just wont send data more than once

    im using tcpview to monitor the connection and it stays going, this is my project for learning winsock so some things may be scrappy.
    thanks
    Last edited by Rare177; 10-04-2004 at 03:05 PM.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    132
    I've never heard of a problem like that before, I'll check into it in a bit to see if I can find a solution for the problem. There might be one. Who knows.

  7. #7
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    well i have decided to read up on some winsock a bit more and have decided to read Network programming for microsoft windows 2nd ed
    thanks for your help though, much appreciated

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    132
    I couldn't see the problem located in your code posted above as to why you weren't able to send to a connection more than once. Sorry I couldn't help you any further, but you should check out: http://www.hal-pc.org/~johnnie2/winsock.html That tutorial was a major help when I began working with the Winsock library to send and receive data via the net with my programs. I've never had any problems with learning it from this tutorial, nor have I ever experienced all that great of a problem with any of my winsock code from using that Tutorial so go ahead and check it out. Who knows it might help you out a lot quicker then having to read a book like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. winsock problem
    By /Muad'Dib\ in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-07-2004, 01:29 AM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. WinSock Problem
    By loobian in forum C++ Programming
    Replies: 1
    Last Post: 02-09-2002, 11:25 AM
  5. Small Winsock problem...
    By SyntaxBubble in forum C++ Programming
    Replies: 0
    Last Post: 02-09-2002, 10:09 AM