Thread: Server recv data problem! Please help!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    30

    Unhappy Server recv data problem! Please help!

    Hello All!
    I have a problem and I realy need help! I want to make my simple server recognize commands! The problem is that when I recive data from client I get garbage at the end of the data. For example I connect with telnet to my server and send message:
    This is the first message
    Server displays them like this:
    This is the first message öÿ¿ ¨õÿ¿) @ @øÈõÿ¿
    I simply store all the recived data into array buffer and this is the result of:
    Code:
    cout << buffer << endl;
    I know that recv function returns size of the recived data, so I can make just a loop like this, to filter thous characters out:
    Code:
    for(int q=0; q<recv_data_size; q++){
        cout << buffer[q];
    }
    This will display me a data without thous garbage characters, but the problem for server to detect command!

    I have my buffer 100 bytes long, so I clear thous garbage characters like this:
    Code:
    for(int c=recv_data_size-2; c<array_size; c++){
        buffer[c] = '\0';
    }
    I used recv_data_size-2 so that the code allso would clear \n and \r characters!

    This will clean the garbage out and for example if I will send command from client stop, then my buffer will look like this:
    [s],[t],[o].[p],[\0],[\0]....
    The problem is that this code does't work:
    Code:
    if(buffer == "stop"){
       cout << "Command executed!\n";
    }
    Can you please show me how to make my server recognize commands?
    Here is the source code. I'm on Linux:

    Code:
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <iostream>
    
    #define port 1000
    #define conn 10
    #define buffsize 100
    
    
    using namespace std;
    
    int main(){
    
     	int sock_fd, fd;
     	struct sockaddr_in server;
     	struct sockaddr_in client;
    	
     	socklen_t sin_size;
    
    	char buffer[buffsize];
    	
    	server.sin_family = AF_INET;
            server.sin_port = htons(port);
    	server.sin_addr.s_addr=INADDR_ANY;
    	
    	if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
     		perror("socket();");
     		exit(1);
    	}
    	
    	
    	if(bind(sock_fd,(struct sockaddr *)&server,sizeof(struct sockaddr)) == -1){
    		perror("bind();");
    		exit(1);
    	}
    	
    	
    	if(listen(sock_fd, conn) == -1){
    		perror("listen();");
    		exit(1);
    	}
    	
    	sin_size = sizeof(struct sockaddr_in);
    	
    	while(true){
    	
    		fd = accept(sock_fd,(struct sockaddr *)&client,&sin_size);
    		
    		while(true){
    		    int data_lenght =  recv(fd, buffer, sizeof(buffer), 0);
    		    
    		    for(int q=data_lenght-2; q<buffsize; q++){
    		           buffer[q] = '\0';
    		    }
    		    
    		    if(buffer == "command"){
    		        cout << "Command executed successfully!\n";
    		    }
    		    
    		}		
    	}
    	
    return 0;
    }
    Please help me!!! Thank you!
    Last edited by hyaku_; 01-07-2005 at 04:57 PM. Reason: Syntax error in code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recv data with sockets
    By r063r in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2008, 09:42 AM
  2. Sending data to a server
    By Hakins90 in forum Networking/Device Communication
    Replies: 6
    Last Post: 04-16-2008, 07:54 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  4. recv() problem:(
    By piotrek_no_1 in forum Networking/Device Communication
    Replies: 5
    Last Post: 05-12-2005, 11:52 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM