Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.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

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++上创建了一个简单的客户机/服务器多人游戏。因此,当我尝试发送smth时,客户端成功连接。对于它,我从调试器中得到以下消息“接收到的信号:SIGPIPE(断管)” 代码如下:_C++_Sockets_Broken Pipe - Fatal编程技术网

接收到信号:信号管(断管) 我在C++上创建了一个简单的客户机/服务器多人游戏。因此,当我尝试发送smth时,客户端成功连接。对于它,我从调试器中得到以下消息“接收到的信号:SIGPIPE(断管)” 代码如下:

接收到信号:信号管(断管) 我在C++上创建了一个简单的客户机/服务器多人游戏。因此,当我尝试发送smth时,客户端成功连接。对于它,我从调试器中得到以下消息“接收到的信号:SIGPIPE(断管)” 代码如下:,c++,sockets,broken-pipe,C++,Sockets,Broken Pipe,服务器: string _ip = "127.0.0.1"; sockaddr_in _tempFullAddress; int _templistener; int _tempPort; int mes = 27; _tempFullAddress.sin_family = AF_INET; _tempFullAddress.sin_port = 5326; inet_aton(_ip.c_str(), &(_tempF

服务器:

    string _ip = "127.0.0.1";
    sockaddr_in _tempFullAddress;
    int _templistener;
    int _tempPort;
    int mes = 27;

    _tempFullAddress.sin_family = AF_INET;
    _tempFullAddress.sin_port = 5326;
    inet_aton(_ip.c_str(), &(_tempFullAddress.sin_addr));

    _templistener = socket(AF_INET, SOCK_STREAM, 0);

    int bindResult = 
bind(_templistener, (sockaddr*) &_tempFullAddress, sizeof(_tempFullAddress));
    if (bindResult<0){
        cout<<"Error on binding\n";
        return 0;
    }

    listen(_templistener, 1);

    char buf[1];
    buf[0]=(char)mes;

    accept(_templistener, NULL, NULL);
    send(_templistener, buf, 1, 0);

    close(_templistener);

客户端成功连接,但未接收任何内容。

您无法在侦听套接字上发送数据
accept
返回表示连接的新套接字,然后发送该连接上的数据

int _tempconn = accept(_templistener, NULL, NULL);
send(_tempconn, buf, 1, 0);

close(_tempconn);
close(_templistener);
int _tempconn = accept(_templistener, NULL, NULL);
send(_tempconn, buf, 1, 0);

close(_tempconn);
close(_templistener);