Console Browser Code Combat

Source: Internet
Author: User

This article code just to be able to deeply understand the network communication, without the code back out, only need to understand, can change on the line

First you need to create a socket

Note the need to write in VS2010: New Socket (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);
Socket socket = new socket (SocketType.Stream, protocoltype.tcp),//tcp, UDP.
Socket. Connect (new Dnsendpoint ("127.0.0.1", 8080));//Connect to the server. The default port number for the HTTP protocol is 80. Each server software listens to a port (other software can not listen to this port), the data sent to this port will only be received by this server software.
using (NetworkStream NetStream = new NetworkStream (socket))//read/write the stream of socket communication data
using (StreamWriter writer = new StreamWriter (NetStream))
{
Writer. WriteLine ("get/index.html http/1.1");//Enter each line of instructions
Writer. WriteLine ("host:127.0.0.1:8080");
Writer. WriteLine ();//empty line carriage return, indicating end of instruction
}
using (NetworkStream NetStream = new NetworkStream (socket))
using (StreamReader reader = new StreamReader (NetStream))
{
String line;
while (line = reader. ReadLine ())!=null)
{
Console.WriteLine (line);
}
}
Socket. Disconnect (FALSE);

Console Browser Code Combat

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.