Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

For me

[Server] Socket 본문

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번 접속)

서버 

'Server > Study' 카테고리의 다른 글

[Server] Socket - Listener 분리  (0) 2023.05.01
[Server] Thread Local Storage  (0) 2023.04.26
[Server] ReaderWriterLock  (0) 2023.04.26
[Server] AutoResetEvent  (0) 2023.04.26
[Server] Context Switching  (0) 2023.04.26