Thread: Socket programming - input from client problem

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Socket programming - input from client problem

    Hi everyone, have a question related on socket programming in C.
    I have a program that simulate a card game, the server accept 2 clients which are going to play, everything is working fine except the input from stdin for clients.
    A client have to type 1,2 or 3 to choose the card he's gonna play, and send the input to the server, i've tried with getc but it doesn't work.

    server socket, bind..
    Code:
        if((list_fd=socket(AF_INET,SOCK_STREAM,0))<0){	
            perror("socket creation error\n");
            exit(-1);
        }
    	
        memset((void*)&serv_add,0,sizeof(serv_add));		
    	
        serv_add.sin_family=AF_INET;						
        serv_add.sin_port=htons(1313);						
        serv_add.sin_addr.s_addr=htonl(INADDR_ANY);			
        int yes=1;
        if(setsockopt(list_fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int))<0){	
            perror("setsockopt\n");
            exit(-1);
        }
    	
        if(bind(list_fd,(struct sockaddr*)&serv_add,sizeof(serv_add))<0){	
            perror("bind error\n");
            exit(-1);
        }
    	
        if(listen(list_fd,BACKLOG)<0){				
            perror("listen error\n");
            exit(-1);
    		
        }
        struct sockaddr_in tmp;
        unsigned int addrlen=sizeof(tmp);
        memset((void*)&tmp,0,sizeof(tmp));
        if((conn_fd=accept(list_fd,(struct sockaddr*)NULL,NULL))<0){
    	perror("accept error\n");
    	exit(-1);
        }
        snprintf(buffer,sizeof(buffer),"%s","Wait for Player 2\n");
        if(write(conn_fd,buffer,strlen(buffer))<0){
    	perror("write error\n");
    	exit(-1);
        }
        if((conn_fd2=accept(list_fd,(struct sockaddr*)NULL,NULL))<0){
    	perror("accept error\n");
    	exit(-1);
        }
        snprintf(buffer,sizeof(buffer),"%s","Welcome\n");
        if(write(conn_fd2,buffer,strlen(buffer))<0){
    	perror("write error\n");
    	exit(-1);
        }
    Client connection
    Code:
        if( (sock_fd=socket(AF_INET,SOCK_STREAM,0))<0 ){
            perror("Socket creation error\n");
            return -1;
        }
    	
        memset((void*)&serv_add,0,sizeof(struct sockaddr_in));
        serv_add.sin_family=AF_INET;   
        serv_add.sin_port=htons(1313);   
    	
        if( inet_pton(AF_INET,indirizzo_server,&serv_add.sin_addr) <= 0){
            perror("Address creation error\n");
            return -1;
        }
    	
        if(connect(sock_fd,(struct sockaddr*) &serv_add,sizeof(serv_add))<0){
            perror("Connection error");
            return -1;
        }
    Then client and server exchange some write and read, and then i need to get the input from client (from stdin) and write it in the buffer to send the information to the server.
    Really don't know why a getc() doesn't work, anyone can help me?

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    No part of code related to the question you've asked. If the getc is not working to reading input from thestdin. Then perhaps you should use a different api. Like getchar or scanf function.

    Put really the getc should if your reading single char from the stdin. Could show us the part of your code where you read data from stdin using getc?

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    sorry, i'm in a complete mess now, can't figure out what's the problem >.<

    client read from stdin and write in buffer
    Code:
    nread=0;
    while ((c = getchar()) != '\n') {    //c char, nread int
    	buffer[nread] = c;
    	nread++;
    }
    buffer[nread+1] = '\0';
    if(write(sock_fd,buffer,strlen(buffer))<0){
    	perror("write error\n");
    	exit(-1);
    }
    server read from buffer and write in stdout
    Code:
    while((nread=read(conn_fd,buffer,80))>0){
    	buffer[nread]=0;
    	if(fputs(buffer,stdout)==EOF){
    		perror("fputs error");
    		return -1;
    	}
    }
    i've also seen that if I put a printf in the client the program actually print on stdout when the socket is closed by the server at the end of the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. server client problem
    By rudeboy in forum Networking/Device Communication
    Replies: 5
    Last Post: 05-17-2008, 12:41 AM
  4. problem collecting data from input file
    By gkoenig in forum C Programming
    Replies: 12
    Last Post: 03-30-2008, 07:40 AM
  5. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM