I am currently trying to understand how to use sockets to make a client/server program in C. I have been reading various tutorials around the Internet in hopes to try and make a small echo server that can deal with multiple clients at the same time. Whenever the client sends the server a message, the server is supposed to echo is back to the client. The code that I created is a combination of both a tutorial from a lecture at school (which explained how to create the client and server) as well as an example I found on here on stackoverflow (which showed how to make the functions to actually echo the messages). I am hoping that someone can explain to me what I am missing to make this program work correctly. Here is the client code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
char buf[80];
struct sockaddr myname;
void replyBack(FILE *fp, int sockfd) {
char sendline[1000], recvline[1000];
printf("Enter your echo: \n");
while(fgets(sendline,1000,stdin) != NULL) {
write(sockfd,sendline,sizeof(sendline));
if(read(sockfd,recvline,1000) == 0) {
printf("str_cli: server terminated prematurely");
exit(-1);
}
fputs(recvline, stdout);
}
}
main() {
int sock, adrlen, cnt;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if(sock < 0) {
printf("client socket failure%d\n", errno);
printf("client: ");
exit(1);
}
myname.sa_family = AF_UNIX;
strcpy(myname.sa_data, "/tmp/billb");
adrlen = strlen(myname.sa_data) + sizeof(myname.sa_family);
if(connect(sock, &myname, adrlen) < 0) {
printf("client connect failure %d\n", errno);
perror("client: ");
exit(1);
}
replyBack(stdin,sock);
exit(0);
}
And here is the server code:
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
struct sockaddr myname;
char buf[80];
void echo(int sockfd) {
ssize_t n;
int write_err;
char buf[1000];
char * send_start_pos;
while(1) {
bytes_in = read(sockfd, buf, 1000);
if(bytes_in < 1) {
if(errno == EINTR)
continue;
break;
}
bytes_remaining = bytes_in;
send_start_pos = buf;
write_err = 0;
while((bytes_remaining > 0) && !(write_err)) {
bytes_out = write(sockfd, send_start_pos,
bytes_remaining);
if(bytes_out < 0) {
if(errno == EINTR)
continue;
write_err = 1;
break;
}
bytes_remaining -= bytes_out;
send_start_pos += bytes_out;
}
if(write_err)
break;
}
}
main() {
int sock, new_sd, adrlen, cnt;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if(sock < 0) {
printf("server socket failure %d\n", errno);
perror("server: ");
exit(1);
}
myname.sa_family = AF_UNIX;
strcpy(myname.sa_data, "/tmp/billb");
adrlen = strlen(myname.sa_data) + sizeof(myname.sa_family);
unlink("/tmp/billb"); /*defensive programming */
if(bind(sock, &myname, adrlen) < 0) {
printf("server bind failure%d\n", errno);
perror("server: ");
exit(1);
}
if(listen(sock, 5) < 0) {
printf("server listen failure %d\n", errno);
perror("server: ");
exit(1);
}
while(1) {
if(new_sd = accept(sock, &myname, &adrlen) < 0) {
printf("server accept failure %d\n", errno);
perror("server: ");
exit(1);
}
printf("Socket address in server %d is %s, %s\n",
getpid(), myname.sa_data, myname.sa_data);
if(fork() == 0) {
close(sock);
echo(new_sd);
exit(0);
}
close(new_sd);
}
}
The problem when I run the program is that the client is accepting the message but then does not actually send it to the server, so the server can never echo it back.
I know this is probably basic stuff so I appreciate your patience and time!
This should be
sizeof myname. Theunix(7)sockets are actually defined to have asockaddr_unlike this:You’ve given it
10+4as the size. Which should almost work — it doesn’t count the terminatingNULbyte at the end of the filename — but even with the+ 1added, I’d feel better if you’re passing in exactly the size of object you have. (Be careful ofstrlen(3)on strings. Almost always you need a+ 1in any expression that involvesstrlen(3). Funny.)And here you’re sending all 1000 bytes of your buffer, regardless of contents. The user might have entered one character, or one hundred, and you’re sending all 1000 bytes of whatever is at that address.
Your
echomethod is a bit much; I’d recommend splitting the writing off into its own routine; Advanced Programming in the Unix Environment, 2nd edition (a superb book, well worth finding a copy if you expect to program Unix or Unix-like systems) has a small sweet routine I really enjoy:(See
lib/writen.cin the source from the book’s website.)I haven’t actually tried your code, so it’s very possible none of these are the problem actually preventing your software from working 🙂 if that’s the case, leave a comment, and I’ll take a closer look.