C++ 客户机-服务器编程 在这里,代码完美地执行了所需的输出 它检查条件并在条件不为真时打印错误,但即使条件为真并遵循要求的输出,它也会打印错误 在小if-else条件下查找问题 如何纠正

C++ 客户机-服务器编程 在这里,代码完美地执行了所需的输出 它检查条件并在条件不为真时打印错误,但即使条件为真并遵循要求的输出,它也会打印错误 在小if-else条件下查找问题 如何纠正,c++,unix,client-server,serversocket,fork,C++,Unix,Client Server,Serversocket,Fork,在下面的代码中,如果(fork()==0)&如果(!request.compare(“”==0)没有效果 /* Loop forever */ while (1) { /* Accept a client connection */ clientFd = accept (serverFd, clientSockAddrPtr, (socklen_t*)&clientLen); //Fork child //if (fork() == 0) {

在下面的代码中,如果(fork()==0)&
如果(!request.compare(“”==0)
没有效果

/* Loop forever */ 
while (1) 
{
/* Accept a client connection */    
    clientFd = accept (serverFd, clientSockAddrPtr, (socklen_t*)&clientLen); 

//Fork child
    //if (fork() == 0) 
    {
    while(1)
    {   //Reads the client
            string request = read_Request (clientFd); 
        bool found = false;
        string response="Error !!! Country not found"; 
        //if (!request.compare("") == 0) 
        {

            for(vector<Country>::iterator it = countryData.begin(); it != countryData.end(); it++)
            {
        if(request.compare(it -> name) == 0) 
        {   
        string countryName = it -> name; 
        response += "\n";               
        response += it -> name; 
        response += "'s TLD Code"; 
        response += ((countryName).append("'s TLD Code")); 
        response += ": ";
        response += it -> TLDCode; 
        response += "\n";

        countryName = it -> name;
        response += it -> name;
        response += "'s FIPS104 Country Code"; 
        response += ((countryName).append("'s FIPS104 Country Code")); 
        response += ": ";
        response += it -> FIPS104CountryCode; 
        response += "\n";

        countryName = it -> name;
        response += it -> name;
        response += "'s ISO2 Country Code"; 
        response += ((countryName).append("'s ISO2 Country Code")); 
        response += ": ";
        response += it -> ISO2CountryCode; 
        response += "\n";

        countryName = it -> name; 
        response += it -> name; 
        response += "'s population"; 
        response += ((countryName).append("'s population")); 
        response += ": ";
        response += it -> population; 
        response += "\n";


            found = true;

            }
        }
}



write (clientFd, response.c_str(), strlen (response.c_str()) + 1);  
    }
    }

        close (clientFd); 
}
/*永远循环*/
而(1)
{
/*接受客户端连接*/
clientFd=accept(serverFd、clientSockAddrPtr、(socklen\u t*)和clientLen);
//叉子
//如果(fork()==0)
{
而(1)
{//读取客户端
字符串请求=读取请求(clientFd);
bool-found=false;
string response=“错误!!!找不到国家/地区”;
//如果(!request.compare(“”==0)
{
对于(vector::iterator it=countryData.begin();it!=countryData.end();it++)
{
if(request.compare(it->name)==0)
{   
字符串countryName=it->name;
响应+=“\n”;
响应+=它->名称;
响应+=“'的TLD代码”;
响应+=((countryName).append(“'s TLD代码”);
响应+=“:”;
响应+=it->TLDCode;
响应+=“\n”;
countryName=it->name;
响应+=它->名称;
响应+=“'s FIPS104国家代码”;
响应+=((国家名称).append('s FIPS104国家代码”);
响应+=“:”;
响应+=it->FIPS104CountryCode;
响应+=“\n”;
countryName=it->name;
响应+=它->名称;
响应+=“'的ISO2国家/地区代码”;
响应+=((countryName).append(“'s ISO2国家代码”);
响应+=“:”;
响应+=it->ISO2CountryCode;
响应+=“\n”;
countryName=it->name;
响应+=它->名称;
响应+=“'的总体”;
响应+=((countryName).append(“'s population”);
响应+=“:”;
响应+=it->总体;
响应+=“\n”;
发现=真;
}
}
}
写入(clientFd,response.c_str(),strlen(response.c_str())+1);
}
}
关闭(clientFd);
}
}

如果为true和false,则打印字符串响应


请提供任何建议

好的,在这里为变量
response

String response=“错误!!!找不到国家/地区”

然后在这里您将其他字符串连接到response,这将提供您的输出

response += "\n";               
response += it -> name; 
i、 错误!!!找不到国家/地区+\n+国家/地区->名称等


其次,正如@ChrisJ.Kiick所说,您有一个神秘的字符串类型,它不是c语言的一部分

您的代码中没有任何内容可以打印字符串响应。这是不完整的。另外,这似乎不是C:C.@ChrisJ.Kiick中没有字符串类型,我已经对代码进行了更改。