0

我正在尝试实现一对能够通过 Unix 套接字交换信息的服务器/客户端程序。问题是客户端不断被服务器重置并循环重新运行,并且在第一次之后我无法从套接字发送/接收数据。现在我有这样的事情:

服务器:

int main(int argc, char const *argv[]){
    unix_socket server;
    server.initSocket(DEFAULT_SOCKET_PATH,SERVER_MODE);
    server.wait();

    bool first=true;

    for (int i = 0; i < 30; ++i)
    {

            //send & receive data

        server.closeSocket();
        first=false;
    }

    return 0;
}

客户:

int loop_controller(){
  unix_socket client;
  client.initSocket(DEFAULT_SOCKET_PATH,CLIENT_MODE);

  // receive & send data

  client.closeSocket();
}


int main () {

  for (int i = 0; i < 30; ++i){
    loop_controller();
  }  

  return 0;
}

initSocket ()wait()closeSocket()成员unix_socket(服务器模式 0,客户端模式 1):

void unix_socket::initSocket(const char* sock_path, const int sc_mode){

    if (sc_mode==0){
        if (mode != sc_mode) mode=sc_mode;
        if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            //exit(1);
        }
        // ---

        local.sun_family = AF_UNIX;
        strcpy(local.sun_path, sock_path);
        unlink(local.sun_path);
        len = strlen(local.sun_path) + sizeof(local.sun_family);
        if (bind(sock, (struct sockaddr *)&local, len) == -1) {
            close(sock);
            perror("bind"); 
                    //exit(1);
        }
        // ---

    } else if(sc_mode==1) {
        if (mode != sc_mode) mode=sc_mode;

        if ((client_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            //exit(1);
        }
        // ---
    remote.sun_family = AF_UNIX;
        strcpy(remote.sun_path, sock_path);
        len = strlen(remote.sun_path) + sizeof(remote.sun_family);
        if (connect(client_sock, (struct sockaddr *)&remote, len) == -1) {
            perror("connect");
            //exit(1);
        }
        // ---
    } else printf("Invalid sc_mode argument: %d\n",sc_mode);
}
//...

void unix_socket::wait(){
    if (mode==0){
        if (listen(sock, 5) == -1) {
            perror("listen");
            //exit(1);
        }
        t = sizeof(remote);
        if ((client_sock = accept(sock, (struct sockaddr *)&remote, &t)) == -1) {
            perror("accept");
            //exit(1);
        }
    } else printf("Invalid call to wait(): mode %d\n, must be 0",mode);
}
//...

void unix_socket::closeSocket(){
    //close(sock);
    close(client_sock); 
}
//...

然而,当使用 Valgrind 运行这两个程序时,我收到了 SIGPIPE 错误:

==5384== 
==5384== Process terminating with default action of signal 13 (SIGPIPE)
==5384==    at 0x573DDA2: send (send.c:28)
==5384==    by 0x405910: unix_socket::sendMsg(char, double) (in  .../server)
==5384==    by 0x405B6C: unix_socket::sendVectorXd(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) (in .../server)
==5384==    by 0x402044: main (in .../server)
==5384== 

我猜在关闭/重新打开套接字时某些事情没有正确完成。什么是正确的程序?

4

1 回答 1

1

在你的循环中,你只accept()ing 一次。循环体的结尾表示客户端套接字已关闭,但它永远不会accept()在下一次迭代中再次被编辑。

你的循环应该是这样的:

server.startListening(); // bind, listen and friends

for (int i = 0; i < 30; i++) {
    server.acceptClient(); // Call accept
    // insert byte pushing routines here..
    server.closeSocket(); // call close() on socket returned by accept()
}
于 2013-10-16T16:08:33.097 回答