0

Basically, I am doing a tcp project in c and I made it so multiple clients can connect to my server by using fork. In my server I have a linked list structure that keeps data that clients are sending. However, when I am sending data from the client to the linked list located in the server, the server creates a new linked list for that client . Instead of adding all client data to the same linked list. I know for certain its not a problem with the linked list. Fork() duplicates the process, so after calling fork there are actually 2 instances of my program running. So yeah I realized the clients aren't communicating with the same server? How should I fix this problem? Do I get rid of fork? Please conceptually explain to me what I must do. I'm not posting my actual code because it is sensitive . But here is basically what I am doing:

server:

#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

void dostuff(int); /* function prototype */

void error(char *msg)
{
   perror(msg);
   exit(1);
}

int main(int argc, char *argv[])
{
  int sockfd, newsockfd, portno, clilen, pid;
  struct sockaddr_in serv_addr, cli_addr;

  if (argc < 2) {
     fprintf(stderr,"ERROR, no port provided\n");
     exit(1);
 }
 sockfd = socket(AF_INET, SOCK_STREAM, 0);
 if (sockfd < 0) 
    error("ERROR opening socket");
 bzero((char *) &serv_addr, sizeof(serv_addr));
 portno = atoi(argv[1]);
 serv_addr.sin_family = AF_INET;
 serv_addr.sin_addr.s_addr = INADDR_ANY;
 serv_addr.sin_port = htons(portno);
 if (bind(sockfd, (struct sockaddr *) &serv_addr,
          sizeof(serv_addr)) < 0) 
          error("ERROR on binding");
 listen(sockfd,5);
 clilen = sizeof(cli_addr);
 while (1) {
     newsockfd = accept(sockfd, 
           (struct sockaddr *) &cli_addr, &clilen);
     if (newsockfd < 0) 
         error("ERROR on accept");
     pid = fork();
     if (pid < 0)
         error("ERROR on fork");
     if (pid == 0)  {
         close(sockfd);
         dostuff(newsockfd);
         exit(0);
     }
     else close(newsockfd);
 } /* end of while */
 return 0; /* we never get here */
}


 void dostuff (int sock)
{
  int n;
  char buffer[256];

bzero(buffer,256);
n = read(sock,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(sock,"I got your message",18);
if (n < 0) error("ERROR writing to socket");

client :

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

void error(char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;

    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");
    printf("Please enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);
    return 0;
}
  • Please use an autoformatter/autoindenter, it makes your code much more readable and avoids a few common mistakes. – Ulrich Eckhardt Mar 04 '18 at 15:34
  • Why do you need different processes? Normally, you do that to isolate different memory spaces, which is exactly what you don't want here. – Ulrich Eckhardt Mar 04 '18 at 15:37
  • Possible duplicate of [How to share memory between process fork()?](https://stackoverflow.com/questions/13274786/how-to-share-memory-between-process-fork) – Ulrich Eckhardt Mar 04 '18 at 15:38

2 Answers2

1

Because fork() creates a new process, you will need to use shared memory (shm_open(), shm_unlink(), mmap(),...). You can find based on this other question's answer.

Simon Berthiaume
  • 643
  • 4
  • 11
0

You can get rid of fork and instead

  • a thread for each connection, which means you can share state between clients in your program. This will however require you to add synchronization (mutexes, etc.) around all shared data.
  • use a single thread in asynchronous/nonblocking mode (select/epoll/etc.) to serve all clients.
Matthias247
  • 9,836
  • 1
  • 20
  • 29