Socket programming (1)

Source: Internet
Author: User

The Socket () function can be used to create two types of sockets: byte stream Socket and datagram Socket.

Byte stream socket: Provides bidirectional, ordered, non-repetitive, and non-record-Free Data Stream Services.

Datagram socket: provides two-way data streams, but it is not guaranteed to be reliable, ordered, and non-duplicated.

Applications based on the TCP protocol (transmission control protocol) and UDP protocol (user data protocol) use byte stream sockets and datagram sockets respectively.

 

Differences Between TCP and UDP:

1. TCP connection-oriented, while UDP connection-oriented

2. The two have different reliable transmission mechanisms for messages. TCP has a dedicated reliable message transmission mechanism. When the receiver receives a message sent by the sender, it automatically returns a message notifying the sender that the message has been received. After the sender receives the message and successfully confirms the message, or wait until the message is confirmed. However, UDP does not have such a mechanism. It only keeps transmitting data and will not be detected when data is lost.

 

 UDP programming mode (using System. Net and using System. Net. Sockets)

 

Server:

Client

1 UdpClient listener = new UdpClient (listenport); // provides UDP Network Services
2 IPEndPoint groupIP = new IPEndPoint (IPAddress. Any, 100); // accept Any IP address, network endpoint with port number 100
3 try
4 {
5 // infinite loop, waiting for listening Information
6 while (true)
7 {
8 byte [] bytes = listener. Receive (ref groupIP); // accept information from port 100
9 string strInfo = Encoding. GetEncoding ("gb2312"). GetString (bytes, 0, bytes. Length );
10 MessageBox. Show (strInfo );
11}
12}
13 catch (Exception e)
14 {
15 Console. WriteLine (e. ToString ());
16}
17 finally
18 {
19 listener. Close ();
20}

Call the StartListen () method in Form1_Load.

Client:

Server

1 Socket s = new Socket (AddressFamily. InterNetwork, SocketType. Dgram, ProtocolType. Udp); // declare the UDP Socket interface
2 IPAddress broadcast = IPAddress. Parse (tbxIP. Text. ToString (); // convert the Server IP address entered in TextBox to IPAddress.
3 byte [] sendbuf = Encoding. GetEncoding ("gb2312"). GetBytes (tbxContent. Text. ToString (); // convert the input content in the TextBox to the byte [] Type
4 IPEndPoint ep = new IPEndPoint (broadcast, 100); // instantiate the network endpoint with the specified address and port number. The port number must be the same as the port in the server code.
5 s. SendTo (sendbuf, ep); // call the SendTo Method

In this way, you can achieve

 

Author: Yu Yi

Source: http://cnblogs.com/zabery

The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

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.