Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 客户不';不要多次发送数据_C_Sockets - Fatal编程技术网

C 客户不';不要多次发送数据

C 客户不';不要多次发送数据,c,sockets,C,Sockets,您好,我正在尝试为自己学习C套接字编程,最近我遇到了这个问题,我创建了服务器,它工作得很好,但我相信客户端有一些问题,因为在第一次询问服务器后,第二次不再工作,就像他卡在无限循环中一样,无论我键入什么数字。基本上,我的应用程序是这样工作的,客户端连接到服务器,他收到一个菜单来获取图片和文本文件(尚未实现),客户端选择1选项,服务器响应等等,直到选择退出选项 /* This is accept loop from server */ while(1) { s

您好,我正在尝试为自己学习C套接字编程,最近我遇到了这个问题,我创建了服务器,它工作得很好,但我相信客户端有一些问题,因为在第一次询问服务器后,第二次不再工作,就像他卡在无限循环中一样,无论我键入什么数字。基本上,我的应用程序是这样工作的,客户端连接到服务器,他收到一个菜单来获取图片和文本文件(尚未实现),客户端选择1选项,服务器响应等等,直到选择退出选项

    /* This is accept loop from server */
    while(1)
    {
        sin_size = sizeof(struct sockaddr_in);

        if((new_fd = accept/* expression */(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
        {
            perror("Server-accept() error");
            continue;
        }


       printf("Server: Got connection from %s\n", inet_ntoa(their_addr.sin_addr));

        //Server sends this
        char msg[100] ="\nWelcome\n1.Get picture.\n2.Get text file.\n3.Quit\n";
        send(new_fd,msg,sizeof(msg),0);

        if((recv(new_fd,buf,12,0)) == -1)
        {
          printf("Didn't receive the data from client\n");
        }else
        {
          printf("I got this from client: %c\n",buf[0]);
          char *customMsg;
          switch (buf[0]) {
          case 49:
          customMsg ="Picture sent.";
          send(new_fd,customMsg,50,0);
          break;
          case 50:
          customMsg ="Text file sent.";
          send(new_fd,customMsg,50,0);
          break;
          case 51:
          customMsg ="Goodbye.";
          send(new_fd,customMsg,50,0);
          close(new_fd);
          printf("Server-new socket, new_fd closed successfully...\n");
          break;
          }
        }

     }

    /* This is the client from connect phase */

    if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)
    {
        perror("connect()");
        exit(1);
    }
    int endTransmission = 0;

    if((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1)
    {
        perror("recv() error");
        exit(1);
    }

    buf[numbytes] = '\0';
    printf("Response from server: %s", buf);
    do
    {
      char *valToSend;
      printf("Enter your option: ");
      scanf("%s",valToSend );
      send(sockfd,valToSend,1,0);

      recv(sockfd,buf,sizeof(buf),0);


      printf("\nThis is what I got back from server:\n%s\n", buf);

      if(*valToSend == 3)
         endTransmission = 1;
    }while(endTransmission != 1);

    printf("\nClient-Closing sockfd\n");
    close(sockfd);
我相信客户端有一些问题,因为在第一次询问服务器后,第二次再也不能工作了

服务器有一些问题,即在连接仍然建立时重复调用
accept
。解决方案是在服务器菜单的发送和对客户机选择的响应(例如)周围放置一个内部循环。G通过改变

        send(new_fd,msg,sizeof(msg),0);


(无
)。

可能您的客户端在
recv()
调用中被阻塞,等待服务器发送数据?所有三个常见的可疑对象:(
char*valToSend;scanf(“%s”,valToSend)
数据被扫描到的确切位置?你能分享错误日志吗?没有错误日志,它就像Jeremy Friesner在recv()中说的那样被卡住了……但为什么第一次像预期的那样工作正常,第二次又卡住了?这个问题我看不出来
        while (send(new_fd, msg, sizeof msg, 0) > 0)