c# 간단한 컨트롤러udp 비동기 통신 프로그램 구현 예시

5787 단어
클라이언트 발송 요청, 서버 측 응답 메커니즘 실현
UDP 클라이언트 코드
 
  
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Client
{
    class Program
    {
        // Socket
        private static Socket clientSocket;
        //
        private static EndPoint epServer;
        //
        private static byte[] receiveData;

        public static void Main(string[] args)
        {
            // Socket
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            // IP
            IPEndPoint server = new IPEndPoint(IPAddress.Parse("192.168.1.165"), 11000);
            //
            epServer = (EndPoint)server;
            string msg;     //
            byte[] sendData;    //
            while (true) {
                msg = Console.ReadLine();   //
                if (msg == "exit") break;   // “exit” ,
                // ASCII ,
                // , UTF-8
                sendData = Encoding.ASCII.GetBytes(msg);   
                //
                // :sendData          
                // :0:                
                // :sendData.Length:  
                // :SocketFlags.None: 
                // :epServer:          ( IP )
                // :new AsyncCallback(SendData):  
                // :null:         
                clientSocket.BeginSendTo(sendData, 0, sendData.Length, SocketFlags.None,
                    epServer, new AsyncCallback(SendData), null);
                //
                // ,
                // ,
                // : “ ”。 “ ”。
                // :“ ”。
                receiveData = new byte[1024];
                //
                // ,
                clientSocket.BeginReceiveFrom(receiveData, 0, receiveData.Length, SocketFlags.None,
                    ref epServer, new AsyncCallback(ReceiveData), null);
            }
        }

        //
        private static void SendData(IAsyncResult iar)
        {
            //
            clientSocket.EndSend(iar);
        }

        //
        private static void ReceiveData(IAsyncResult iar)
        {
            //   recv
            int recv = clientSocket.EndReceive(iar);
            //
            Console.WriteLine("Server: " + Encoding.ASCII.GetString(receiveData, 0, recv));
        }

    }
}


UDP 서버측 코드
 
  
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Server
{
    class AsyncUdpServer
    {
        // Socket
        private static Socket serverSocket;
        //
        private static byte[] receiveData = new byte[1024];

        public static void Main(string[] args)
        {
            // Socket
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            // IP ,IPAddress.Any :0.0.0.0, , IP
            IPEndPoint server = new IPEndPoint(IPAddress.Any, 11000);
            //Socket IP
            serverSocket.Bind(server);
            // IP , 0
            IPEndPoint clients = new IPEndPoint(IPAddress.Any, 0);
            //
            EndPoint epSender = (EndPoint)clients;
            //   ,epSender IP
            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)
        {
            // IP , 0
            IPEndPoint client = new IPEndPoint(IPAddress.Any, 0);
            //
            EndPoint epSender = (EndPoint)client;
            //   recv
            int recv = serverSocket.EndReceiveFrom(iar, ref epSender);           
            // , ,
            Console.WriteLine("Client:" + Encoding.ASCII.GetString(receiveData, 0, recv));
            // , ASCII ,
            // , UTF-8           
            byte[] sendData = Encoding.ASCII.GetBytes("hello");
            //   epSender IP
            serverSocket.BeginSendTo(sendData, 0, sendData.Length, SocketFlags.None,
                epSender, new AsyncCallback(SendData), epSender);
            //
            receiveData = new byte[1024];
            // , ,
            serverSocket.BeginReceiveFrom(receiveData, 0, receiveData.Length, SocketFlags.None,
                ref epSender, new AsyncCallback(ReceiveData), epSender);
        }

    }
}

좋은 웹페이지 즐겨찾기