| Recently in writing a Socket based MFC program, requiring clients to use the HTTP proxy to connect to the server. Online check a pile of information, incredibly did not see a few good, is I find the way wrong >_<. Who has the package ready to give me a. In short, sorted out the next pile of data, and then changed the online several examples (mainly "HTTP tunnel (HTTP proxy socket client)" This article, in fact, I also put the inside of the CSocket into a socket, and get out of this pile of things. Socket Socketsend=socket (af_inet,sock_stream,0);//Create Socket Sockaddr_in toaddr; int Comm=proxyip. Find (': '); Proxyip format is Ip:port Toaddr.sin_addr. S_un. S_addr=inet_addr (Proxyip. Left (comm)); Toaddr.sin_family=af_inet; Proxyip=proxyip. Mid (comm+1); Toaddr.sin_port=htons (Atoi (Proxyip)); int Reterr=connect (Socketsend, (sockaddr*) &toaddr,sizeof (TOADDR)); if (reterr==socket_error) { AfxMessageBox ("Connect error"); return 0; } Char tmpbuffer[40]; memset (tmpbuffer,0,40); CString temp; IPAddress for server-side ip,port that need to be connected Temp. Format ("CONNECT%s:%s http/1.1/r/nuser-agent:myapp/0.1/r/n/r/n", ipaddress,port); Send (socketsend,temp. GetBuffer (temp. GetLength ()), temp. GetLength (), 0); Recv (socketsend,tmpbuffer,40,0); Char *tmp=strchr (Tmpbuffer, '); Char flag[4]; for (int i=0;i<3;i++) FLAG[I]=TMP[I+1]; Flag[4]= '/0 '; if (strcmp (flag, "200")!=0) { AfxMessageBox ("Fail to connect proxy!/n"); return 0; } return 1; Here the proxy server is connected successfully, and then the socketsend can be used to send and receive data directly. Like what: Send (Socketsend, "try to send some words", 128,0); Recv (socketsend,buf,128,0); This is the example of blocking mode, using the Windows API, we will look at it, who has a non-blocking mode of trouble to send me a ... In fact, the whole process is very simple: 1.connect Proxy Server 2. Send a package, the format is: CONNECT ip:port http/1.1/r/nuser-agent:myapp/0.1/r/n/r/n The IP and port here are the IP and ports of the server you really need to connect to. 3. Accept the server response, if the response with the word "http/1.x 200", it means the connection is successful. You can use this socket to send data directly later. This article is from the "Larch" blog, please be sure to keep this source http://larch.blog.51cto.com/277889/51991 This article originates from 51CTO. COM Technology Blog |