简体   繁体   中英

Signal received: SIGPIPE (Broken pipe)

I am creating a simple client/server multiplayer game on C++. So the client connects successfully, and when I'm trying to send smth. to it, I get this message from debugger "Signal received: SIGPIPE (Broken pipe)" Here is the code:

Server:

    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);

Client:

    sockaddr_in _tempServerAddress;
    int _tempServerPort=5326;
    int _tempSocket;
    char buf[1];
    string _serverIp=""127.0.0.1";

    _tempServerAddress.sin_family=AF_INET;
    _tempServerAddress.sin_port=_tempServerPort;
    inet_aton(_serverIp.c_str(), &(_tempServerAddress.sin_addr));
    _tempSocket=socket(AF_INET, SOCK_STREAM, 0);

    connect(_tempSocket, (sockaddr*)&_tempServerAddress, sizeof(_tempServerAddress));

    recv(_tempSocket, buf, 1, 0);
    _serverPort=5300+((int)buf[0]-'0');

Client connects successfully, but don't receive anything.

You can't send data on the listening socket. accept returns a new socket that represents the connection, and you send the data on that connection.

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

close(_tempconn);
close(_templistener);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM