服务器返回<null>值 iOS 端如何处理

两张图让你对服务器返回值不再纠结,其实就是过滤吧<null>  类型替换成 "" 不就得了

// 网络请求成功后,拿出 response object 在这里进行处理


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
写一个 Web 服务器需要考虑很多方面,包括网络通信、请求处理、安全性等等。在本回答中,我将提供一份基于 C++ 和 Ubuntu 系统的 Web 服务器的简单实现,希望对您有所帮助。 以下是大体实现思路: 1. 创建一个 TCP 套接字,绑定服务器的 IP 地址和口号,并监听客户连接请求。 2. 接收客户连接请求,并创建一个新的线程来处理连接。 3. 在新的线程中,接收客户发送的 HTTP 请求报文,并提取出请求头和请求体。 4. 解析请求头,获取请求方法、请求 URL、HTTP 版本号等信息。 5. 根据请求 URL,读取服务器本地的文件并将其作为响应体返回给客户。 6. 构造 HTTP 响应报文,包括响应头和响应体,并发送给客户。 7. 关闭连接,线程退出。 以下是一个简单的代码示例,仅供参考: ```c++ #include <iostream> #include <string> #include <cstring> #include <sys/socket.h> #include <netinet/in.h> #include <pthread.h> #include <unistd.h> #include <fstream> using namespace std; const int PORT = 8080; const int BUFFER_SIZE = 1024; void handle_request(int client_sock) { char buffer[BUFFER_SIZE]; memset(buffer, 0, BUFFER_SIZE); int len = recv(client_sock, buffer, BUFFER_SIZE - 1, 0); if (len <= 0) { cout << "recv error" << endl; return; } // 解析请求头 string request(buffer, len); string method = request.substr(0, request.find(' ')); string url = request.substr(request.find(' ') + 1, request.find(' ', request.find(' ') + 1) - request.find(' ') - 1); string version = request.substr(request.find("\r\n") + 2, request.find("\r\n", request.find("\r\n") + 2) - request.find("\r\n") - 2); // 处理GET请求 if (method == "GET") { if (url == "/") { url = "/index.html"; } string file_path = "./www" + url; ifstream ifs(file_path.c_str(), ios::binary); if (!ifs) { string not_found = "HTTP/1.1 404 Not Found\r\n\r\n"; send(client_sock, not_found.c_str(), not_found.length(), 0); return; } string content((istreambuf_iterator<char>(ifs)), (istreambuf_iterator<char>())); string ok = "HTTP/1.1 200 OK\r\nContent-Length: " + to_string(content.length()) + "\r\n\r\n" + content; send(client_sock, ok.c_str(), ok.length(), 0); } // 关闭连接 close(client_sock); } void* worker(void* arg) { int client_sock = *(int*)arg; handle_request(client_sock); return NULL; } int main() { int server_sock = socket(AF_INET, SOCK_STREAM, 0); if (server_sock < 0) { cout << "socket error" << endl; return -1; } struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); server_addr.sin_addr.s_addr = INADDR_ANY; if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { cout << "bind error" << endl; return -1; } if (listen(server_sock, 10) < 0) { cout << "listen error" << endl; return -1; } while (true) { struct sockaddr_in client_addr; socklen_t client_addr_len = sizeof(client_addr); int client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &client_addr_len); if (client_sock < 0) { cout << "accept error" << endl; continue; } pthread_t tid; pthread_create(&tid, NULL, worker, (void*)&client_sock); } close(server_sock); return 0; } ``` 上述代码仅为一个简单的示例,实际的 Web 服务器需要考虑更多的细节问题,例如请求报文的解析、请求的安全性、并发处理等等。因此,如果您需要实现一个完整的 Web 服务器,建议参考更多的资料和实现方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dzb1060545231

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值