代碼首先Winsock2的錯誤10014
DWORD WINAPI tcp_t(LPVOID lpParam)
{
SOCKET tcp_client_s = (SOCKET)lpParam;
struct sockaddr_in tcp_client;
int tcp_client_len = sizeof(tcp_client), length;
char req[4096], resp[4096];
getpeername(tcp_client_s, (struct sockaddr *)&tcp_client, &tcp_client_len);
cli_log(PROTO_TCP, LOG_SYS, "(%s:%d) TCP thread spawned\n", inet_ntoa(tcp_client.sin_addr), ntohs(tcp_client.sin_port));
length = get_req_tcp(tcp_client_s, req, tcp_client);
if(strci(req, "GET /syachi2ds/web/", 0))
{
while(!strstr(req, "Connection: close\r\n\r\n"))
length += get_req_tcp(tcp_client_s, req + length, tcp_client);
length = check_req(req, resp);
if(length > 0)
send_resp_tcp(tcp_client_s, resp, length, tcp_client);
}
closesocket(tcp_client_s);
cli_log(PROTO_TCP, LOG_SYS, "(%s:%d) socket closed, closing thread\n", inet_ntoa(tcp_client.sin_addr), ntohs(tcp_client.sin_port));
ExitThread(0);
}
int get_req_tcp(SOCKET in_s, char *buf, struct sockaddr_in in)
{
int retval;
cli_log(PROTO_TCP, LOG_COMM, "(%s:%d) waiting for incoming request...\n", inet_ntoa(in.sin_addr), ntohs(in.sin_port));
if ((retval = recv(in_s, buf, 4096, 0)) == SOCKET_ERROR)
cli_log(PROTO_TCP, LOG_ERROR, "(%d) recv() failed\n", WSAGetLastError());
cli_log(PROTO_TCP, LOG_COMM, "(%s:%d) data received\n", inet_ntoa(in.sin_addr), ntohs(in.sin_port));
return retval;
}
這是一個較大的程序我正在寫的一部分做了一些類型的服務器仿真。它適用於TCP流不會分成多個數據包的情況,否則會在while循環中調用的第一個後續recv()
上產生winsock錯誤10014。
PS:strci()
是一個自定義的不區分大小寫strstr()
PS2:我知道有沒有檢查緩衝區REQ陣列上溢出。
在失敗的情況下:其值做的第一'get_req_tcp()'調用返回的'length'(循環之前)? – sb9
此時,如果LOG_ERROR作爲第二個參數傳遞(我已經計劃更改此行爲),cli_log()調用exit(0) – Ithilion
從套接字流中讀取的數據不是nul結尾的,所以您需要手動添加如果你想使用'strstr',每個'recv'後面的''\ 0''。 –