c implements a simple console example of udp asynchronous communicator
- 2020-06-19 11:35:23
- OfStack
Realize the client to send the request, the server side response mechanism
UDP client code
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Client
{
class Program
{
// The client Socket object
private static Socket clientSocket;
// The server side At the end of
private static EndPoint epServer;
// An array of characters to receive data
private static byte[] receiveData;
public static void Main(string[] args)
{
// The client Socket Object instantiation
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Set up the server side IP Address and corresponding port
IPEndPoint server = new IPEndPoint(IPAddress.Parse("192.168.1.165"), 11000);
// Instantiate the server side At the end of
epServer = (EndPoint)server;
string msg; // The message to send
byte[] sendData; // The string to send
while (true) {
msg = Console.ReadLine(); // Enter the message to send
if (msg == "exit") break; // When the input" exit ", exit the client program
// Pass the message through ASCII The encoding is converted to an array of characters,
// If Chinese characters or other special symbols are to be sent, they can be used UTF-8
sendData = Encoding.ASCII.GetBytes(msg);
// Start sending messages asynchronously
// Parameters: sendData The data to be sent
// Parameters: 0 : The starting location of the data to be sent
// Parameters: sendData.Length : The number of bytes to send data
// Parameters: SocketFlags.None : Combination by bit
// Parameters: epServer : Receiver equipment (including IP And port)
// Parameters: new AsyncCallback(SendData): entrust
// Parameters: null : The status information of the request
clientSocket.BeginSendTo(sendData, 0, sendData.Length, SocketFlags.None,
epServer, new AsyncCallback(SendData), null);
// Instantiates an array of characters that receives data
// If it was already initialized at the time of the declaration, it is still reinitialized here
// When the length of data received last time is larger than this time, the array contains residual data received last time
// For example: last time I received "You little joke". This time receive "joke".
// Then the data in the array is: "Joking force".
receiveData = new byte[1024];
// Start receiving messages asynchronously
// The parameter section corresponds to the asynchronous send section, basic 1 to
clientSocket.BeginReceiveFrom(receiveData, 0, receiveData.Length, SocketFlags.None,
ref epServer, new AsyncCallback(ReceiveData), null);
}
}
// The delegate function that sends the message asynchronously
private static void SendData(IAsyncResult iar)
{
// Complete asynchronous sending
clientSocket.EndSend(iar);
}
// The delegate function that receives the message asynchronously
private static void ReceiveData(IAsyncResult iar)
{
// Complete asynchronous reception recv Represents the number of bytes received
int recv = clientSocket.EndReceive(iar);
// Print out the data received
Console.WriteLine("Server: " + Encoding.ASCII.GetString(receiveData, 0, recv));
}
}
}
UDP server-side code
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Server
{
class AsyncUdpServer
{
// The server side Socket object
private static Socket serverSocket;
// An array of characters to receive data
private static byte[] receiveData = new byte[1024];
public static void Main(string[] args)
{
// Instantiate the server side Socket object
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Server side IP And port, IPAddress.Any The actual is: 0.0.0.0 , means arbitrary, and basically means native IP
IPEndPoint server = new IPEndPoint(IPAddress.Any, 11000);
//Socket Object and server side IP And port binding
serverSocket.Bind(server);
// The client's IP And port, port 0 Represents any port
IPEndPoint clients = new IPEndPoint(IPAddress.Any, 0);
// Instantiate the client At the end of
EndPoint epSender = (EndPoint)clients;
// Start receiving messages asynchronously After receiving, epSender The storage is for the sender IP And port
serverSocket.BeginReceiveFrom(receiveData, 0, receiveData.Length, SocketFlags.None,
ref epSender, new AsyncCallback(ReceiveData), epSender);
Console.WriteLine("Listening...");
Console.ReadLine();
}
private static void SendData(IAsyncResult iar)
{
serverSocket.EndSend(iar);
}
private static void ReceiveData(IAsyncResult iar)
{
// The client's IP And port, port 0 Represents any port
IPEndPoint client = new IPEndPoint(IPAddress.Any, 0);
// Instantiate the client At the end of
EndPoint epSender = (EndPoint)client;
// End asynchronous receiving messages recv Represents the number of characters received
int recv = serverSocket.EndReceiveFrom(iar, ref epSender);
// Print out the received data, and what encoding is used by the sender, and what encoding is used here Convert to a string
Console.WriteLine("Client:" + Encoding.ASCII.GetString(receiveData, 0, recv));
// Defines a message to send back to the client ASCII Coding,
// If Chinese characters or other special symbols are to be sent, they can be used UTF-8
byte[] sendData = Encoding.ASCII.GetBytes("hello");
// Start sending messages asynchronously epSender Is the client that received the message the last time IP And port information
serverSocket.BeginSendTo(sendData, 0, sendData.Length, SocketFlags.None,
epSender, new AsyncCallback(SendData), epSender);
// Reinstantiate the received data byte array
receiveData = new byte[1024];
// Start receiving messages asynchronously, and the delegate function here is the function itself, recursively
serverSocket.BeginReceiveFrom(receiveData, 0, receiveData.Length, SocketFlags.None,
ref epSender, new AsyncCallback(ReceiveData), epSender);
}
}
}