2丶 TCP 비동기식 연결

서버
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace TcpServer
{
    class Program
    {
        static byte[] dataBuffer = new byte[1024];
        static void Main(string[] args)
        {
            StartServerAsync();
            Console.ReadKey();
        }
        static void StartServerAsync()
        {
            Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress iPAddress = IPAddress.Parse("192.168.200.1");
            IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, 88);
            socketServer.Bind(iPEndPoint);

            socketServer.Listen(10);
            Socket clientSocket = socketServer.Accept();
            string msg = "Hello";
            clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(msg));
            clientSocket.BeginReceive(dataBuffer,0,1024,SocketFlags.None,ReceiveCallBack, clientSocket);
        }

        static void ReceiveCallBack(IAsyncResult ar)
        {
            //            ar.AsyncState  object
            Socket clientSocket = ar.AsyncState as Socket;
            int count=clientSocket.EndReceive(ar);
            string msgReceive=Encoding.UTF8.GetString(dataBuffer, 0, count);
            Console.WriteLine(msgReceive);
            clientSocket.BeginReceive(dataBuffer, 0, 1024, SocketFlags.None, ReceiveCallBack, clientSocket);
        }

        void StartServerSync()
        {

            Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPAddress iPAddress = IPAddress.Parse("192.168.200.1");
            IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, 88);
            socketServer.Bind(iPEndPoint);

            socketServer.Listen(10);
            Socket clientSocket = socketServer.Accept();
            string msg = "Hello";
            clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(msg));
            byte[] databuffer = new byte[1024];
            int count = clientSocket.Receive(databuffer);
            string megReceive = System.Text.Encoding.UTF8.GetString(databuffer, 0, count);
            Console.Write(databuffer);
            clientSocket.Close();
            socketServer.Close();

        }
    }
}

클라이언트
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace TcpClinet
{
    class Program
    {
        static byte[] data = new byte[1024];
        static void Main(string[] args)
        {
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSocket.Connect(new IPEndPoint(IPAddress.Parse("192.168.200.1"),88));

            int count=clientSocket.Receive(data);
            string msg = System.Text.Encoding.UTF8.GetString(data, 0, count);
            Console.WriteLine(msg);
            while (true)
            {
                string s = Console.ReadLine();
                clientSocket.Send(Encoding.UTF8.GetBytes(s));
            }
            Console.ReadKey();
            clientSocket.Close();
        }

    }
}

이제 서버에서 여러 클라이언트 메시지를 받을 수 있습니다.
(이전에 내가 쓴 C++ 서버에 비해 epoll로 연결해야 하는데, C#은 너무 간단하다)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace TcpServer
{
    class Program
    {
        static byte[] dataBuffer = new byte[1024];
        static void Main(string[] args)
        {
            StartServerAsync();
            Console.ReadKey();
        }
        static void StartServerAsync()
        {
            Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress iPAddress = IPAddress.Parse("192.168.200.1");
            IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, 88);
            socketServer.Bind(iPEndPoint);

            socketServer.Listen(10);
            //Socket clientSocket = socketServer.Accept();  , ,  c++ epoll 
            socketServer.BeginAccept(AcceptCallBack, socketServer); 
        }

        static void AcceptCallBack(IAsyncResult ar)
        {
            Socket socketServer = ar.AsyncState as Socket;
            Socket socketClient = socketServer.EndAccept(ar);

            string msg = "Hello";

            socketClient.Send(System.Text.Encoding.UTF8.GetBytes(msg)); 

            socketClient.BeginReceive(dataBuffer, 0, 1024, SocketFlags.None, ReceiveCallBack, socketClient);
            socketServer.BeginAccept(AcceptCallBack, socketServer);
        }

        static void ReceiveCallBack(IAsyncResult ar)
        {
            //            ar.AsyncState  object
            Socket clientSocket = ar.AsyncState as Socket;
            int count=clientSocket.EndReceive(ar);
            string msgReceive=Encoding.UTF8.GetString(dataBuffer, 0, count);
            Console.WriteLine(msgReceive);
            clientSocket.BeginReceive(dataBuffer, 0, 1024, SocketFlags.None, ReceiveCallBack, clientSocket);
        }

        void StartServerSync()
        {

            Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPAddress iPAddress = IPAddress.Parse("192.168.200.1");
            IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, 88);
            socketServer.Bind(iPEndPoint);

            socketServer.Listen(10);
            Socket clientSocket = socketServer.Accept();
            string msg = "Hello";
            clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(msg));
            byte[] databuffer = new byte[1024];
            int count = clientSocket.Receive(databuffer);
            string megReceive = System.Text.Encoding.UTF8.GetString(databuffer, 0, count);
            Console.Write(databuffer);
            clientSocket.Close();
            socketServer.Close();

        }
    }
}

좋은 웹페이지 즐겨찾기