【问题标题】:Client Server Programming客户端服务器编程
【发布时间】:2014-02-01 17:38:59
【问题描述】:
  • 这里的代码完美地执行了所需的输出
  • 它会检查条件并在条件不为真时打印错误,但即使条件为真且遵循但需要的输出,它也会打印。
  • 在一个小的 if else 条件下发现问题
  • 如何纠正
  • 在下面的代码中if (fork() == 0) & if (!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); 
    }
    

    }

Here is the snip of output

在真假的情况下打印字符串响应。

有什么建议

【问题讨论】:

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

标签: c++ unix client-server serversocket fork


【解决方案1】:

好的,在这里你将一个字符串分配给变量response
String response="Error !!! Country not found";

然后在这里您将其他字符串连接到响应中,从而为您提供输出。

response += "\n";               
response += it -> name; 

即错误 !!!未找到国家 + \n + it->名称等

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

【讨论】:

    猜你喜欢
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多