The "Go" socket sends data through the HTTP proxy

Source: Internet
Author: User

Copyright Notice: original works, allow reprint, reprint, please be sure to hyperlink form to indicate the original source of the article, author information and this statement. Otherwise, legal liability will be held. http://larch.blog.51cto.com/277889/51991

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

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.