C 为什么赢了';我的服务器实现不能同时正常运行吗?

C 为什么赢了';我的服务器实现不能同时正常运行吗?,c,tcp,concurrency,network-programming,logic,C,Tcp,Concurrency,Network Programming,Logic,我正在尝试创建一个客户机/服务器系统,该系统可以使用unix系统调用fork处理多个并发连接 客户端输入电影标题,服务器将检查电影是否在那里。如果有,它会告诉客户排名、姓名和方框记录 看看我的分叉实现,客户机请求用户输入,但是程序只是简单地通过了它 输出示例: connection made with client 127.0.0.1 PID IS 27270 --> all messages read - connection being closed CLIENT: Please

我正在尝试创建一个客户机/服务器系统,该系统可以使用unix系统调用fork处理多个并发连接

客户端输入电影标题,服务器将检查电影是否在那里。如果有,它会告诉客户排名、姓名和方框记录

看看我的分叉实现,客户机请求用户输入,但是程序只是简单地通过了它

输出示例:

connection made with client 127.0.0.1
PID IS 27270


 --> all messages read - connection being closed
CLIENT: Please input an string: PID IS 0
在这一行,CLIENT:请输入一个字符串:PID为0,假设用户输入一个字符串,但是程序会浏览它。如何使程序从客户端接收字符串

服务器代码:

