1. Preface
A simple understanding of the HTTP protocol, in fact, is to send the data is packaged (many of the headers representing various attributes)
This link learns to write an HTTP server: Ultra-Lightweight HTTP server tinyhttpd
HTTP protocol messages
2. Environment
My computer is configured with the apache+php development environment, using 80 ports
3. Code
Client
ConnectToPHPServer.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <stdio.h> #include <winsock.h> #include <string.h> #pragma comment (lib , "Ws2_32.lib") #define PORT 80#define SIZE 512#define max_size 1024void buildgetheader (char* header, int SIZE, char* host , char* Res) {memset (header,0,size); strcat (header, "GET"); strcat (header,res); strcat (header, "http/1.1\r\n"); Strcat ( Header, "HOST:"); strcat (header,host); strcat (header, "\r\nconnection:keep-alive\r\n\r\n"); return;} void Buildpostheader (char* header, int size, char* host, char* res) {memset (header,0,size); strcat (header, "POST"); strcat (header,res); strcat (header, "http/1.1\r\n"); Strcat (header, "HOST:"); strcat (header,host); strcat (header, "\r\ Ncontent-type:application/x-www-form-urlencoded\r\n "); strcat (header, "content-length:8\r\n"), strcat (header, "connection:keep-alive\r\n\r\n"); Strcat (header, "user=cjc\r \n\r\n "); return;} void SendData (SOCKET serversocket, char* data) {Send (ServerSocket, data, strlen (data), 0);} void gEtdata (SOCKET serversocket, char* data, int size) {int Num=0;char buf[size];memset (data,0,max_size); memset (buf,0,size) ; while (recv (serversocket,buf,size,0) >0) {strcat (DATA,BUF); memset (buf,0,size);} return;} void parseURL (Char*url, char*& Host, char*& res) {char* p;int num=0;p=url+7;while (*p!= '/' && *p!= ' + ') {p ++;} if (strlen (p)!=0) {Num=strlen (P) +1;res= (char*) malloc (num); memset (Res,0,num); strcpy (res,p);} Else{res= (char*) malloc (2); memset (res,0,2); res[0]= '/';} *p= ' + '; host=url+7;return;} void Connecttohost (char* host, socket& ServerSocket, char* res) {wsadata wsadata={0};struct sockaddr_in addr;struct hostent* Phost;char header[size],buf[max_size];if (WSAStartup (Makeword (2,2), &wsadata)) {printf ("WSASartup failed!\n "); System (" pause "); exit (-1);} Serversocket=socket (AF_INET,SOCK_STREAM,IPPROTO_TCP);p host=gethostbyname (host); addr.sin_family=af_inet; Addr.sin_addr. S_un. s_addr=* (unsigned long*) phost->h_addr); addr.sin_port=htons (port); Connect (ServerSocket, (sockaddr*) &addr, SizEOF (addr));p rintf ("Connect to host ok!\n"); return;} int main (void) {char url[100]= "http://localhost/Workspace/FirstPHP/TestPost.php"; char *host,*res,header[size],buf[ Max_size]; SOCKET Serversocket;parseurl (url,host,res); Connecttohost (host,serversocket,res);/*buildgetheader (Header,SIZE, Host,res); SendData (Serversocket,header); GetData (serversocket,buf,max_size);p rintf ("%s\n", buf); */ Buildpostheader (header,size,host,res); SendData (Serversocket,header); GetData (serversocket,buf,max_size);p rintf ( "%s\n", buf); closesocket (ServerSocket); WSACleanup (); System ("pause"); return 0;}
Server
<?phpif (isset ($_post[' user ')) {echo $_post[' user '];}? >
4. Results
If successful, the client side dox the end of the execution window will output CJC words
The C language socket uses the HTTP protocol to access the Apache server