Server/Study

[Server] Socket

GiveZero 2023. 5. 1. 18:22
  1. 소켓 준비
  2. 서버 주소로 Connet
    소켓을 통해 Session 소켓과 패킷 송수신 가능
  3. Listener 소켓 준비
  4. Bind ( 서버 주소, Port 소켓 연동)
  5. Listen
  6. Accept

=> 클라 세션을 통해 사용자와 연결 가능

 


Server

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace ServerCore
{
    class Program
    {
        static void Main(string[] args)
        {
            //DNS (Domain Name System)
            string host = Dns.GetHostName();
            IPHostEntry ipHost = Dns.GetHostEntry(host);
            IPAddress ipAddr = ipHost.AddressList[0];
            IPEndPoint endPoint = new IPEndPoint(ipAddr, 7777);
            
            // Listener
            Socket listenSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                // Bind
                listenSocket.Bind(endPoint);
            
                // Listen
                // backlog : 최대 대기 수 
                listenSocket.Listen(10);

                while (true)
                {
                    Console.WriteLine("Listening...");
                
                    // Accept
                    Socket clientSocket = listenSocket.Accept();
                
                    // 수신
                    byte[] recvBuff = new byte[1024];
                    int recvBytes = clientSocket.Receive(recvBuff); 
                    string recvData = Encoding.UTF8.GetString(recvBuff, 0, recvBytes);
                    Console.WriteLine($"[From Client] {recvData}");

                    // 발신
                    byte[] sendBuff = Encoding.UTF8.GetBytes("Welcome to YoungJune Server!");
                    clientSocket.Send(sendBuff);

                    // 퇴장
                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

Client

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            //DNS (Domain Name System)
            string host = Dns.GetHostName();
            IPHostEntry ipHost = Dns.GetHostEntry(host);
            IPAddress ipAddr = ipHost.AddressList[0];
            IPEndPoint endPoint = new IPEndPoint(ipAddr, 7777);
            
            // 설정
            Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                // 연결
                socket.Connect(endPoint);
                Console.WriteLine($"Connected To {socket.RemoteEndPoint.ToString()}");
            
                // 발신
                byte[] sendBuff = Encoding.UTF8.GetBytes("Hi YoungJune Server");
                int sendBytes = socket.Send(sendBuff);

                // 수신
                byte[] recvBuff = new byte[1024];
                int recvBytes = socket.Receive(recvBuff);
                string recvData =Encoding.UTF8.GetString(recvBuff, 0, recvBytes);
                Console.WriteLine($"[From Server] {recvData}");
            
                // 퇴장
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

클라이언트 접속 (7번 접속)

서버