C# Simple Socket Programming In Console - 간단한 콘솔 소켓 통신
C# 2021. 12. 22. 21:12 |반응형
콘솔에서 간단한 메세지를 주고 받는 서버, 클라이언트를 만들어 보자.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace Server
{
class Program
{
static void Main(string[] args)
{
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.100"), 7777);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine("■ Server Info:");
Console.WriteLine("IP Address : {0}", serverEndPoint.Address);
Console.WriteLine("Port Number: {0}", serverEndPoint.Port);
Console.WriteLine("AddressFamily : {0}", serverEndPoint.AddressFamily);
// Associates a Socket with a local endpoint.
server.Bind(serverEndPoint);
// Places a Socket in a listening state.
// Parameter: The maximum length of the pending connections queue.
server.Listen(5);
Console.WriteLine("Waiting for a client...");
// Creates a new Socket for a newly created connection.
Socket client = server.Accept();
IPEndPoint clientEndPoint = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Client connected: {0}", clientEndPoint.Address);
byte[] sendBuff = Encoding.UTF8.GetBytes("Connected to the server.");
// Sends the specified number of bytes of data to a connected Socket, using the specified SocketFlags.
client.Send(sendBuff, sendBuff.Length, SocketFlags.None);
byte[] recvBuff = new byte[256];
// Receives data from a bound Socket into a receive buffer.
// Return: The number of bytes received.
if (client.Receive(recvBuff) != 0)
{
Console.WriteLine("Message from a client: " + Encoding.UTF8.GetString(recvBuff));
} else
{
Console.WriteLine("No message.");
}
// Closes the Socket connection and releases all associated resources.
client.Close();
server.Close();
}
}
}
|
서버 소스
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
32
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace Client
{
class Program
{
static void Main(string[] args)
{
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.100"), 7777);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Establishes a connection to a remote host.
client.Connect(serverEndPoint);
Console.WriteLine("Connecting to the server...");
byte[] recvBuff = new byte[256];
client.Receive(recvBuff);
Console.WriteLine("Message from the server: " + Encoding.UTF8.GetString(recvBuff));
client.Send(Encoding.UTF8.GetBytes("Hello."));
client.Close();
}
}
}
|
클라이언트 소스
반응형
'C#' 카테고리의 다른 글
C# TCP/IP Image transfer - 이미지(파일) 전송 1 (0) | 2021.12.23 |
---|---|
C# Desktop(Screen) Capture - 스크린 캡쳐 (0) | 2021.12.23 |
C# IP Addresses and Hostname - IP 주소 호스트 네임 확인 (0) | 2021.12.22 |
C# Default Value - 기본값 (0) | 2021.12.19 |
C# typeof - 타입 정보 확인 (0) | 2021.12.15 |