套接字编程问题,退出时发送数据

Socket programming issue, data sent on exit

我在 OS X (C++) 上进行套接字编程时遇到问题。我的客户端可以连接到服务器,但是当我发送数据时,尽管 send() 没有失败,但只有在程序退出时才会发送数据。

这是我的代码:

在客户端构造函数中创建套接字并连接到服务器:

ComBridge::ComBridge(std::string comManagerIP, int portNumber){
    this->myComManagerIP = comManagerIP;
    this->portNum = portNumber;

    initComBridge();
}

ComBridge::~ComBridge(){
   close(socketFD);
}

void ComBridge::initComBridge(){
    /*Opening the socket*/
    socketFD = socket(AF_INET, SOCK_STREAM, 0);
    if (socketFD<0) {
        cout<<"Error opening socket !"<<endl;
        exit(1);
    }

    /*Defining the server address*/
    bzero((char *) &server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    /*bcopy(myComManagerIP.c_str(),
      (char *)&server_addr.sin_addr.s_addr,
      myComManagerIP.size());*/
    inet_pton(AF_INET,myComManagerIP.c_str(),&server_addr.sin_addr);
    server_addr.sin_port = htons(portNum);

    cout<<"ip server store : "<<myComManagerIP.c_str()<<endl;

    cout<<"ip server in server_addr: "<<server_addr.sin_addr.s_addr<<endl;

    /*Connecting to ComManager*/
    int rcon = connect(socketFD, (struct sockaddr*)&server_addr, sizeof(server_addr));
    cout<<"back num for connection : "<<rcon<<endl;
    if (rcon<0) {
        cout<<"Failed connection to ComManager !"<<endl;
    }
}

以及收发方式:

void ComBridge::readMessage(){
    bzero(bufferIn,256);

    /*Waiting for data from socket*/
    ssize_t numOfCharRecv = recv(socketFD, &bufferIn, 255,0);
    //ssize_t numOfCharRecv = read(socketFD,&bufferIn,255);

    if (numOfCharRecv < 0)
        cout<<"ERROR reading socket"<<endl;

    /*Storing the string received in a jsonDocument*/
    jsonDocument.Parse(bufferIn);
    cout<<"message : "<<bufferIn<<endl;
    /*Notifying observers of event*/
    //notify(jsonDocument);
}

void ComBridge::writeMessage(rapidjson::Document& d){
    buffer = myJsonHandler.convertJsonToString(d);

    cout<<buffer<<endl;

    ssize_t writeRes = send(socketFD,buffer.c_str(),strlen(buffer.c_str())+1,0);
    if (writeRes < 0)
        cout<<"ERROR writing to socket"<<endl;
    else cout<<"Message sent ! "<<writeRes<<endl;
}

调用 readMessage 和 writeMessage 的更新方法:

void ComBridge::update(rapidjson::Document& d){

    /*If the message is for the ComManager*/
    if (!d.HasMember("To")) {
        rapidjson::Value myVal;
        /*Adding the comManager IP*/
        myVal.SetString(myComManagerIP.c_str(), d.GetAllocator());
        d.AddMember("To", myVal, d.GetAllocator());
    }

    /*Sending message*/
    writeMessage(d);

    /*Waiting for answer*/
    //readMessage();

}

有没有人知道出了什么问题?

感谢帮助!

Mikael10j

这通常发生在您读行而不是写行时。这会导致行读取方法阻塞,直到:

  • 收到行终止符
  • 流结束
  • 发生错误。

不是直到程序退出才发送数据,而是读行方法不会return直到流结束直到有行结束符或流结束。如果发送方没有发送行终止符,则在关闭套接字之前不会发生任何事情。

void ComBridge::writeMessage(rapidjson::Document& d){
    buffer = myJsonHandler.convertJsonToString(d);
    buffer+="\n\r";
    cout<<buffer<<endl;

    ssize_t writeRes = send(socketFD,buffer.c_str(),strlen(buffer.c_str()),0);
    if (writeRes < 0)
         cout<<"ERROR writing to socket"<<endl;
    else cout<<"Message sent ! "<<writeRes<<endl;
}