Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4599640
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:34:12+00:00 2026-05-21T23:34:12+00:00

Earlier I got some great advice about Client/server sockets in c . I have

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:34:13+00:00Added an answer on May 21, 2026 at 11:34 pm

    You seem to be calling fork() twice – surely you only want to call it once? I think your code should be:

    if(fork() == 0)
        makeServer();
    else 
       showChoices();
    

    Also, you may want to investigate the use of wait(), which will allow you to wait for a child process to complete.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I posted earlier about how to do this, and got some great replies, and
I asked a question earlier which elicited some great responses. Here's the earlier question
I have tried to ask a variant of this question before. I got some
I've got this code (Java, GAE): // Much earlier: playerKey = KeyFactory.keyToString(somePlayer.key); // Then,
I asked a question earlier , and got an excellent response, but, being a
Earlier this week I ask a question about filtering out duplicate values in sequence
Earlier versions of the Notes client would automatically turn a sent URL into a
Earlier I asked a question about why I see so many examples use the
Earlier today I asked a question about environ , and one of the more
I was thinking earlier today about an idea for a small game and stumbled

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.