C语言 通过 send() recv() 发送多条消息,Socket 编程,C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4172538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:01:49  来源:igfitidea点击:

Sending multiple messages via send() recv(), Socket programming, C

csocketssendrecv

提问by ragnaroh

I'm trying to make a program (client) which kan send a message to a server upon request from user. Stripped down code follows:

我正在尝试制作一个程序(客户端),它可以根据用户的请求向服务器发送消息。精简后的代码如下:

Client:

客户:

int main(int argc, char **argv) {

  struct sockaddr_in servaddr;
  int sock = socket(AF_INET, SOCK_STREAM, 0);

  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(6789);
  servaddr.sin_addr.s_addr = inet_addr(<ip_address_of_server>);

  while(1) {

    char message[161];
    fgets(message, 161, stdin);

    /* Replacing '\n' with '
int main(int argc, char **argv) {

  struct sockaddr_in servaddr;  
  int sock = socket(AF_INET, SOCK_STREAM, 0);

  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(6789);

  bind(sock, (struct sockaddr *)&servaddr, sizeof(servaddr));  
  listen(sock, 5);

  while(1) {
    int clisock = accept(sock, (struct sockaddr *) NULL, NULL);

    if (clisock >= 0) {
      int messageLength = 160;
      char message[messageLength+1];
      int in, index = 0, limit = messageLength;

      while ((in = recv(clisock, &message[index], messageLength, 0)) > 0) {
        index += in;
        limit -= in;
      }

      printf("%s\n", message);
    }

    close(clisock);
  }
}
' */ char *tmp = strchr(message, '\n'); if (tmp) *tmp = '
servaddr.sin_addr.s_addr = inet_addr(<ip_address_of_server>);
'; connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)); send(sock, message, strlen(message), 0); close(sock); } }

Server:

服务器:

inet_pton(AF_INET,"<ipofserver>",&servaddr.sin_addr);

Now, this works for the first message I send. But then it is not able to make another connection (I get the error message "Bad file descriptor" when trying to connect in the Client program.) Can anyone see what I have misunderstood? Thank you :)

现在,这适用于我发送的第一条消息。但是它无法建立另一个连接(尝试在客户端程序中连接时我收到错误消息“错误的文件描述符”。)谁能看到我误解了什么?谢谢 :)

采纳答案by Yusuf Khan

your client programme also does the same mistake, first time you open the socket but after the first connection is done you close the socket, so the next time in the loop the socket descriptor is not valid, you need to re-open the socket but that's missing, please remove the socket call from top and add the below line in the start of while loop

您的客户端程序也犯了同样的错误,第一次打开套接字但在第一次连接完成后关闭了套接字,因此下次循环中套接字描述符无效,您需要重新打开套接字但缺少的,请从顶部删除套接字调用并在 while 循环的开头添加以下行

int sock = socket(AF_INET, SOCK_STREAM, 0);

int sock = socket(AF_INET, SOCK_STREAM, 0);

回答by Sreevisakh

##代码##

instead of the above lines in your client code use the following

而不是您的客户端代码中的上述行使用以下

##代码##

perform an error check for the fllowing function also.

还对流动函数执行错误检查。

回答by caf

The problem is that you're closing the listening socket sock, instead of the client socket clisock.

问题是您正在关闭侦听 socket sock,而不是客户端 socket clisock