problem in simple client-server chat program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shreedhan
    New Member
    • Jul 2007
    • 52

    problem in simple client-server chat program

    hi i am trying to write a simple client server chat program.
    the code of server program is:

    Code:
    #define PORT 9999
    
    
    int main()
    {
    
    struct sockaddr_in server_addr,client_addr;
    int sockfd, newsockfd, clientlen,reuse=1;
    char buf[30],buf1[30];
    
    if ((sockfd=socket(AF_INET, SOCK_STREAM, 0))==-1)
    {
    perror("socket"); exit(1);
    }
    setsockopt(sockfd, SOL_PACKET, SO_REUSEADDR, &reuse, sizeof (int));
    server_addr.sin_family=AF_INET;
    server_addr.sin_port=htons(PORT);
    server_addr.sin_addr.s_addr=INADDR_ANY;
    
    if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof server_addr)==-1)
    {
    perror("bind"); exit(1);
    }
    
    if (listen(sockfd,0)==-1)
    {
    perror("listen"); exit(1);
    }
    
    clientlen=sizeof client_addr;
    if ((newsockfd=accept(sockfd,(struct sockaddr *)&client_addr, &clientlen))==-1)
    {
    perror("accept"); exit(1);
    }
    
    close(sockfd);
    [B]while(1){
    
    read(newsockfd, buf, sizeof buf);
    printf("client: %s",buf);
    
    printf("\nYou:  ");
    //fscanf(stdin,"%[^\n]",buf1);
    gets(buf1);
    write(newsockfd, buf1, sizeof (buf1));
    
    }[/B]
    
    close(newsockfd);
    
    return 0;
    }
    the code in bold is looping the reads and writes so that both server and client can continuously send message to each other one after another

    but the problem is
    after connecting to the server using another client program,
    after first iteration, the loop goes uncontrollable, that is it doesn't wait for scanf, but just goes on looping infinitely
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I believe your fscanf line is reading everything except newlines. Since the used puts a newline into the input buffer (when the press enter which they must do for the data to reach the program) this newline is left in the buffer.

    You never empty the buffer of that newline so when the loop comes round again there is dta in the input buffer, a newline, fscanf grabs everything up to the newline (nothing) and returns.

    You can't just leave data in the input buffer if messes things up. You must read everything, even the charactets you don't want.

    Comment

    • mohammadazim
      New Member
      • Aug 2007
      • 28

      #3
      Originally posted by Banfa
      I believe your fscanf line is reading everything except newlines. Since the used puts a newline into the input buffer (when the press enter which they must do for the data to reach the program) this newline is left in the buffer.

      You never empty the buffer of that newline so when the loop comes round again there is dta in the input buffer, a newline, fscanf grabs everything up to the newline (nothing) and returns.

      You can't just leave data in the input buffer if messes things up. You must read everything, even the charactets you don't want.

      This can not happen because loop has read system call. It is a blocking system call and waits untill new data is received on socket. What is your client program. looks that is sending data continously. Are you using send () in client program. That keeps sending if some thing is in buffer. Try using write there also. Your server code looks fine.

      Comment

      • mohammadazim
        New Member
        • Aug 2007
        • 28

        #4
        Originally posted by mohammadazim
        This can not happen because loop has read system call. It is a blocking system call and waits untill new data is received on socket. What is your client program. looks that is sending data continously. Are you using send () in client program. That keeps sending if some thing is in buffer. Try using write there also. Your server code looks fine.

        Oh sorry! There is one mistake in your server code which is causing this problem. This is your read () and write () system calls.
        You need socket read and write calls and not file descriptor read write. Actual synopsis of socket read and write calls is as:

        int countOfBytes read (int socketDescripto r, char * buffer, int numberOfBytesRe ad, int socketFlags)

        int countOfBytes write(int socketDescripto r, char * buffer, int numOfBytesToWri te, int socketFlags)
        you missed last argument flags. We generally don'y require it.

        call the read as read(sockFd, buff, len,0) and that should work. Also change the write ().

        Comment

        • shreedhan
          New Member
          • Jul 2007
          • 52

          #5
          Thank you both of you,

          if you didn't notice, i would remind you that i have disabled the fscanf() line using //.

          and mohmaddazim, i think the system calls for read and write are correct in my code (see: man 2 read)
          and i am using socket descriptor returned by socket() system call.

          or are you talking of recv() system call?

          thanks again

          Comment

          Working...