The following example shows how to use the Socket class to send data and receive a response to an HTTP server.
[C #]
public string Dosocketget (string server)
{
Sets up variables and a string to write to the server
Encoding ASCII = encoding.ascii;
String get = "Get/http/1.1\r\nhost:" + server +
"\r\nconnection:close\r\n\r\n";
byte[] Byteget = ASCII. GetBytes (get);
byte[] recvbytes = new byte[256];
String strretpage = null;
IPAddress and IPEndPoint represent the endpoint that would
Receive the request.
Get the "the" in the list using DNS.
IPAddress Hostadd = dns.resolve (server). ADDRESSLIST[0];
IPEndPoint ephost = new IPEndPoint (Hostadd, 80);
Creates the Socket for sending data over TCP.
Socket s = new socket (AddressFamily.InterNetwork, SocketType.Stream,
PROTOCOLTYPE.TCP);
Connects to the host using IPEndPoint.
S.connect (Ephost);
if (!s.connected)
{
Strretpage = "Unable to connect to host";
return strretpage;
}
Sends the get-text to the host.
S.send (Byteget, Byteget.length, Socketflags.none);
Receives the page, looping until all bytes are received
Int32 bytes = s.receive (recvbytes, recvbytes.length, 0);
Strretpage = "Default HTML page on" + server + ": \ r \ n";
Strretpage = Strretpage + ASCII. GetString (recvbytes, 0, bytes);
while (bytes > 0)
{
bytes = S.receive (recvbytes, Recvbytes.length, Socketflags.none);
Strretpage = Strretpage + ASCII. GetString (recvbytes, 0, bytes);
}
Call S.close () if you want to close the connection now;
return strretpage;
}
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