Client - Server

This is a discussion on Client - Server within the Windows Programming forums, part of the Platform Specific Boards category; Hello! I use Windows 98, VC++ 6.0. I make client - server Winsock 2 application. Client is MFC Application. Server ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    15

    Client - Server

    Hello!
    I use Windows 98, VC++ 6.0.
    I make client - server Winsock 2 application.
    Client is MFC Application. Server is Win32 Application.
    I test my both programs in one computer. Same IP, different ports. I send "Hello!" from client to server. But when I display the received message on server I see some strange text. Why?
    What should I do?
    Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    One possible problem is that you are not null terminating the string. Without seeing any code, that is the best I can do.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    15
    Server's code:
    Code:
    WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	while (1)
    	{
    		//TODO
    		WSADATA w;
    		int error=WSAStartup(0x0202,&w);
    		if(error) return 0;
    		if(w.wVersion!=0x0202)
    		{
    			WSACleanup();
    			return 0;
    		}
    		SOCKET s=socket(AF_INET,SOCK_STREAM,0);
    		sockaddr_in a2;
    		a2.sin_family=AF_INET;
    		a2.sin_port=htons(55155);
    		a2.sin_addr.s_addr=htonl(INADDR_ANY);
    		if(bind(s,(LPSOCKADDR)&a2,sizeof(a2))==SOCKET_ERROR)
    		{
    			WSACleanup();
    			return 0;
    		}
    		if(listen(s,5)==SOCKET_ERROR)
    		{
    			WSACleanup();
    			return 0;
    		}
    
    		SOCKET c;
    		sockaddr_in ca;
    		int k=sizeof(struct sockaddr);
    		for(;;)
    		{
    			c=accept(s,(struct sockaddr*) &ca,&k);
    			if(c==INVALID_SOCKET)
    			{
    				WSACleanup();
    				return 0;
    			}
    			char buf[9];
    			int ret = recv(s,buf,sizeof(buf) - 1,0);
    			if (ret != SOCKET_ERROR)
    			{
    				WSACleanup();
    				return 0;
    			}
    			closesocket(s);
    			MessageBox(NULL,buf,buf,MB_OK);
    			if(!strcmp(buf,"prxyen"))
    			{
    				CRegKey k2;
    				long err;
    				DWORD n=1;
    				err=k2.Open(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
    				err=k2.SetValue(n,"ProxyEnable");
    				if(err!=ERROR_SUCCESS) return 0;
    				k2.Close();
    			}
    			else if(!strcmp(buf,"prxydis"))
    			{
    				CRegKey k2;
    				long err;
    				DWORD n=0;
    				err=k2.Open(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
    				err=k2.SetValue(n,"ProxyEnable");
    				if(err!=ERROR_SUCCESS) return 0;
    				k2.Close();
    			}
    		}
    	}
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    15
    Client's code:
    Code:
    void CClient1Dlg::OnButton1() 
    {
    	// TODO: Add your control notification handler code here
    	SOCKET s=socket(AF_INET,SOCK_STREAM,0);
        sockaddr_in a1;
        a1.sin_family=AF_INET;
        a1.sin_port=htons(55155);
        a1.sin_addr.s_addr=inet_addr("192.168.120.201");
        if(connect(s,(LPSOCKADDR) &a1,sizeof(a1))==SOCKET_ERROR)
        {
            closesocket(s);
            return;
        }
        char *cmd1="prxyen";
        int ret = send(s,cmd1,sizeof(cmd1),0);
        closesocket(s);
    	
    }
    What is here wrong?

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You need to nul terminate the string you receive over the network.
    Code:
    			int ret = recv(s,buf,sizeof(buf) - 1,0);
    			if (ret != SOCKET_ERROR)
    			{
    				WSACleanup();
    				return 0;
    			}
    
    			buf[ret] = '\0';
    
    			closesocket(s);
    Also
    Code:
        char *cmd1="prxyen";
        int ret = send(s,cmd1,sizeof(cmd1),0);
    sizeof, when used on a pointer will return 4. You want to use strlen:
    Code:
        char *cmd1="prxyen";
        int ret = send(s,cmd1,strlen(cmd1),0);

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    15
    I made corrections. I run client and server in different computers. Server receives messages but some hieroglyphs insteed of my text.
    Help me, please!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 09:54 PM
  2. c program client server
    By steve1_rm in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-24-2008, 09:33 AM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 07:04 PM
  4. Server and Client process
    By wise_ron in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-07-2006, 01:11 AM
  5. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM

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