--TCP Protocol of C # Network programming (I.)

Source: Internet
Author: User

TCP is a connection-oriented transport protocol

Connection-oriented, in fact, like, a call B, if B to listen to, then A and B on the call, is the connection-oriented

TCP is a full-duplex transport protocol

Full duplex, this understanding is also very simple, a call b,b answer the phone, then a can speak to B listen, the same B can also give a speech, it is impossible to only allow a person to speak.

TCP is a point-to-point

Point-to-point, this look at the above example compared to everyone knows, but also to say that if A and B calls in the process, B has an emergency call, then B will be with a call to maintain, so no matter how the same connection can only be point-to-point, not a pair of more.

TCP is reliable data transfer

Reliably, once a connection is established, the transmission of the data must be reachable, and if a says "How are you?" "B won't hear" Hello ", this is reliable data transfer

After talking about the many advantages of TCP, another important partner of TCP: socket (socket)

Well, to tell the truth I don't know how to say socket, perhaps I do not understand the reason, roughly speaking, TCP is the transport layer of the Protocol for data transmission, when we write text messages, SMS window is actually an application, when we send text messages, SMS from the application roll to There is a socket between the transport channel, the application and the transport channel, and without the socket, the application cannot roll the text message to the transport channel. The amount ... Feeling grey is often awkward.

If you want to know what the socket is, please go to C # network programming

The most important thing that a socket contains is two information: connect to the remote local port information (native address and port number), and connect to the remote port information (remote address and port number). NET provides two classes that encapsulate the programming of sockets, the two classes are TcpClient and TcpListener, TcpListener is used to accept connection requests, and TcpClient is used to receive and send stream data. TcpListener continues to keep listening to the port, once a connection request is received, a TcpClient object can be obtained, and the data is sent and received tcpclient to complete. At this point, TcpListener does not stop working, and it always keeps the listening state on the port continuously. ----Zhang Ziyang "C # Network programming (Basic concepts and operations)-Part.1"

Quote a passage as a transition

Thanks to the. Net encapsulation of sockets, we can simply write a simple basic example, about the TCP protocol, will usually initiate the request to connect the client side, the other end of the server

Simple server-side code:

IPAddress IP = ipaddress.parse ("127.1.1.1");//service-side address TcpListener server = new TcpListener (ip,8005);//port number to listen on server. Start ();//Begin Listening

In fact, the server is like an airport, the address of an airport is the address of the server, a landing runway at the airport is the port number of the server monitoring

Client code:

IPAddress IP = ipaddress.parse ("127.1.1.1"); TcpClient client = new TcpClient (); client. Connect (IP, 8005);

Read the above code, it is very clear what is going on, the plane to fly to an airport must first know the address of an airport, and the airport allows the aircraft landing runway number, one and all know, then fly over, land, hey

With the above code, the client can connect to the server, but how does the server know that it is connected to a client?

After the server-side starts listening, you can call AcceptTcpClient () on the TcpListener instance to get a connection to a client that returns an instance of the TcpClient type

TcpClient client = server. AcceptTcpClient ();//server is an example of TcpListener.

TcpListener is like a hotel front desk, can accept many objects of connection, the responsibility is very simple, is to accept the connection request, the connection request object to TcpClient, and then continue to the most self-guided work.

The first study record to this, where insufficient, or wrong place, hope to see can correct, thank you ~

--TCP Protocol of C # Network programming (I.)

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.