Earlier I got some great advice about Client/server sockets in c. I have modified the code slightly and it works fine when I have the server running in one terminal and the clients running in other windows. Now I would like to make a TUI for the program, but I am running into a problem. Since the server is constantly listening for a client it is in an infinite loop; once the server is established nothing else can go on in the terminal. What I thought I should do is use fork() so that the server can be running in the background, leaving the TUI free to create clients. Here is my code so far (again, I cannot take much credit for the client/server code):
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];
ssize_t readLine(int sockd, char *vptr, size_t maxlen) {
ssize_t n, rc;
char c, *buffer;
buffer = vptr;
for(n = 1; n < maxlen; n++) {
if((rc = read(sockd, &c, 1)) == 1) {
*buffer++ = c;
if(c== '\n')
break;
}
else if(rc == 0){
if(n == 1)
return 0;
else
break;
}
else {
if(errno == EINTR)
continue;
return -1;
}
}
*buffer = 0;
return n;
}
ssize_t modifyBuf(int sockd, char *vptr, size_t n) {
int i, j;
char temp;
for(i = 0, j = n-1; i < j; i++, j--) {
temp = vptr[i];
vptr[i] = vptr[j];
vptr[j] = temp;
}
for(i = 0; i < n; i++)
vptr[i] = toupper(vptr[i]);
return writeLine(sockd,vptr,n);
}
ssize_t writeLine(int sockd, const void *vptr, size_t n) {
size_t nleft;
size_t nwritten;
const char *buffer;
buffer = vptr;
nleft = n;
while(nleft > 0) {
if((nwritten = write(sockd,buffer,nleft)) <= 0){
if(errno == EINTR)
nwritten = 0;
else
return -1;
}
nleft -= nwritten;
buffer += nwritten;
}
return n;
}
makeServer() {
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\n", getpid());
if(fork() == 0) {
close(sock);
readLine(new_sd,buf,999);
modifyBuf(new_sd,buf,strlen(buf));
exit(0);
}
close(new_sd);
}
}
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);
}
}
makeClient() {
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);
}
TUI
#include <stdio.h>
#include </server.c>
#include </client.c>
void displayWelcomeMessage(){
....
}
void showChoices(){
printf("\nTo make a new client, enter 1.\n");
printf("To close server, enter 2.\n");
printf(">>> ");
int choice;
scanf("%d",&choice);
if(choice == 1){
makeClient();
showChoices();
} else if(choice == 2){
printf("Goodbye.");
exit(0);
} else
printf("Invalid choice.\n");
showChoices();
}
void main() {
displayWelcomeMessage();
printf("Server is now listening for clients.\n");
if(fork() == 0)
makeServer();
if(fork() > 0)
showChoices();
}
The TUI should create the server when it is executed and then show a short menu. The user can chose to create a new client or exit the program. If the user creates a client then the user should be prompted for a message to send to the server; once the message is sent back the menu should be shown again. If the user elects to exit the program then the program will terminate and the socket should close. If the user makes an invalid choice, then the menu should display again.
What is happening is the server is created and I can see the menu, but if I select to create a new client the program terminates or terminates after I input any text + enter (so the message is not getting sent to the server). Like I said, the client/server works without the TUI so I believe the problem is in my usage of fork(), but I’m not sure what other way there is to solve this problem.
Phew, that was long. Thank you for your time!
You seem to be calling fork() twice – surely you only want to call it once? I think your code should be:
Also, you may want to investigate the use of wait(), which will allow you to wait for a child process to complete.