. NET Learning Note----2015-07-06 (socket concept)

Source: Internet
Author: User

Socket in English: "Hole" or "socket"

As a process communication mechanism, take a later meaning. Usually also called "socket", which describes the IP address and port number, is a handle to a communication chain. (It's just two programs for communication)

Sockets are very similar to telephone sockets

Take a telephone network as an example. The telephone calls the two sides equal the exchange information the 2 procedure, the telephone number is the IP address. Any user before the call, the first to occupy a telephone, the equivalent of applying for a socket;

At the same time, to know the other person's number, the equivalent of a fixed socket.

The call is then dialed to the other side, which is the equivalent of making a connection request.

If the other party is present and free, pick up the telephone handset, both sides can formally call, the equivalent of a successful connection. The process of the two sides of the call is the direction of the telephone signal and the other side from the telephone to receive the signal process,

Equivalent to sending data to the socket and receiving data from the socket.

After the call ends, one side hangs up the phone, which is the equivalent of closing the socket and revoking the connection.

1), the server welcome Socket start Listening Port (responsible for listening to client connection information)

2), client clients socket connection server designated port (responsible for receiving and sending service-side messages)

3), the service end welcome Socket monitor Hear the client connection, create correction socket. (responsible for communicating with clients)

There are many such hosts on the internet, which typically run multiple service software while providing several services. Each service opens a socket and binds to a port, and the different ports correspond to different services (applications)

For example: HTTP using port 80, FTP using 21 port SMTP using 25 port

Two types: 50000

Streaming socket (stream):

A connection-oriented socket that is secure, but inefficient, for connection-oriented TCP service applications

Datagram Socket (DATAGRAM):

A non-connected socket, which corresponds to a non-connected UDP service Application, is unsafe (data loss, order confusion, analysis of rearrangement at the receiving end and re-sending required), but high efficiency.

Socket Communication Basic Flowchart

Example:

         PublicForm1 () {InitializeComponent (); }        Private voidComboBox1_SelectedIndexChanged (Objectsender, EventArgs e) {        }        Private voidbtnStart_Click (Objectsender, EventArgs e) {            //When you click Start listening, create a socket on the server that is responsible for listening for the IP address and port numberSocket Socketwatch =Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); IPAddress IP=Ipaddress.any; //Create a port number objectIPEndPoint point =Newipendpoint (IP, Convert.ToInt32 (txtport.text)); //MonitorSocketwatch.bind (point); ShowMsg ("Monitor Successful"); Socketwatch.listen (Ten); Thread th=NewThread (Listen); Th. IsBackground=true; Th.                                Start (Socketwatch); }        /// <summary>        ///loops through the client's connection and creates a socket for communication with it/// </summary>        voidListen (Objecto) {Socket socketwatch= O asSocket; //wait for the client to connect and create a socket that is responsible for communication             while(true) {Socket socketsend=socketwatch.accept (); ShowMsg (socketSend.RemoteEndPoint.ToString ()+":"+"Connection Successful"); }        }        /// <summary>        ///append text, log list, append to see messages sent by client/// </summary>        /// <param name= "str" ></param>        voidShowMsg (stringstr) {Txtlog.appendtext (str+"\ r \ n"); }        Private voidForm1_Load (Objectsender, EventArgs e) {Control.checkforillegalcrossthreadcalls=false; }    }

. NET Learning Note----2015-07-06 (socket concept)

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.