Exercise using socket to send simple http get requests

Source: Internet
Author: User

In recent projects, mobile phone verification modules need to be embedded in the game, while mobile phone binding information is stored in the Operation background, and the game server cannot be directly accessed, therefore, the game server needs to send some HTTP requests to the operation background to obtain such information. Because these functions are written on the server end, in order to avoid server blocking as much as possible, the select Operation is added before the recv and a thread is allocated for each HTTP request. Here are some exercises for this requirement. [Cpp] # include <stdio. h> # include <WinSock2.h> # pragma comment (lib, "ws2_32.lib") // connect to the server to send a message and save the received data to pReceiveBuf. In this process, select and recv the message, until no data arrives or the received cache is full int connectAndSendData (const char * szHost, unsigned short nPort, const char * pDataToSend, unsigned int nDataSize, char * pReceiveBuf = NULL, unsigned int * pnReceiveSize = NULL) {WORD wVersionRequested = MAKEWORD (1, 1); WSADATA wsaData; int err =: WSAStartup (wVersionRequested, & wsaData); if (0! = Err) {printf ("[connectAndSendData]: WSAStartup failed. return % d. \ r \ n", err); return-1;} if (wsaData. wVersion! = WVersionRequested) {printf ("[connectAndSendData]: wsaData. wVersion % d is not equal to wVersionRequested % d. \ r \ n ", wsaData. wVersion, wVersionRequested);: WSACleanup (); return-2;} SOCKET sock =: socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); if (INVALID_SOCKET = sock) {printf ("[connectAndSendData]: socket error % d. \ r \ n ", WSAGetLastError (); return-3;} struct hostent * p_hostent = gethostbyn Ame (szHost); if (NULL = p_hostent) {printf ("[gethostbyname]: socket error % d. \ r \ n ", WSAGetLastError ();: closesocket (sock);: WSACleanup (); return-4;} SOCKADDR_IN addr_server; addr_server.sin_family = AF_INET; topology = * (struct in_addr *) p_hostent-> h_addr); memset (addr_server.sin_zero, 0, 8); addr_server.sin_port = htons (nPort); err =: connect (sock, (SOCKADDR *) & addr_server, s Izeof (addr_server); if (SOCKET_ERROR = err) {printf ("[connectAndSendData]: connect % s: % d error % d. \ r \ n ", szHost, nPort, WSAGetLastError ();: closesocket (sock);: WSACleanup (); return-5;} err = :: send (sock, pDataToSend, nDataSize, 0); if (SOCKET_ERROR = err) {printf ("[connectAndSendData]: send error % d. \ r \ n ", WSAGetLastError ();} if (NULL! = PReceiveBuf & NULL! = PnReceiveSize) {char * p_receive = pReceiveBuf; char * p_buf = p_receive; int n_buf_len = * pnReceiveSize; int n_len = n_buf_len-1; int n_read = 0; char temp [256]; int n_head_len =-1; int n_content_len =-1; const char * content = NULL; while (1) {fd_set fds; FD_ZERO (& fds); FD_SET (sock, & fds); struct timeval timeo; timeo. TV _sec = 10; timeo. TV _usec = 1000; int ret = select (sock, & fds, NULL, NULL, & Timeo); if (ret <= 0) break; if (FD_ISSET (sock, & fds) {n_read =: recv (sock, p_buf, n_len, 0 ); if (n_read <= 0) break; p_buf + = n_read; n_len-= n_read; if (n_len = 0) break; const char * rnrn = strstr (p_receive, "\ r \ n"); if (NULL! = Rnrn & rnrn <p_buf) {rnrn + = 4; content = rnrn; if (-1 = n_content_len) {const char * content_length = strstr (p_receive, "Content-Length:"); if (NULL! = Content_length & content_length <p_buf) {content_length + = 16; const char * rn = strstr (content_length, "\ r \ n"); if (NULL! = Rn & rn <p_buf) {int count = rn-content_length; strncpy (temp, content_length, count); temp [count] = '\ 0 '; n_content_len = atoi (temp) ;}} if (-1 = n_content_len) {const char * rn = strstr (rnrn, "\ r \ n"); if (NULL! = Rn & rn <p_buf) {int count = rn-rnrn; strncpy (temp, rnrn, count); temp [count] = '\ 0 '; if (1 = sscanf (temp, "% x", & n_content_len) {n_content_len + = 7; // 0D 0A 30 0D 0A 0D 0A content = rn + 2 ;}} if (-1 = n_content_len) {const char * connection = strstr (p_receive, "Connection: "); if (NULL! = Connection & connection <p_buf) {connection + = 12; const char * rn = strstr (connection, "\ r \ n"); if (NULL! = Rn & rn <p_buf) {int count = rn-connection; strncpy (temp, connection, count); temp [count] = '\ 0 '; connection = _ strupr (temp); if (0 = strcmp (connection, "CLOSE") n_content_len = 0 ;}}} if (NULL! = Content & n_content_len> 0) {n_head_len = content-p_receive; int n_cur_len = p_buf-p_receive; if (n_cur_len >=n_head_len + n_content_len) break ;}}} n_len = n_buf_len-1-n_len; n_buf_len = n_len; p_receive [n_len] = '\ 0';} err =: closesocket (sock); if (SOCKET_ERROR = err) {printf ("[connectAndSendData]: closesocket error % d. \ r \ n ", WSAGetLastError (); }:wsacleanup (); return 0 ;} Int main () {char request_buffer [1024]; request_buffer [0] = '\ 0'; // write the HTTP header information. // The HTPP header can have multiple rows, each row ends with \ r \ n and ends with \ r \ n. strcat (request_buffer, "GET/index. php? Id = 1 HTTP/1.1 \ r \ n "); strcat (request_buffer," Host: 127.0.0.1: 80 \ r \ n "); strcat (request_buffer," Connection: keep-alive \ r \ n "); strcat (request_buffer," Accept: */* \ r \ n "); strcat (request_buffer," \ r \ n "); char evaluate [102400]; unsigned n_receive_len = 102400; receive_buff [0] = '\ 0'; connectAndSendData ("127.0.0.1", 80, request_buffer, strlen (request_buffer), receive_buff, & n_receive_len); printf (receive_buff); getchar (); return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.