Thread: Something weird with socket (probably stupid mistake by me though)

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    Something weird with socket (probably stupid mistake by me though)

    So I try to make a very simple mail server client on unix. Right now I create a socket at the server for listening and a socket at the client for sending messages to the server.
    What I do at this point is create a connection and send a user name from the client to the server where the server will check it through a list and if it's valid will reply with ACCEPT.

    Code:
    server
    ...
    loc_socket=socket(AF_UNIX,SOCK_STREAM,0);
    	unlink(SOCKFILE);	
    	bzero(&addr,sizeof(addr));
    	
    	addr.sa_family=AF_UNIX;
    	strcpy(addr.sa_data,SOCKFILE);
    	
    	if (bind(loc_socket,&addr,sizeof(addr))<0)	{
    		printf("Server bind failure %d\n",errno);
    		perror("Server:");
    		exit(1);
    	}
    
    	if (listen(loc_socket,LISTEN_Q)<0)	{
    		printf("Server listen failure %d\n",errno);
    		perror("Server:");
    		exit(1);
    	}
    
    	for (;;)	{	
    		if ((rem_socket=accept(loc_socket,&client_addr,&client_len))<0){
    			printf("server accept failure%d\n",errno);
    			perror("Server: ");
    		}
    		assertUserName(&rem_socket,user_list);
    		
    		close(rem_socket);
    	}
    ...
    Code:
    int assertUserName(int* socket,user* users){
    	int i, flag=0;
    	char buf[BUFFER_SIZE];
    		
    	read(*socket,buf,sizeof(buf));
    	printf("%s\n",buf);
    
    	for (i=0; i<3; i++)	{
    		if (strcmp(users[i].loginname,buf)==0)	{
    			flag=1;
    			break;
    		}
    		printf("try %d --- flag %d\n",i,flag);
    	}
    	if (flag==1)
    		write(*socket,"ACCEPT\0",sizeof("ACCEPT\0"));
    	else
    		write(*socket,"REJECT\0",sizeof("REJECT\0"));
    	return 0;	
    }
    Code:
    client
    ...
    loc_socket= socket(AF_UNIX,SOCK_STREAM,0);
    
    	addr.sa_family=AF_UNIX;
    	strcpy(addr.sa_data,SOCKFILE);
    
    	if (connect(loc_socket,&addr,sizeof(addr))<0)	{
    		printf("Client connect failure %d\n",errno);
    		perror("Client:");
    		exit(1);
    	}
    	if (signIn(&loc_socket))
    		return 0;
    ...
    Code:
    int signIn(int* socket){	
    	char user_name[LOGIN_NL];
    	char buf[BUFFER_SIZE];
    	
    	printf("Log in: ");
    	fgets(user_name,sizeof(user_name),stdin);
    	printf("%s",user_name);
    	write(*socket,user_name,sizeof(user_name);
    
    	read(*socket,buf,sizeof(buf));
    	printf("server read '%s'\n",buf);
        
    	if(strcmp(buf,"REJECT")==0){
    		printf("User Name doesn't exist\n");
            	return 1;
    	}
    	return 0;
    
    }
    My problem is in the signIn function wher I get the user name with fgets.Right afterwards I print what is on the user_name variable and no matter what i write it prints back 0Υy9 or something and so does the server. I think the sending and receiving of the message works fine since the server prints out the same (I also tried and sent a string directly and it got through fine) so why does it store rubbish on the variable. At first I thought it had to do something with the buffer but i think thats silly since it's a short program and it closes afterwards so if someone had any idea I 'd appreciate it.
    By the way I dont know if it has any relevance but I am writing it on unix and test it using two terminals on the same pc and same account...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	write(*socket,user_name,sizeof(user_name);
    Surely this doesn't compile correctly?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    yes i forgot a parenthesis when i copied it on my post but i have it correct on my hard drive

  4. #4
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Ok that's really weird i disconnected from the system 20 minutes back and now that i logged on again it works fine and prints what I write, but I dont know if it will happen again. I am wondring if it has something with SOCKFILE (btw it's a constant "sockfile") which I use both in client and server in addr.sa_data. As far as I understand you bind the server socket with a name so you work with it (something like an address) and you use the same name on the struct sockaddr addr of both client and server.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There are several things you don't do, which you must.

    - check the return result of all send() and recv() calls. Either can transmit less data than you planned for. Message fragmentation and reassembly is your problem to solve.

    - recv() will not append a \0. So all your uninitialised buffers, followed by a recv(), followed by some kind of string operation are broken.

    - calling write(), then immediately calling read() makes no account for network delays, even if you are using the loopback.

    Finally, network programming is so much easier if you have ethereal / wireshark, and some traces of known working apps to compare against.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Replies: 6
    Last Post: 09-07-2004, 11:06 PM
  3. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM
  4. Weird socket problem
    By k_w_s_t_a_s in forum Linux Programming
    Replies: 0
    Last Post: 06-04-2003, 11:41 AM
  5. Weird (stupid?) Problem: error C2448: '<Unknown>'
    By Dual in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2001, 03:28 AM