我试图将unix套接字api调用包装在C++类中,以使其更易于使用。我已经用C语言编写了一个最小的工作示例,并将代码复制到了一个类中。从下面可以看到,无论我调用包装程序代码的函数版本还是方法版本,代码都是相同的。请参阅下面的难看的临时代码:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

static int sockfd;
static int rec_sock;
static int len;

typedef struct sockaddr_in sockaddr_in;
static sockaddr_in addr;
static sockaddr_in recaddr;

int ServerSocket_socket(int family, int type, int protocol)
{
    sockfd = socket(AF_INET, SOCK_STREAM, 0)
    return sockfd;
}

void ServerSocket_bind(int port)
{
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_family = AF_INET;
    addr.sin_port = port;

    bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
}

void ServerSocket_printInfo(void)
{
    len = sizeof(addr);
    getsockname(sockfd, (struct sockaddr *)&addr, (socklen_t *)&len);
    printf("ip = %s, port = %d\n", inet_ntoa(addr.sin_addr), (addr.sin_port));
}

void ServerSocket_listen(int backlog)
{
    listen(sockfd, backlog);
}

void ServerSocket_accept(void)
{
  rec_sock = accept(sockfd, (struct sockaddr *)(&recaddr), (socklen_t *)&len);
}

void ServerSocket_printPeerInfo(void)
{
    printf("remote machine = %s, port = %x, %x.\n", inet_ntoa(recaddr.sin_addr), recaddr.sin_port, ntohs(recaddr.sin_port));
    memset(&recaddr, 0, sizeof(recaddr));
    len = sizeof(addr);
    getpeername(rec_sock, (struct sockaddr *)&recaddr, (socklen_t *) &len);
}

void ServerSocket_write(void)
{
  write(rec_sock, "hi, there", 10);
  sleep(20);
  exit(1);
}

struct ServerSocket
{
  int socket(int family, int type, int protocol)
  {
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      return sockfd;
  }

  void bind(int port)
  {
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_family = AF_INET;
    addr.sin_port = port;

    ::bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
  }

  void printInfo(void)
  {
      len = sizeof(addr);
      getsockname(sockfd, (struct sockaddr *)&addr, (socklen_t *)&len);
      printf("ip = %s, port = %d\n", inet_ntoa(addr.sin_addr), (addr.sin_port));
  }

  void listen(int backlog)
  {
      ::listen(sockfd, backlog);
  }

  void accept(void)
  {
    rec_sock = ::accept(sockfd, (struct sockaddr *)(&recaddr), (socklen_t *)&len);
  }

  void printPeerInfo(void)
  {
      printf("remote machine = %s, port = %x, %x.\n", inet_ntoa(recaddr.sin_addr), recaddr.sin_port, ntohs(recaddr.sin_port));
      memset(&recaddr, 0, sizeof(recaddr));
      len = sizeof(addr);
      getpeername(rec_sock, (struct sockaddr *)&recaddr, (socklen_t *) &len);
      printf("remote machine = %s, port = %d, %d.\n", inet_ntoa(recaddr.sin_addr), recaddr.sin_port, ntohs(recaddr.sin_port));
  }

  void write(void)
  {
    ::write(rec_sock, "hi, there", 10);
    sleep(20);
    exit(1);
  }
};

当我调用函数时,代码就像是冠军。但是,当我运行包装在方法中的几乎相同的代码时,连接被拒绝。知道这个最小的代码更改会导致示例无法正常工作吗?
   ServerSocket_socket(AF_INET, SOCK_STREAM, 0);
    ServerSocket_bind(1031);
    ServerSocket_printInfo();
    ServerSocket_listen(5);
    ServerSocket_accept();
    ServerSocket_printPeerInfo();
    ServerSocket_write();
  /*
    ServerSocket sock;
    sock.socket(AF_INET, SOCK_STREAM, 0);
    sock.bind(1031);
    sock.printInfo();
    sock.listen(5);
    sock.accept();
    sock.printPeerInfo();
    sock.write();
  */

最佳答案

在ServerSocket_bind中更改:

addr.sin_port =端口;



addr.sin_port = htons(端口);

它应该工作。另外,使用strace调试syscall(对于libc,则使用ltrace),如下所示:

strace -f ./a.out

ltrace ./a.out

关于c++ - 套接字api在类内部不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9439684/

10-13 00:46