- 서버
using System.Net;
using System.Net.Sockets;
using System.Net.WebSockets;
namespace Server;
internal class Program
{
static void Main(string[] args)
{
// param: 주소체계, 소켓타입
// Stream 은 연결 지향을 의미
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//엔드 포인트
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.168.30.1"), 20000);
//serverSocket에 ip와 port 할당
serverSocket.Bind(endPoint);
// Listen의 역할
// 1. 클라이언트들의 연결 요청을 대기상태로 변경
// 2. 백로그큐 생성 => 클라이언트들의 연결 요청 대기실
// 최대 20개의 클라이언트 연결요청을 대기할 수 있다.
serverSocket.Listen(20);
// 3. 대기실에서 연결 요청중인 클라이언트 중 하나에 연결요청을 수락
// 클라이언트와 데이터 통신을 위한 소켓을 만든다.
Socket clientSocket = serverSocket.Accept();
// 클라이언트 ip와 port 확인
Console.WriteLine("연결성공 : " + clientSocket.RemoteEndPoint);
}
}
- 클라이언트
using System.Net;
using System.Net.Sockets;
namespace Client;
internal class Program
{
static void Main(string[] args)
{
// 1. 클라이언트 소켓 생성
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.168.30.1"), 20000);
// 2. 커넥트하면 서버소켓의 백로그큐에 들어가서
// 클라이언트가 연결요청을 대기하게 된다.
clientSocket.Connect(endPoint);
}
}
'웹 개발 > 네트워크' 카테고리의 다른 글
[C#] TCP/IP 소켓 프로그래밍_handshake (0) | 2023.07.27 |
---|---|
[C#] TCP/IP 소켓 프로그래밍_ echo 프로그램 (0) | 2023.07.20 |
[C#] TCP/IP 소켓 프로그래밍_ 직렬화, 바이트 오더 (0) | 2023.07.20 |
[C#] TCP/IP 소켓 프로그래밍_ 소켓 기초용어 정리 (0) | 2023.07.15 |
[C#] TCP/IP 소켓 프로그래밍_ 네트워크 기초용어 정리 (0) | 2023.07.15 |