Thread: TCP Client problem while trying to multiplex stdin and scoket

  1. #1
    Registered User
    Join Date
    Nov 2011
    Edmonton, Alberta, Canada

    Post TCP Client problem while trying to multiplex stdin and scoket

    I am writing this TCPClient which will multiplex between the stdin and socket to get mesages from the server. The server is implemented to deal with multiple clients. But I have written the following code. But when I run the code, it is only going in the infinite loop and not even printing the statements which I have mentioned below. And obviously, it is not working as I want to work (send messages and receive messages at the same time)

    /* tcpclient.c */
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    int main()
        int sock, bytes_recieved;  
        char send_data[1024],recv_data[1024];
        struct hostent *host;
        struct sockaddr_in server_addr;  
        fd_set read_fds;
        char ch;
        int count = 0;
        int i;
        host = gethostbyname("");
        //create the socket
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        //set server info
        server_addr.sin_family = AF_INET;     
        server_addr.sin_port = htons(5000);   
        server_addr.sin_addr = *((struct in_addr *)host->h_addr);
        //connect to the server
        if (connect(sock, (struct sockaddr *)&server_addr,
            sizeof(struct sockaddr)) == -1) 
        printf("stage 1"); //the program is not even printing this but it is going to the infinite loop below!!!!
            //initialize the readset and add stdin and the socket to it
            FD_SET(0, &read_fds);
            FD_SET(sock, &read_fds);
            printf("Send something: %s",send_data); //this is also is not getting printed
            if(select(sock+1, &read_fds, NULL, NULL, NULL) == -1)
                perror("Server-select() error lol!");
            if(FD_ISSET(0, &read_fds)) //something has happened in the stdin
                printf("key pressed"); //when I press a key, it should get here, but its not printing this also!!
                ch = getc(0);
                if (ch=='\n')  //user typed enter, time to send the content of the buffer and clear it
                    send(sock, send_data, sizeof(send_data), 0);
                    count = 0;
                else //append the pressed character to the buffer
                    printf("here");  //it is also not printed!!!
            else if(FD_ISSET(sock, &read_fds)) //server has sent something
                bytes_recieved = recv(i, recv_data, sizeof(recv_data), 0);
        }//end of infinite while
        return 0;
    I see in the following thread that there has been a discussion on this. But I am still having problem
    Multiplexing read socket and stdin

    Thanks for your help;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well you don't seem to be printing any newlines, or calling fflush(stdout)
    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
    Nov 2011
    Edmonton, Alberta, Canada
    Thank you for your answer. Sorry I didn't get you when you said I am not printing newline. When is it needed? I am just trying to receive messages from the server and also send the message to the server. And the message is only sent when the client presses "enter" (as I getc it and it appears as \n then).

    When would it be necessary to print newline and also where should I use fflush?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    stdout is line buffered. That means that the data normally doesn't appear on the screen until you print a newline. That is, you can call printf or putchar, but the data wont show up on the screen right away unless there's a newline at the end. One way to get it there sooner without printing a newline, is to use fflush(stdout). So the answer to when you should print a new line is "whenever you want a new line in your output, or whenever you want to flush the stdout buffer and don't mind a newline in your output". The answer to when you should use fflush(stdout) is "whenever you want to flush the stdout buffer". In the program you posted, this would probably be right after any comment that says "this isn't getting printed".
    Last edited by anduril462; 11-02-2011 at 06:26 PM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Probably not part of your current problem, but you use if...else to check what select() did, but select potentially will set more than one fd as having pending events.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdin size problem
    By Kempelen in forum C Programming
    Replies: 5
    Last Post: 04-13-2009, 10:14 AM
  2. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  3. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  4. new problem after not using fflush(stdin)
    By megablue in forum C Programming
    Replies: 2
    Last Post: 07-04-2003, 08:54 AM
  5. ascii 10 in the stdin after fflush(stdin)??
    By Kev2 in forum A Brief History of
    Replies: 3
    Last Post: 06-03-2002, 03:53 PM