int main()
{
   int                sock, clientsock, mlen, addrsize, msgct, chc, chct, pid;
   struct sockaddr_in addr; //ipv4 address
   char                ch, buf[80];

   /*
    * Create a socket.
    */

   sock = socket(AF_INET, SOCK_STREAM,0); //create socket (AF_NET shows its ipv4 internet connection, SOCK_STREAM shows its a tcp)
   if (sock == -1)
   {   
      perror("opening socket");
      exit(-1);
   }

   //Bind socket to local address
   /*
    * Bind a name to the socket.  Since the server will bind with
    * any client, the machine address is zero or INADDR_ANY.  The port
    * has to be the same as the client uses.
    */

   addr.sin_family = AF_INET;
   addr.sin_port = htons (32351); //port number for local address
   addr.sin_addr.s_addr = htonl (INADDR_ANY); //ip address (you can also hard code it) 

   if (bind(sock, (struct sockaddr *) &addr, //binding, first parameter : is the socket you created, &addr is the 
      sizeof (struct sockaddr_in)) == -1) //error checking
   {  
      perror ("on bind");
      exit (-1);
   } //(at this moment we have binded socket)

   /*
    * Make the socket available for potential clients.
    */

   //if there is connection or not?
   if (listen(sock,1) == -1)  
   {  
      perror("on listen");
      exit(-1);
   }


   //-------Text File Implementation-----------
   FILE *fp;
   char data[5][200];

   char rank[5][2];
   char name[5][255];  
   char value[5][100];

   /* opening file for reading */
   fp = fopen("movie.txt", "r");
   if(fp == NULL) {
      perror("Error opening file");
      return(-1);
   }

   fgets (data[0], 200, fp);
   int i = 1;

   while(fgets (data[i], 200, fp)!=NULL)
   {
      /* writing content to stdout */
      sscanf(data[i],"%s %[^$] %s",rank[i],name[i],value[i]);
      puts(data[i]);
      i+=1;
   }

   //CODE DOES NOT IMPLEMENT AFTER THIS WHILE LOOP

   //close the file 
   fclose(fp);

   addrsize = sizeof(struct sockaddr_in);

   //THIS WHILE LOOP IS NOT BEING IMPLEMENTED...

   while(1)
   {
      clientsock = accept(sock, (struct sockaddr *) &addr, &addrsize);
      if (clientsock == -1)//error checking
      {  
         perror("on accept");
         exit(-1);
      }
      printf("connection made with client ");

      printf ("%s\n", inet_ntoa (addr.sin_addr)); //also print client address

      /* Create child process */
      pid = fork();
      if (pid < 0)
      {
         perror("ERROR on fork");
         exit(1);
      }

      if (pid == 0)
      {
         /* This is the client process */
         close(sock);

         bool exist = false;

         mlen = recv (clientsock, buf, 80, 0);
         if (mlen < 0)
         {
            perror("ERROR reading from socket");
            exit(1);
         }

         int lenS;
         int which;
         for(int i = 1; i<5; i++)
         {
            printf("%s\n\n", name[i]);

            char *pch = strstr(name[i],buf);

            if(pch != NULL)
            {
                which = i;
                exist = true;
                puts("GOOD");


            }
            else
            {
               puts("bad");
            }
         }

         if(exist)
         {

            //SEND TO CLIENT FROM HERE!
            printf("%s\n", rank[which]);
            printf("%s\n", name[which]);
            printf("%s\n", value[which]);

            lenS = strlen(name[which]);
            send (clientsock, name[which], lenS+1, 0);
         }
         else
         {
            //SEND TO CLIENT FROM HERE!!!!
            printf("NOT HERE ");
            send (clientsock, "NOT HERE", 9, 0);
         }

         printf("Here is the message: %s\n",buf);

         exit(0);
      }
      else
      {
         close(clientsock);
         printf(" --> all messages read - connection being closed\n");
      }
   }
}
intmain()
{
int sock、clientsock、mlen、addrsize、msgct、chc、chct、pid;
地址中的结构sockaddr\u;//ipv4地址
char-ch,buf[80];
/*
*创建一个套接字。
*/
sock=socket(AF_INET,sock_STREAM,0);//创建套接字(AF_NET显示其ipv4 internet连接,sock_STREAM显示其tcp连接)
如果(sock==-1)
{   
perror(“开口插座”);
出口(-1);
}
//将套接字绑定到本地地址
/*
*将名称绑定到套接字。因为服务器将使用
*任何客户机,机器地址为零或INADDR__any。端口
*必须与客户端使用的相同。
*/
addr.sin_family=AF_INET;
addr.sin_port=htons(32351);//本地地址的端口号
addr.sin_addr.s_addr=htonl(INADDR_ANY);//ip地址(也可以硬编码)
if(bind(sock,(struct sockaddr*)&addr,//binding,第一个参数:是您创建的套接字,&addr是
sizeof(struct sockaddr_in))==-1)//错误检查
{  
佩罗(“受约束”);
出口(-1);
}//(此时我们已绑定套接字)
/*
*使套接字可供潜在客户使用。
*/
//是否有连接?
如果(听(sock,1)=-1)
{  
佩罗尔(“倾听”);
出口(-1);
}
//-------文本文件实现-----------
文件*fp;
字符数据[5][200];
字符秩[5][2];
字符名[5][255];
字符值[5][100];
/*打开文件进行读取*/
fp=fopen(“movie.txt”、“r”);
如果(fp==NULL){
perror(“打开文件时出错”);
返回(-1);
}
fgets(数据[0],200,fp);
int i=1;
while(fgets(数据[i],200,fp)!=NULL)
{
/*将内容写入标准输出*/
sscanf(数据[i],%s%[^$]%s],秩[i],名[i],值[i]);
看跌期权(数据[i]);
i+=1;
}
//代码在此WHILE循环之后不会实现
//关闭文件
fclose(fp);
addrsize=sizeof(结构sockaddr_in);
//未实现此WHILE循环。。。
而(1)
{
clientsock=accept(sock,(struct sockaddr*)&addr,&addrsize);
if(clientsock==-1)//错误检查
{  
perror(“接受时”);
出口(-1);
}
printf(“与客户建立的联系”);
printf(“%s\n”,inet\u ntoa(addr.sin\u addr));//还可以打印客户端地址
/*创建子进程*/
pid=fork();
if(pid<0)
{
perror(“叉子上的错误”);
出口(1);
}
如果(pid==0)
{
/*这是客户端进程*/
关闭(袜子);
bool exist=false;
mlen=recv(clientsock,buf,80,0);
if(mlen<0)
{
perror(“从套接字读取错误”);
出口(1);
}
内透镜;
int其中;

对于(int i=1;i您在同一台机器上使用同一个控制终端运行客户机和服务器程序。因此,服务器主进程、其客户机服务子进程以及独立的客户机进程都可以写入该终端。它们独立且并发地运行,因此它们的输出可以混合在一起

PID为0
消息是在提示符后发出的,这并不表示客户端程序已跳过接受输入,这确实是不可能的。提示符和PID消息来自不同的进程


从单独的(虚拟)终端启动服务器进程和客户端进程会更清晰,这样它们的输出就不会混合。

您的格式设置很混乱。看起来您在分叉时,父进程会立即关闭套接字。这可能是您无法通信的原因。
int main()
{
   int                sock, addrsize;
   struct sockaddr_in addr;
   unsigned int       in_address;
   char buf[80];
   int mlen;

   /* 
    * Open a socket for Internet stream services.
    */

   sock = socket(AF_INET, SOCK_STREAM,0); //creating a socket to connect to server, AF_INET : ipv4 internet connection, SOCK_STREAM tcp
   if (sock == -1)
   {   perror("opening socket");
       exit(-1);
   }

   addr.sin_family = AF_INET; 
   addr.sin_port = htons (32351); //port number has to be the same as the one from server
   in_address = 127 << 24 | 0 << 16 | 0 << 8 | 1; //ip address, local host, since we are running client and server on the same computer, it needs to have the same ip address
   addr.sin_addr.s_addr = htonl (in_address);

   if (connect (sock, (struct sockaddr *) &addr,  //binding
       sizeof (struct sockaddr_in)) == -1)
   {   
      perror("on connect");
      exit(-1);
   } 

   char word[100];
   int len;

   printf("CLIENT: Please input an string: ");
   scanf("%s", word);

   //printf("You entered: %s\n", word);
   len = strlen(word);

   send (sock, word, len+1, 0);

   mlen = recv (sock, buf, 80, 0);
   printf ("%s\n\n\n\n\n\n\n", buf);

   /* 
    * Do a shutdown to gracefully terminate by saying - "no more data"
    * and then close the socket -- the shutdown is optional in a one way
    * communication that is going to terminate, but a good habit to get
    * into. 
    */

   if (shutdown(sock, 1) == -1)
   {  
      perror("on shutdown");
      exit(-1);
   }
   printf ("Client is done\n");
   close(sock);
}