參考下面:
SOCKET server_socket;
SOCKADDR_IN internet_addr;
int ret, error, addr_len;
PROXY_SOCK_PARAM_T *_param;
HANDLE hThread;
unsigned int thid;
// 建立 socket
server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(server_socket == INVALID_SOCKET) /* 呼叫 socket 不成功, 返回 INVALID_SOCKET,
呼叫 WSAGetLastError() 可檢視錯誤程式碼 */
{
error = WSAGetLastError();
// TODO: 錯誤處理
return 0;
}
// 進行 Socket 繫結
internet_addr.sin_family = PF_INET;
internet_addr.sin_port = htons(PROXY_SERVER_PORT);
internet_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(server_socket, (SOCKADDR *)&internet_addr, sizeof(internet_addr));
if(ret != 0) /* bind 成功返回 0. 否則返回 SOCKET_ERROR, 呼叫 WSAGetLastError() 可檢視錯誤程式碼 */
closesocket(server_socket);
// 在 socket 上進行監聽
ret = listen(server_socket, 128); /* listen 成功返回 0. 否則返回 SOCKET_ERROR,
if(ret != 0)
std::cout << "[PROXY-SERVR] 埠 5200 監聽啟動..." << std::endl;
// 接收來自客戶端的請求
for(;;)
// 組織傳遞給新執行緒的資料結構
_param = (PROXY_SOCK_PARAM_T *)malloc(sizeof(PROXY_SOCK_PARAM_T));
_param->server_socket = server_socket;
addr_len = sizeof(_param->client_addr);
_param->client_socket = accept( _param->server_socket,
(SOCKADDR *)&(_param->client_addr),
&(addr_len));
if(_param->client_socket == INVALID_SOCKET)
} else {
// 建立工作執行緒
hThread = (HANDLE)_beginthreadex(NULL, 0, server_work_thread, (void *)_param, 0, &thid);
if(hThread == INVALID_HANDLE_VALUE)
free(_param);
CloseHandle(hThread);
參考下面:
SOCKET server_socket;
SOCKADDR_IN internet_addr;
int ret, error, addr_len;
PROXY_SOCK_PARAM_T *_param;
HANDLE hThread;
unsigned int thid;
// 建立 socket
server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(server_socket == INVALID_SOCKET) /* 呼叫 socket 不成功, 返回 INVALID_SOCKET,
呼叫 WSAGetLastError() 可檢視錯誤程式碼 */
{
error = WSAGetLastError();
// TODO: 錯誤處理
return 0;
}
// 進行 Socket 繫結
internet_addr.sin_family = PF_INET;
internet_addr.sin_port = htons(PROXY_SERVER_PORT);
internet_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(server_socket, (SOCKADDR *)&internet_addr, sizeof(internet_addr));
if(ret != 0) /* bind 成功返回 0. 否則返回 SOCKET_ERROR, 呼叫 WSAGetLastError() 可檢視錯誤程式碼 */
{
error = WSAGetLastError();
closesocket(server_socket);
// TODO: 錯誤處理
return 0;
}
// 在 socket 上進行監聽
ret = listen(server_socket, 128); /* listen 成功返回 0. 否則返回 SOCKET_ERROR,
呼叫 WSAGetLastError() 可檢視錯誤程式碼 */
if(ret != 0)
{
error = WSAGetLastError();
closesocket(server_socket);
// TODO: 錯誤處理
return 0;
}
std::cout << "[PROXY-SERVR] 埠 5200 監聽啟動..." << std::endl;
// 接收來自客戶端的請求
for(;;)
{
// 組織傳遞給新執行緒的資料結構
_param = (PROXY_SOCK_PARAM_T *)malloc(sizeof(PROXY_SOCK_PARAM_T));
_param->server_socket = server_socket;
addr_len = sizeof(_param->client_addr);
_param->client_socket = accept( _param->server_socket,
(SOCKADDR *)&(_param->client_addr),
&(addr_len));
if(_param->client_socket == INVALID_SOCKET)
{
error = WSAGetLastError();
// TODO: 錯誤處理
} else {
// 建立工作執行緒
hThread = (HANDLE)_beginthreadex(NULL, 0, server_work_thread, (void *)_param, 0, &thid);
if(hThread == INVALID_HANDLE_VALUE)
{
free(_param);
} else {
CloseHandle(hThread);
}
}
}