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

时间:2015-03-28 08:43:26

标签: c++ sockets tcp xcode6 osx-yosemite

我在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的update方法:

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

2 个答案:

答案 0 :(得分:0)

这通常发生在您阅读线条但没有写线的情况下。这导致行读取方法阻塞,直到:

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

在程序退出之前,数据不会被发送,但是线路读取方法直到流程结束才会返回,直到那里为止。行终止符或流结束。如果发件人没有发送行终止符,则在关闭套接字之前不会发生任何事情。

答案 1 :(得分:0)

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