Reading and writing I / O messages from the client to the server program

So, I play with the idea of ​​ports and communication between client and server.

I have a server.c program that can open a port, open a listen descriptor, and when a connection is received, the child’s plug processes the connection with the connecting client. I have a client.c program that takes 5 command line arguments. Basically, the first three arguments are practical strings to send to the server, and the fourth is the host name and the fifth is the port number.

So far, the connection of these two files has worked fine, however, when the client tries to write 3 different lines (argv [1], argv [2] and argv [3]) on server.c, server.c seems to be able to read only the first , then it seems to be stuck and will not continue reading additional messages, even if the client finishes writing all the lines to the communication file descriptor. I’ve been stuck for more than 4 hours trying to figure out what should have been a simple practice to better explore servers and clients. I don’t want to get lost anymore, then I already hope so that someone can give me any advice on how to deal with this problem or what I am doing wrong.

Client.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "uici.h"
#include "func.h"


int main(int argc, char *argv[]){
  int fd;
  u_port_t portnum;

  if(argc != 6){
    fprintf(stderr, "Usage: %s string1 string2 string3 host port\n",argv[0]);
    return -1;
  }
  portnum = (u_port_t)atoi(argv[5]);
  if((fd = u_connect(portnum, argv[4])) == -1){
    perror("Failled to establish connection");
    return 1;
  }
  fprintf(stderr, "[%ld]:connection made to %s\n", (long)getpid(), argv[4]);
  if((write(fd, argv[3], strlen(argv[3])+1)) == -1){
    fprintf(stderr, "Failed to write %s to fd", argv[3]);
    r_close(fd);
    return 0;
  }
  if((write(fd, argv[1], strlen(argv[1])+1)) == -1){
    fprintf(stderr, "Failed to write %s to fd", argv[1]);
    r_close(fd);
    return 0;
  }
  if((write(fd, argv[2], strlen(argv[2])+1)) == -1){
    fprintf(stderr, "Failed to write %s to fd", argv[2]);
    close(fd);
    return 0;
  }
  fprintf(stderr, "Everything has been written\n");
  return 0;
}

Server.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "func.h"
#include "uici.h"

int main(int argc, char *argv[])
{
   u_port_t portnumber;
   int listenfd;
   int fd;
   char client[MAX_CANON];
   int bytes_copied;
   pid_t child;

   if (argc != 2) {
      fprintf(stderr, "Usage: %s port\n", argv[0]);
      return 1;
   }

   portnumber = (u_port_t) atoi(argv[1]);
   if ((listenfd = u_open(portnumber)) < 0) {
      perror("Listen endpoint creation failed");
      return 1;
   }

   fprintf(stderr, "[%ld]: Waiting for the first connection on port %d\n",
                    (long)getpid(), (int)portnumber);
   for ( ; ; ) {
      if ((fd = u_accept(listenfd, client, MAX_CANON)) != -1) {
         fprintf(stderr, "[%ld]: A connection has been received from %s\n",
                 (long) getpid(), client);
         if ((child = fork()) == -1)
            perror("Could not fork a child");

         if (child == 0) {                            /* child code */
            r_close(listenfd);
            int MAXSZ = 1024;
            char str3[MAXSZ];
            char str1[MAXSZ];
            char str2[MAXSZ];
            int bytesread = 0;
            fprintf(stderr, "Beginning the reads\n");
            read(fd,str3, MAXSZ);
            fprintf(stderr, "Finished 1st read\n");
            read(fd,str1, MAXSZ);
            fprintf(stderr, "Finished 2nd read\n");
            read(fd,str2, MAXSZ);
            fprintf(stderr, "str3: %s\n",str3);
            fprintf(stderr, "str1 = %s\n",str1);
            fprintf(stderr, "str2 = %s\n",str2);
            close(fd);
            return 0;
         } else {                                    /* parent code */
            close(fd);
            while (waitpid(-1, NULL, WNOHANG) > 0) ;  /* clean up zombies */
         }
      }
      else
         perror("Accept failed");
   }
}
+4
source share
2 answers

. , read write / , , , .

, fd, . - , , , , , 1 '\0', .

MAXSZ, , , , , , .

0

r_close(listenfd);

, argv[1] , , , u_accept(listenfd, client, MAX_CANON) - listenfd .

0

All Articles