Thread: client server programming strange behaviour

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    client server programming strange behaviour

    Hi All,
    I have written a client server program which works in a strange way, it will be helpful for me to understand if somebody give information about the error in the code.
    When the connection is established frm many clients to the server, the server behaves strangely producing the ip address of the FIRST CLIENT alone as 127.0.0.0 and also before i send any message, a blank message is sent from the FIRST client to server and i receive acknowledgement from server for that blank messages and after this what ever message i send from the first client or any client is received at the server correctly and also i receive the ip address of all other clients correctly from the server Except the first client as i mentioned previously.
    I can do my coding in some other way to avoid this but i need to know y this particular code which is intented to work correctly is behaving strangely.

    Question1: Y is the server showing the ip address of first client as 127.0.0.0?
    Question2: Y is the blank message sent to the server from first client?

    Code:
    Server Program:
    
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<stddef.h>
    #include<arpa/inet.h>
    /* things to be noted 1. File descriptor 2. Network byte order 3.What are the different family exits */
    /* any problem at any part of the code this will display the error message */
    void error(char *message)
    {
            perror(message);
            exit(1);
    }
    
    void main()
    {
            int sockfd,newsockfd,port_no,n,client_len;
            char buffer[255];
            struct sockaddr_in serv_addr,client_addr;
            pid_t PID;
            sockfd = socket(AF_INET, SOCK_STREAM, 0);  /*socket(domain,type,protocol) and if it is possible to get protocol details from 1st and 2nd use 0*/
    /* socket function returns an file descriptor no --> which means its a new file created which is accessible only by the kernel memory*/ 
           if(sockfd<0)
            {
                    error("Error in opening of socket");
            }
            printf("Enter the portno");
            scanf("%d",&port_no);
    /*definition of four fields which are present in sockaddr_in  structure*/
            serv_addr.sin_family = AF_INET;          /*this information we specify that it belongs to tcp or udp*/
            serv_addr.sin_port = htons(port_no);     /*htons is used because we may need to change it to network byte order which is always big endian*/
            serv_addr.sin_addr.s_addr = INADDR_ANY;  /*this means that server will serve to the specific port which is mentioned regardless of what its ip address is*/
    /*binding the socket address with the socket field descriptor which we have already created*/
             if(bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) <0)
             {
                     error("Error in binding");
             }
               
              /* added newly */
         
             while(1)
             {
                listen(sockfd,5);
             newsockfd = accept(sockfd, (struct sockaddr *) &client_addr,&client_len); /*this will actually receive the client address and the length of it*/
             printf("The client ip address is:"); 
                printf("%s\n", inet_ntoa(client_addr.sin_addr));
             PID=fork();
             if(!PID)
             { 
             printf("Forking the new child");        
              for(;;)
              {
             bzero(buffer,256);   
               fflush(buffer);
              n = read(newsockfd,buffer,255);
              if (n < 0)
              error("ERROR reading from socket");
              printf("Here is the message: %s\n",buffer);
              n = write(newsockfd,"I got your message\n",18);
              if (n < 0) error("ERROR writing to socket");
               bzero(buffer,256);
              }
             }
            }
    }       
    
    Client Program:
    
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<string.h>
    #include<stddef.h>
    void error(char *msg)
    {
      perror(msg);
      exit(1);
    }
    
    
    void main()
    {
            int sockfd, port_no, n,option;
            char ip_adr[15]="10.121.9.240";
            struct sockaddr_in serv_addr;
            char buffer[255];
            printf("Enter the portno"); 
            scanf("%d",&port_no); 
            sockfd = socket(AF_INET, SOCK_STREAM, 0); 
             if(sockfd<0)
            {
                    error("Error in opening of socket");
            }
          /*  gets(ip_adr);*/
            serv_addr.sin_addr.s_addr =inet_addr(ip_adr);           /* the obtained ip address which is a dotted string is converted to unsigned long.*/
            serv_addr.sin_family = AF_INET;          /* this information we specify that it belongs to tcp or udp*/
            serv_addr.sin_port = htons(port_no);      /*htons is used because we may need to change it to network byte order which is always big endian*/
            /* We need not have a binding in this because we need not have to include a specified port*/
            if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)        /* same as accept here in the client we establish a connection */
            error("ERROR connecting");
            else
                   printf("Connected"); 
            for(;;)
            {
           printf("\nPlease enter the message which u need to send:\n"); 
             bzero(buffer,256);                                          
            fgets(buffer,255,stdin);                                            
            n = write(sockfd,buffer,strlen(buffer));                     
            if (n < 0)
            error("ERROR writing to socket");
            bzero(buffer,strlen(buffer));
            n = read(sockfd,buffer,255);
            if (n < 0)
            error("ERROR reading from socket");
            printf("%s",buffer);
          
          }
    }

    If anybody could reply for my questions soon i could proceed further with this....




    Thanks in Advance.
    regards,
    Sudharsanam.
    Last edited by nsudharrao90; 04-07-2011 at 01:54 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Quote Originally Posted by accept man page
    The argument addr is a pointer to a sockaddr structure. This structure is filled in with the address of the peer socket, as known to the communications layer. The exact format of the address returned addr is determined by the socket's address family (see socket(2) and the respective protocol man pages). The addrlen argument is a value-result argument: it should initially contain the size of the structure pointed to by addr; on return it will contain the actual length (in bytes) of the address returned. When addr is NULL nothing is filled in.
    This, you do not do.

    > fflush(buffer);
    Huh?
    Did your compiler even complain about this, or do you just ignore warnings and see what happens?
    buffer is a char ARRAY, not a FILE*

    You should probably ease up on all the bzero() calls as well.
    If you're doing the right thing with all the return results, then you shouldn't need any of them.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    ya i have removed that fflush(buffer) even then it shows the same thing...
    Could you please clearly explain me why the server printing the ip address of first client as 127.0.0.0 and also a blank message?
    Last edited by nsudharrao90; 04-07-2011 at 06:07 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Have you read the manual pages for ALL your functions?

    Have you added result / error checking for ALL your functions?

    Is your code compiling without warnings?

    Post your latest code.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    S i have read the manual page for all the fucntion ....
    Code:
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<string.h>
    #include<stddef.h>
    void error(char *msg)
    {
      perror(msg);
      exit(1);
    }
    
    
    
    void main()
    {
            int sockfd, port_no, n,option;
            char ip_adr[15]="10.121.9.240";
            struct sockaddr_in serv_addr;
            char buffer[255];
            printf("Enter the portno"); 
            scanf("%d",&port_no); 
            sockfd = socket(AF_INET, SOCK_STREAM, 0); 
             if(sockfd<0)
            {
                    error("Error in opening of socket");
            }
          /*  gets(ip_adr);*/
            serv_addr.sin_addr.s_addr =inet_addr(ip_adr);           /* the obtained ip address which is a dotted string is converted to unsigned long.*/
            serv_addr.sin_family = AF_INET;          /* this information we specify that it belongs to tcp or udp*/
            serv_addr.sin_port = htons(port_no);      /*htons is used because we may need to change it to network byte order which is always big endian*/
            /* We need not have a binding in this because we need not have to include a specified port*/
            if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)        /* same as accept here in the client we establish a connection */
            error("ERROR connecting");
            else
                   printf("Connected"); 
            for(;;)
            {
           printf("\nPlease enter the message which u need to send:\n"); 
             bzero(buffer,256);                                          
            fgets(buffer,255,stdin);                                            
            n = write(sockfd,buffer,strlen(buffer));                     
            if (n < 0)
            error("ERROR writing to socket");
            bzero(buffer,strlen(buffer));
            n = read(sockfd,buffer,255);
            if (n < 0)
            error("ERROR reading from socket");
            printf("%s",buffer);
          
          }
    }  
    
    Server Side 
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<stddef.h>
    #include<arpa/inet.h>
    /* things to be noted 1. File descriptor 2. Network byte order 3.What are the different family exits */
    /* any problem at any part of the code this will display the error message */
    void error(char *message)
    {
            perror(message);
            exit(1);
    }
    
    void main()
    {
            int sockfd,newsockfd,port_no,n,client_len;
            char buffer[255];
            struct sockaddr_in serv_addr,client_addr;
            pid_t PID;
            sockfd = socket(AF_INET, SOCK_STREAM, 0);  /*socket(domain,type,protocol) and if it is possible to get protocol details from 1st and 2nd use 0*/
    /* socket function returns an file descriptor no --> which means its a new file created which is accessible only by the kernel memory*/ 
           if(sockfd<0)
            {
                    error("Error in opening of socket");
            }
            printf("Enter the portno");
            scanf("%d",&port_no);
    /*definition of four fields which are present in sockaddr_in  structure*/
            serv_addr.sin_family = AF_INET;          /*this information we specify that it belongs to tcp or udp*/
            serv_addr.sin_port = htons(port_no);     /*htons is used because we may need to change it to network byte order which is always big endian*/
            serv_addr.sin_addr.s_addr = INADDR_ANY;  /*this means that server will serve to the specific port which is mentioned regardless of what its ip address is*/
    /*binding the socket address with the socket field descriptor which we have already created*/
             if(bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) <0)
             {
                     error("Error in binding");
             }
               
              /* added newly */
         
             while(1)
             {
                listen(sockfd,5);
             newsockfd = accept(sockfd, (struct sockaddr *) &client_addr,&client_len); /*this will actually receive the client address and the length of it*/
             printf("The client ip address is:"); 
                printf("%s\n", inet_ntoa(client_addr.sin_addr));
             PID=fork();
             if(!PID)
             { 
             printf("Forking the new child");        
              for(;;)
              {
             bzero(buffer,256);   
             n = read(newsockfd,buffer,255);
              if (n < 0)
              error("ERROR reading from socket");
              printf("Here is the message: %s\n",buffer);
              n = write(newsockfd,"I got your message\n",18);
              if (n < 0) error("ERROR writing to socket");
               bzero(buffer,256);
              }
             }
            }
    }
    The only warning it shows is
    server_multiclient.c: In function `main':
    server_multiclient.c:16: warning: return type of 'main' is not `int'
    Which i think is acceptable....

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    And yet you're STILL ignoring the first point I made in my first reply.

    > Which i think is acceptable....
    Since the fix is very easy, I conclude you're just unwilling to learn.

    Oh, and for what it's worth (though I doubt you'll pay any attention),
    SourceForge.net: Indentation - cpwiki
    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.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    There is no point in telling i am unwilling to learn....If i am so, i could not have responded for your first message...
    Any ways thanks for giving the idea.
    --
    Sudharsanam.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Should should learn to indent your code correctly.
    You should always use braces in if/else code blocks.
    You logic is so flawed/confusing in the if/else blocks, that I can not follow it.
    I have no idea if you know what is/is not inside the if/else blocks because of your lack of proper indenting.

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket ( server client Get pid )
    By Syd in forum C Programming
    Replies: 0
    Last Post: 02-15-2011, 08:00 AM
  2. Server sends unknown number of messages, client doesn't know when he stops
    By Phoenix_Rebirth in forum Networking/Device Communication
    Replies: 0
    Last Post: 11-21-2010, 01:48 PM
  3. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  4. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  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

Tags for this Thread