c++ - 如何在 C/C++ 中关闭特定客户端的套接字?

标签 c++ c sockets connection

服务器连接到几个客户端。客户端向服务器发送消息再见。如果服务器关闭它的套接字,它就会失去与其他客户端的连接。我是否需要通知服务器客户端已关闭以及如何在以下简单示例中为特定客户端关闭它:

服务器:

#include "wrappers.h"
int main(){

  unsigned int Sockfd, NewSockfd, ClntLen;
  sockaddr_in ClntAddr, ServAddr;
  int Port = SERV_TCP_PORT;
  char String[MAX_SIZE];
  int Len;

  // open a TCP socket (an Internet stream socket)
  Sockfd = Socket(AF_INET, SOCK_STREAM, 0); // socket() wrapper fn

  // bind the local address, so that the client can send to server
  memset((char*)&ServAddr, 0, sizeof(ServAddr));
  ServAddr.sin_family = AF_INET;
  ServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  ServAddr.sin_port = htons(Port);

  Bind(Sockfd, (sockaddr*) &ServAddr, sizeof(ServAddr));

  // listen to the socket
  Listen(Sockfd, 5);

  for(;;){
     // wait for a connection from a client; this is an iterative server
     ClntLen = sizeof(ClntAddr);
     NewSockfd = Accept(Sockfd, (sockaddr*)&ClntAddr, &ClntLen);

     if(NewSockfd < 0){
        perror("Can't bind local address!");
     }

     // read a message from the client
     Len = read(NewSockfd, String, MAX_SIZE);
     String[Len] = 0;// make sure it's a proper string
     printf("%s\n", String);


     write(NewSockfd, "hello", sizeof("hello"));
     cout<<"wrote to client"<<endl;

     close(NewSockfd);
  }
}

客户:

#include "wrappers.h"

int main()
{
  int Sockfd;
  sockaddr_in ServAddr;
  char ServHost[] = "localhost";
  hostent *HostPtr;
  int Port = SERV_TCP_PORT;
  int BuffSize = 0;

  // get the address of the host
  HostPtr = Gethostbyname(ServHost);

  if(HostPtr->h_addrtype !=  AF_INET){
     perror("Unknown address type!");
     exit(1);
  }

  memset((char *) &ServAddr, 0, sizeof(ServAddr));
  ServAddr.sin_family = AF_INET;
  ServAddr.sin_addr.s_addr = ((in_addr*)HostPtr->h_addr_list[0])->s_addr;
  ServAddr.sin_port = htons(Port);
  bool loop = true;

  int i= 0;
  while(loop){


      // open a TCP socket
      Sockfd = Socket(AF_INET, SOCK_STREAM, 0);

      // connect to the server
      Connect(Sockfd, (sockaddr*)&ServAddr, sizeof(ServAddr));


      if(i>5){
          loop = false;
      }
      i++;
      // write a message to the server
      write(Sockfd, "hello world", sizeof("hello world"));

      char String[10];
      int Len = read(Sockfd, String, MAX_SIZE);
      String[Len] = 0;// make sure it's a proper string
      printf("%s === \n", String);

  }


  close(Sockfd);
}

最佳答案

如果client 套接字关闭,您将从read 得到服务器端的返回值0。这是客户端连接结束的指示。

如果你尝试write到这样的socket,你会收到SIGPIPE信号,write会返回错误errno 设置为 EPIPE(“Broken Pipe”)。

关于c++ - 如何在 C/C++ 中关闭特定客户端的套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23798408/

相关文章:

c - 函数循环

c - C语言中的DCCP套接字编程

c - 传输端已连接错误: 106 with connect()

c++ - 在 NDK 中启用链接时间优化时出现链接器错误

c++ - 是否需要为 C/C++ 中的文件 I/O 操作创建自己的缓冲区?

c++ - 检测 keyPressEvent 上的插入符 (^) 键 - Qt C++

c++ - GNU C 和 C++ 链接错误

使用 Http Post 的 C Wininet 文件上传

c++ - 为什么 gcc 不发出格式警告?

windows - IOCP : how does the kernel decide to complete WSASend synchronously or asynchronously?