C # network application | understanding Scoket protocol | beginner's tutorial

Source: Internet
Author: User

Using System;

Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Net;
Using System. Net. Sockets;


Understanding Scoket protocol in namespace
{
Class synchronous socket
{
Static void Main (string [] args)
{
/// Synchronous socket technology is used to implement network communication in five aspects: Host resolution, host binding, port listening, data receiving, and data sending.
}
Public void defines host objects ()
{
// ① Define the Host Object (two methods are available)
// The first method is to use IPEndPoint to define host objects.
IPEndPoint myServer1 = new IPEndPoint (12345, 80 );
// The second type uses the IPAddress class, which is used to convert the string to the IP address format
IPAddress myIP = IPAddress. Parse ("127.0.0.1 ");
IPEndPoint myServer2 = new ipendpoints (myIP, 8080 );
}
Public void host resolution ()
{
// ② Host resolution (use the DNS server to resolve the host before binding the host)
// The following code uses the Dns GetHostEntry () method to parse remote google hosts
IPHostEntry myHost = new IPHostEntry ();
MyHost = Dns. GetHostEntry ("www.2cto.com ");
// Use the AddressList () method of the IPHostEntry object to obtain the IP address list associated with the host. The code for obtaining the IP address list is as follows:
For (int I = 0; I <myHost. AddressList. Length; I ++)
{
MyHost. AddressList [I]. ToString ();
}
// Use the HostName () method of the IPHostEntry object to obtain the host's DNS name
}
Public void port binding and listening ()
{
// ③ Both port binding and listening use the Socket class. The method Bind () indicates binding the communication host, and the method Listen () indicates listening.
// The following code uses the Dns GetHostEntry () method to parse remote google hosts
IPAddress myIP = IPAddress. Parse ("127.0.0.1 ");
IPEndPoint myServer = new IPEndPoint (myIP, 8080 );
// Create a Socket object
Socket socket = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );
// Bind the host
Socket. Bind (myServer );
// Listen
Socket. Listen (123 );
}
Public void data transmission ()
{
// ④ After binding host ports and listeners, you must use the Socket-class Accept () method to receive client connection requests;
// In this way, the client and the server establish a connection, and the data sending uses the Send () method of the Socket class. The Code is as follows:
IPAddress myIP = IPAddress. Parse ("127.0.0.1 ");
IPEndPoint myServer = new IPEndPoint (myIP, 8080 );
// Create a Socket object
Socket socket = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );
// Bind the host
Socket. Bind (myServer );
// Listen
Socket. Listen (123 );
Socket mySend = socket. Accept ();


// Define the sent message
Byte [] myByte = new Byte [64];
String message = "send a message! ";
MyByte = System. Text. Encoding. BigEndianUnicode. GetBytes (message. ToCharArray ());
// Send
MySend. Send (myByte, myByte. Length, 0 );


}
Public void data receiving ()
{
// ⑤ The Receive () method of the Socket class can be used to Receive data. The Code is as follows:
IPAddress myIP = IPAddress. Parse ("127.0.0.1 ");
IPEndPoint myServer = new IPEndPoint (myIP, 8080 );
// Create a Socket object
Socket socket = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );
// Bind the host
Socket. Bind (myServer );
// Listen
Socket. Listen (123 );
Socket mySend = socket. Accept ();


// Define the receiving format
Byte [] myByte = new Byte [64];
// Receive
MySend. Receive (myByte, myByte. Length, 0 );
// Format conversion
String msg = System. Text. Encoding. BigEndianUnicode. GetString (myByte );
}
}
Class asynchronous socket
{
/// Asynchronous sockets are the same as synchronous sockets in five aspects: Host parsing, host binding, port listening, data receiving, and data sending.
/// Host resolution, host binding, and port listening are the same, so we will not detail them here. The following describes how to receive and send data.
Public void data transmission ()
{
/// Unlike synchronous sockets, asynchronous sockets use the BeginSend () method of the Socket class. This method has six parameters.
/// The first parameter is a byte array.
/// The second parameter is the start position of sending.
/// The third parameter is the number of bytes sent.
/// The fourth parameter is the bitwise combination of the SocketFlags value.
/// The fifth parameter is asynchronous callback.
/// The sixth parameter is a custom object.
}
Public void data receiving ()
{
/// Unlike synchronous sockets, asynchronous sockets use the BeginReceive () method of the Socket class. This method has six parameters.
/// The first parameter is a byte array.
/// The second parameter is the start position of sending.
/// The third parameter is the number of bytes sent.
/// The fourth parameter is the bitwise combination of the SocketFlags value.
/// The fifth parameter is asynchronous callback.
/// The sixth parameter is a custom object.
}
}
}
Author: bugDemo

Related Article

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.