服务器未从客户端接收。 Linux TCP / IP通讯

时间:2019-03-10 10:24:30

标签: c++ linux tcp server

我正在Linux操作系统中运行服务器应用程序以从客户端接收消息,然后根据该消息,服务器应将响应发送回客户端。这不是一个一次性的过程,我一直在运行服务器以侦听消息,然后它将响应发送回去。

我面临的问题是,每从服务器发送一条消息,它就不会接收到来自客户端的请求消息,然后在第二次尝试接收它时,但是在客户端,消息却显示为已发送。如果我禁用发送部分,则服务器将正确接收消息。

我在这里做错什么吗?

#include "server.h"

server::server(int port)
{
    bufferLength = 1024;
    buffer = new char[bufferLength+1];
    portNo = port;
    client = -1;
}

server::~server()
{
    delete buffer;
}

void server::run() {
    createSocket();
    serve();
}

void server::createSocket(){
    sockId = socket(AF_INET, SOCK_STREAM, 0);
    cout << "socketId "<<sockId<<" port is "<<this->portNo<<endl;
    if (!sockId) {
        perror("socket");
        exit(-1);
    }

    struct sockaddr_in serverAddress;
    memset(&serverAddress,0,sizeof(serverAddress));
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(portNo);
    serverAddress.sin_addr.s_addr = INADDR_ANY;

    int reUse = 1;
    if (setsockopt(sockId, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof(reUse)) < 0) {
        perror("setsockopt");
        exit(-1);
    }

    if (bind(sockId,(const struct sockaddr *)&serverAddress,sizeof(serverAddress)) < 0) {
        perror("bind");
        exit(-1);
    }

    if (listen(sockId,SOMAXCONN) < 0) {
        perror("listen");
        exit(-1);
    }

}

void server::closeSocket(){
    int status;
    status = close(sockId);
    if (!status) {
        perror("socket");
        exit(-1);
    }
}

void server::serve() {
    struct sockaddr_in clientAddress;
    socklen_t clientLength = sizeof(clientAddress);

    while ((client = accept(sockId,(struct sockaddr *)&clientAddress,&clientLength)) > 0) {
        getMessage(client);
    }
    closeSocket();
}

void server::handle(int client) {
    getMessage(client);
    close(client);
}

void server::getMessage(int client) {
    int result;
    cout<<"receive client "<<client<<endl;
    result = recv(client,buffer,1024,0);

    if(result <= 0)
        perror("receive function failed or finished");

    cout <<"ROI "<<buffer<<endl;

    char* output = NULL;
    output = strstr(buffer,"end");
    char* output3 = NULL;
    output3 = strstr(buffer,"get");

    if(output){
        strncpy(output,"",3);
        writeNewRoi();
        cout<<"restarting "<<endl;
        system("sudo ./restart_detection.sh");
    }

    if(output3){
        sendRoi(client);
    }
}

void server::writeNewRoi(){
    ofstream outfile("ROI.txt");
    outfile << buffer;
    outfile.close();
}

void server::sendRoi(int client){
    ifstream roiFile;
    roiFile.open("ROI.txt");
    string str;
    while(!roiFile.eof()){
        getline(roiFile,str);
        str +=  "\n";
        const char* message = str.c_str();

        int nleft = str.length();
        int nwritten;

        while (nleft) {
            nwritten = send(client, message, nleft, 0);
            nleft -= nwritten;
            message += nwritten;
        }
    }
    roiFile.close();

    char endMsg[] = "end";
    send(client, endMsg, strlen(endMsg), 0);
}

0 个答案:

没有答案