// testsocketclient.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <winsock2.h> #include <stdio.h> #include <mstcpip.h> #pragma comment(lib, "Ws2_32.lib") SOCKET sockClient = 0; static int conn_status = 0; #define IP_ADDRESS ("192.168.207.131") #define PORT (9099) #include <iostream> using namespace std; int socket_tcp_alive(int socket) { int ret = 0; int keep_alive = 1; ret = setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, (char*)&keep_alive, sizeof(keep_alive)); if (ret == SOCKET_ERROR) { printf("setsockopt failed: %d \n", WSAGetLastError()); return -1; } { TCP_KEEPALIVE in_keep_alive = 0 {struct}; unsigned Long ul_in_len = the sizeof (struct TCP_KEEPALIVE); struct = {0} TCP_KEEPALIVE out_keep_alive; unsigned Long ul_out_len = the sizeof (struct TCP_KEEPALIVE); unsigned Long ul_bytes_return = 0; in_keep_alive.onoff =. 1; / * * open keepalive / in_keep_alive.keepaliveinterval = 5000; / * transmitting keepalive heartbeat interval - milliseconds * / in_keep_alive.keepalivetime = 1000; / * how long did not start transmitting keepalive packets heartbeat packets - milliseconds * / RET = the WSAIoctl (Socket, SIO_KEEPALIVE_VALS, (LPVOID) & in_keep_alive, ul_in_len, (LPVOID) & out_keep_alive, ul_out_len, & ul_bytes_return, NULL, NULL); IF (RET == of SOCKET_ERROR is) the printf ( "the WSAIoctl failed:% D \ n-", the WSAGetLastError ()) ; return -1; } return 0; } int tcp_connect_server() { int save_errno; SOCKADDR_IN server_addr; conn_status = 0; memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(IP_ADDRESS); server_addr.sin_port = htons(PORT); sockClient = ::socket(PF_INET, SOCK_STREAM, 0); if (sockClient == -1) return sockClient; conn_status = ::connect(sockClient, (struct sockaddr*)&server_addr, sizeof(server_addr)); if (conn_status == -1) { save_errno = errno; // Cleanup closesocket(sockClient); errno = save_errno; // Close On May BE The error return -1; } // evutil_make_socket_nonblocking (sockfd); return sockClient; } DWORD WINAPI monitorThread (LPVOID pM) { the while (. 1) { char szRecvBuf [ 10] = {0}; // Send (sockClient, "the this ge1233 IS Qiang", strlen ( "the this Qiang ge1234 IS"). 1 +, 0); int = nRet the recv (sockClient, szRecvBuf,. 1, MSG_PEEK); / / Notice that the last parameter must be MSG_PEEK, otherwise it will affect the main thread receives information IF (nRet <= 0) { the printf ( "monitor to it: nRet iS% D \ n-", nRet); the closesocket (sockClient); sockClient = NULL; sockClient tcp_connect_server = (); IF (conn_status =! -1) socket_tcp_alive (sockClient); // BREAK; } Sleep (5000); } return 0; } void main () { // load the socket library, version negotiation WORD wVersionRequired; WSADATA WSADATA; int ERR; int Status, save_errno; // request library // WSAStartup (WORD wVersionRequired , WSADATA lpWSAData); wVersionRequired MAKEWORD = (. 1,. 1); ERR = WSAStartup (wVersionRequired, & WSADATA); ! IF (ERR = 0) { // is not equal to 0 to withdraw return; } // the high and low bytes is not. 1 IF (LOBYTE (wsaData.wVersion) = || hibyte. 1 (wsaData.wVersion) =. 1!!) { WSACleanup (); return; } sockClient = Socket (AF_INET, SOCK_STREAM, 0); // setting server address information SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr = inet_addr(IP_ADDRESS); addrSrv.sin_family = AF_INET; addrSrv.sin_port = htons(PORT); while (1) { conn_status = connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)); if (conn_status == -1) { save_errno = errno; //清理 errno = save_errno; //the close may be error Sleep(3000); cout << "one lian test" << endl; continue; } else { socket_tcp_alive(sockClient); break; } } HANDLE handle = CreateThread(NULL, 0, monitorThread, NULL, 0, NULL); // send(sockClient, "this is qiang ge", strlen("this is qiang ge") + 1, 0); getchar(); closesocket(sockClient); WSACleanup(); system("PAUSE"); }