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?