ํด๋น ๊ฒ์๊ธ์ ์๋ ๊ฐ๋ฐํ๊ฒฝ์ ์ฌ์ฉํ์ฌ ์์ฑ๋จ
IDE : VisualStudio 2022
Framework : .NET 7.0
Language : C# 11
1. TCP
- TCP์ ๋ํ ๋ด์ฉ์ ์๋ ๊ฒ์๊ธ์ ์ฐธ๊ณ
[C#] ๋คํธ์ํฌ ํต์ - TCP&UDP
1. ์ฉ์ด์ ๋ฆฌ ์์ผ (Socket) : ๋คํธ์ํฌ ์์์ ๋์๊ฐ๋ ํ๋ก๊ทธ๋จ์ ์๋ฐฉํฅ ํต์ ์ ์๋ ํฌ์ธํธ ์์ผํต์ : ์์ผ์ ํตํด ์๋ฒ-ํด๋ผ์ด์ธํธ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ์๋ฐฉํฅ ์ฐ๊ฒฐ ์งํฅ์ฑ ํต์ TCP (Transmi
ssvip.tistory.com
2. ํ๋ก์ ํธ ์์ฑ
- TCP_Server ํ๋ก์ ํธ๋ฅผ ์์ฑ
- ํ๋ก์ ํธ๋ช : TCP_Server
- ์๋ฃจ์ ๋ช : TCP_Project
- ํ๋ ์์ํฌ : .NET 7.0
3. ๋์์ธ
- ์๋ ์ด๋ฏธ์ง์ ๊ฐ์ด ์ปจํธ๋กค๋ฌ๋ค์ ๋ฐฐ์นํ๋ค.
4. C# - TCP Server ์ฝ๋ ์์ฑํ๊ธฐ
- 4.1 ~ 4.10๋ฅผ ์ฐธ๊ณ ํ์ฌ TCP Server ์ฝ๋๋ฅผ ์์ฑํ๋ค.
4.1 ๋ค์์คํ์ด์ค ์์ฑ
- ํด๋น ํ๋ก์ ํธ์์ ์ฌ์ฉํ ๋ค์์คํ์ด์ค๋ฅผ ์ ์ธํ๋ค.
using System.Net;
using System.Net.Sockets;
using System.Text;
4.2 ์ ์ญ๋ณ์ ์ ์ธ
- ํด๋น ํ๋ก์ ํธ์์ ์ฌ์ฉํ ์ ์ญ ๋ณ์๋ค์ ์ ์ธํ๋ค.
TcpListener server = null; // ์๋ฒ
TcpClient clientSocket = null; // ์์ผ
string date; // ๋ ์ง
private Dictionary<TcpClient, string> clientList = new Dictionary<TcpClient, string>(); // ๊ฐ ํด๋ผ์ด์ธํธ ๋ง๋ค ๋ฆฌ์คํธ์ ์ถ๊ฐ
private int PORT = 5000; // ํฌํธ ์ ๋ณด
4.3 IP&Port ์ค์
- Server๊ฐ ์คํ๋๋ PC ๋๋ ์๋ฒ์ IP๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์๋ ์์ฑ
- Client์ ํต์ ์ ์ฌ์ฉํ Port ์ง์
private int PORT = 5000; // ํฌํธ ์ ๋ณด
/// <summary>
/// Form Load ์ด๋ฒคํธ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
InitForm();
this.Text = "Server";
txtIP.Text = "";
txtIP.Text = GetLocalIP();
txtPort.Text = PORT.ToString();
}
/// <summary>
/// IP ๊ฐ์ ธ์ค๊ธฐ
/// </summary>
/// <returns></returns>
private string GetLocalIP()
{
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
string localIP = string.Empty;
for (int i = 0; i < host.AddressList.Length; i++)
{
if (host.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
{
localIP = host.AddressList[i].ToString();
break;
}
}
return localIP;
}
private void InitForm()
{
txtIP.Text = "";
txtPort.Text = "";
txtMessage.Text = "";
richTextBox1.Text = "";
listBox1.Items.Clear();
}
4.4 HandleClient ์์ฑ
- ํด๋์ค๋ช : HandleClient.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace TCPIP_Server
{
internal class HandleClient
{
TcpClient clientSocket = null;
public Dictionary<TcpClient, string> clientList = null;
public void startClient(TcpClient clientSocket, Dictionary<TcpClient, string> clientList)
{
this.clientSocket = clientSocket;
this.clientList = clientList;
Thread t_hanlder = new Thread(doChat);
t_hanlder.IsBackground = true;
t_hanlder.Start();
}
public delegate void MessageDisplayHandler(string message, string userName);
public event MessageDisplayHandler OnReceived;
public delegate void DisconnectedHandler(TcpClient clientSocket);
public event DisconnectedHandler OnDisconnected;
private void doChat()
{
NetworkStream stream = null;
try
{
byte[] buffer = new byte[1024];
string msg = string.Empty;
int bytes = 0;
int MessageCount = 0;
while (true)
{
MessageCount++;
stream = clientSocket.GetStream();
bytes = stream.Read(buffer, 0, buffer.Length);
msg = Encoding.Unicode.GetString(buffer, 0, bytes);
msg = msg.Substring(0, msg.IndexOf("$"));
if (OnReceived != null)
OnReceived(msg, clientList[clientSocket].ToString());
}
}
catch (SocketException se)
{
Trace.WriteLine(string.Format("DoChat - SocketException : {0}", se.Message));
if (clientSocket != null)
{
if (OnDisconnected != null)
OnDisconnected(clientSocket);
clientSocket.Close();
stream.Close();
}
}
catch (Exception ex)
{
Trace.WriteLine(string.Format("DoChat - Exception : {0}", ex.Message));
if (clientSocket != null)
{
if (OnDisconnected != null)
OnDisconnected(clientSocket);
clientSocket.Close();
stream.Close();
}
}
}
}
}
4.5 InitSocket() ๋ฉ์๋ ์์ฑ
- InitSocket() : Client์ ํต์ ํ๋ ๋ฉ์๋
- ํด๋น ๋ฉ์๋๋ฅผ ํตํ์ฌ Client์ ์ฃผ๊ณ ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ํ์ธํ๋ค.
/// <summary>
/// Socket ์ด๊ธฐํ
/// </summary>
private void InitSocket()
{
server = new TcpListener(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text)); // ์๋ฒ ๊ฐ์ฒด ์์ฑ ๋ฐ IP์ฃผ์์ Port๋ฒํธ๋ฅผ ํ ๋น
clientSocket = default(TcpClient); // ์์ผ ์ค์
server.Start(); // ์๋ฒ ์์
DisplayText("System : Server ์์");
while (true)
{
try
{
clientSocket = server.AcceptTcpClient(); // client ์์ผ ์ ์ ํ์ฉ
NetworkStream stream = clientSocket.GetStream();
byte[] buffer = new byte[1024]; // ๋ฒํผ
int bytes = stream.Read(buffer, 0, buffer.Length);
string userName = Encoding.Unicode.GetString(buffer, 0, bytes);
userName = userName.Substring(0, userName.IndexOf("$")); // client ์ฌ์ฉ์ ๋ช
DisplayText("System : [" + userName + "] ์ ์");
clientList.Add(clientSocket, userName); // cleint ๋ฆฌ์คํธ์ ์ถ๊ฐ
SendMessageAll(userName + " ๋์ด ์
์ฅํ์
จ์ต๋๋ค.", "", false); // ๋ชจ๋ client์๊ฒ ๋ฉ์ธ์ง ์ ์ก
SetUserList(userName, "I");
HandleClient h_client = new HandleClient(); // ํด๋ผ์ด์ธํธ ์ถ๊ฐ
h_client.OnReceived += new HandleClient.MessageDisplayHandler(OnReceived);
h_client.OnDisconnected += new HandleClient.DisconnectedHandler(h_client_OnDisconnected);
h_client.startClient(clientSocket, clientList);
}
catch (SocketException se) { break; }
catch (Exception ex) { break; }
}
clientSocket.Close(); // client ์์ผ ๋ซ๊ธฐ
server.Stop(); // ์๋ฒ ์ข
๋ฃ
}
/// <summary>
/// Cleint ์ ์ ํด์ ํธ๋ค๋ฌ
/// </summary>
/// <param name="clientSocket">Client ์ ๋ณด</param>
private void h_client_OnDisconnected(TcpClient clientSocket)
{
if (clientList.ContainsKey(clientSocket))
{
clientList.Remove(clientSocket);
}
}
/// <summary>
/// Cleint๋ก ๋ถํฐ ๋ฐ์ ๋ฐ์ดํฐ
/// </summary>
/// <param name="message">๋ฉ์ธ์ง</param>
/// <param name="userName">์ฌ์ฉ์๋ช
</param>
private void OnReceived(string message, string userName)
{
if (message.Equals("LeaveChat"))
{
string displayMessage = "Leave user : " + userName;
DisplayText(displayMessage);
SendMessageAll("LeaveChat", userName, true);
SetUserList(userName, "D");
}
else
{
string displayMessage = "From Client : " + userName + " : " + message;
DisplayText(displayMessage); // Server๋จ์ ์ถ๋ ฅ
SendMessageAll(message, userName, true); // ๋ชจ๋ Client์๊ฒ ์ ์ก
}
}
4.6 ๊ธฐํ ๋ฉ์๋
- DisplayText() : richTextBox์ ํ ์คํธ๋ฅผ ์ถ๊ฐํ๊ธฐ ์ํด ์ ์๋ ๋ฉ์๋
- SetUserList() : ์ ์ํ ์ฌ์ฉ์ ๋ฆฌ์คํธ ์ถ๋ ฅ ๋ฉ์๋
- SendMessageAll() : ๋ฉ์ธ์ง ์ ์ก ๋ฉ์๋
/// <summary>
/// ํ๋ฉด์ Text ์ถ๋ ฅ
/// </summary>
/// <param name="text"></param>
private void DisplayText(string text)
{
richTextBox1.Invoke((MethodInvoker)delegate { richTextBox1.AppendText(text + "\r\n"); }); // ๋ฐ์ดํฐ๋ฅผ ์์ ์ฐฝ์ ํ์, ๋ฐ์ดํฐ ์ถฉ๋์ ํผํ๊ธฐ ์ํด Invoke ์ฌ์ฉ
richTextBox1.Invoke((MethodInvoker)delegate { richTextBox1.ScrollToCaret(); }); // ์คํฌ๋กค์ ์ ์ผ ์๋๋ก ์ค์
}
/// <summary>
/// ์ ์ Client ์ถ๋ ฅ
/// </summary>
/// <param name="userName">์ฌ์ฉ์๋ช
</param>
/// <param name="div">๊ตฌ๋ถ(I: ์
๋ ฅ, D: ์ญ์ )</param>
private void SetUserList(string userName, string div)
{
try
{
if (div.Equals("I"))
listBox1.Items.Add(userName);
else if (div.Equals("D"))
listBox1.Items.Remove(userName);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// ๋ฉ์ธ์ง ์ ์ก
/// </summary>
/// <param name="message">๋ฉ์ธ์ง</param>
/// <param name="userName">Client</param>
/// <param name="flag">๊ตฌ๋ถ์(True: ์ ์ฒด์ ์ก, False: ๊ฐ๋ณ์ ์ก)</param>
public void SendMessageAll(string message, string userName, bool flag)
{
foreach (var pair in clientList)
{
date = DateTime.Now.ToString("yyyy.MM.dd. HH:mm:ss"); // ํ์ฌ ๋ ์ง ๋ฐ๊ธฐ
TcpClient client = pair.Key as TcpClient;
NetworkStream stream = client.GetStream();
byte[] buffer = null;
if (flag)
{
if (message.Equals("LeaveChat"))
buffer = Encoding.Unicode.GetBytes(userName + " ๋์ด ๋ํ๋ฐฉ์ ๋๊ฐ์ต๋๋ค.");
else
buffer = Encoding.Unicode.GetBytes("[ " + date + " ] " + userName + " : " + message);
}
else
{
buffer = Encoding.Unicode.GetBytes(message);
}
stream.Write(buffer, 0, buffer.Length); // ๋ฒํผ ์ฐ๊ธฐ
stream.Flush();
}
}
4.7์๋ฒ ์คํ
- ์๋ฒ ์คํ ๋ฒํผ์ ํด๋ฆญํ๋ฉด 4.5์์ ์์ฑํ InitSocket() ๋ฉ์๋๊ฐ Thread์ ๋ฑ๋ก๋์ด ์คํ๋๋ค.
/// <summary>
/// ์๋ฒ ์คํ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConnect_Click(object sender, EventArgs e)
{
Thread thread1 = new Thread(InitSocket);
thread1.IsBackground = true;
thread1.Start();
}
4.8 ๋ฉ์ธ์ง ์ ์ก
- ์ ์ก ๋ฒํผ์ ํด๋ฆญํ๋ฉด Server์ ์ ์์ค์ธ Client ๋ชจ๋์๊ฒ ๋ฉ์์ง๋ฅผ ๋ฐ์กํ๋ค.
/// <summary>
/// ์ ์ก ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSendMessage_Click(object sender, EventArgs e)
{
string userName = "Admin";
string message = txtMessage.Text.Trim();
string displayMessage = "[" + userName + "] : " + message;
DisplayText(displayMessage);
SendMessageAll(message, userName, true); // ๋ชจ๋ Client์๊ฒ ์ ์ก
}
4.9 ์คํ ๊ฒฐ๊ณผ
- ์คํ ๊ฒฐ๊ณผ๋ ์๋ ์ด๋ฏธ์ง์ ๊ฐ๋ค.
- IP ์ฃผ์ TextBox์๋ ์คํํ PC๋ ์๋ฒ์ IP๊ฐ ์ถ๋ ฅ๋๋ค.
4.10 ์ ์ฒด ์์ค์ฝ๋
- TCP_Server ์ฝ๋
using System.Net;
using System.Net.Sockets;
using System.Text;
using TCPIP_Server;
namespace TCP_Server
{
public partial class Form1 : Form
{
TcpListener server = null; // ์๋ฒ
TcpClient clientSocket = null; // ์์ผ
string date; // ๋ ์ง
private Dictionary<TcpClient, string> clientList = new Dictionary<TcpClient, string>(); // ๊ฐ ํด๋ผ์ด์ธํธ ๋ง๋ค ๋ฆฌ์คํธ์ ์ถ๊ฐ
private int PORT = 5000; // ํฌํธ ์ ๋ณด
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Form Load ์ด๋ฒคํธ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
InitForm();
this.Text = "Server";
txtIP.Text = "";
txtIP.Text = GetLocalIP();
txtPort.Text = PORT.ToString();
}
private void InitForm()
{
txtIP.Text = "";
txtPort.Text = "";
txtMessage.Text = "";
richTextBox1.Text = "";
listBox1.Items.Clear();
}
/// <summary>
/// IP ๊ฐ์ ธ์ค๊ธฐ
/// </summary>
/// <returns></returns>
private string GetLocalIP()
{
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
string localIP = string.Empty;
for (int i = 0; i < host.AddressList.Length; i++)
{
if (host.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
{
localIP = host.AddressList[i].ToString();
break;
}
}
return localIP;
}
/// <summary>
/// ์๋ฒ ์คํ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConnect_Click(object sender, EventArgs e)
{
Thread thread1 = new Thread(InitSocket);
thread1.IsBackground = true;
thread1.Start();
}
/// <summary>
/// Socket ์ด๊ธฐํ
/// </summary>
private void InitSocket()
{
server = new TcpListener(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text)); // ์๋ฒ ๊ฐ์ฒด ์์ฑ ๋ฐ IP์ฃผ์์ Port๋ฒํธ๋ฅผ ํ ๋น
clientSocket = default(TcpClient); // ์์ผ ์ค์
server.Start(); // ์๋ฒ ์์
DisplayText("System : Server ์์");
while (true)
{
try
{
clientSocket = server.AcceptTcpClient(); // client ์์ผ ์ ์ ํ์ฉ
NetworkStream stream = clientSocket.GetStream();
byte[] buffer = new byte[1024]; // ๋ฒํผ
int bytes = stream.Read(buffer, 0, buffer.Length);
string userName = Encoding.Unicode.GetString(buffer, 0, bytes);
userName = userName.Substring(0, userName.IndexOf("$")); // client ์ฌ์ฉ์ ๋ช
DisplayText("System : [" + userName + "] ์ ์");
clientList.Add(clientSocket, userName); // cleint ๋ฆฌ์คํธ์ ์ถ๊ฐ
SendMessageAll(userName + " ๋์ด ์
์ฅํ์
จ์ต๋๋ค.", "", false); // ๋ชจ๋ client์๊ฒ ๋ฉ์ธ์ง ์ ์ก
SetUserList(userName, "I");
HandleClient h_client = new HandleClient(); // ํด๋ผ์ด์ธํธ ์ถ๊ฐ
h_client.OnReceived += new HandleClient.MessageDisplayHandler(OnReceived);
h_client.OnDisconnected += new HandleClient.DisconnectedHandler(h_client_OnDisconnected);
h_client.startClient(clientSocket, clientList);
}
catch (SocketException se) { break; }
catch (Exception ex) { break; }
}
clientSocket.Close(); // client ์์ผ ๋ซ๊ธฐ
server.Stop(); // ์๋ฒ ์ข
๋ฃ
}
/// <summary>
/// Cleint ์ ์ ํด์ ํธ๋ค๋ฌ
/// </summary>
/// <param name="clientSocket">Client ์ ๋ณด</param>
private void h_client_OnDisconnected(TcpClient clientSocket)
{
if (clientList.ContainsKey(clientSocket))
{
clientList.Remove(clientSocket);
}
}
/// <summary>
/// Cleint๋ก ๋ถํฐ ๋ฐ์ ๋ฐ์ดํฐ
/// </summary>
/// <param name="message">๋ฉ์ธ์ง</param>
/// <param name="userName">์ฌ์ฉ์๋ช
</param>
private void OnReceived(string message, string userName)
{
if (message.Equals("LeaveChat"))
{
string displayMessage = "Leave user : " + userName;
DisplayText(displayMessage);
SendMessageAll("LeaveChat", userName, true);
SetUserList(userName, "D");
}
else
{
string displayMessage = "From Client : " + userName + " : " + message;
DisplayText(displayMessage); // Server๋จ์ ์ถ๋ ฅ
SendMessageAll(message, userName, true); // ๋ชจ๋ Client์๊ฒ ์ ์ก
}
}
/// <summary>
/// ๋ฉ์ธ์ง ์ ์ก
/// </summary>
/// <param name="message">๋ฉ์ธ์ง</param>
/// <param name="userName">Client</param>
/// <param name="flag">๊ตฌ๋ถ์(True: ์ ์ฒด์ ์ก, False: ๊ฐ๋ณ์ ์ก)</param>
public void SendMessageAll(string message, string userName, bool flag)
{
foreach (var pair in clientList)
{
date = DateTime.Now.ToString("yyyy.MM.dd. HH:mm:ss"); // ํ์ฌ ๋ ์ง ๋ฐ๊ธฐ
TcpClient client = pair.Key as TcpClient;
NetworkStream stream = client.GetStream();
byte[] buffer = null;
if (flag)
{
if (message.Equals("LeaveChat"))
buffer = Encoding.Unicode.GetBytes(userName + " ๋์ด ๋ํ๋ฐฉ์ ๋๊ฐ์ต๋๋ค.");
else
buffer = Encoding.Unicode.GetBytes("[ " + date + " ] " + userName + " : " + message);
}
else
{
buffer = Encoding.Unicode.GetBytes(message);
}
stream.Write(buffer, 0, buffer.Length); // ๋ฒํผ ์ฐ๊ธฐ
stream.Flush();
}
}
/// <summary>
/// ํ๋ฉด์ Text ์ถ๋ ฅ
/// </summary>
/// <param name="text"></param>
private void DisplayText(string text)
{
richTextBox1.Invoke((MethodInvoker)delegate { richTextBox1.AppendText(text + "\r\n"); }); // ๋ฐ์ดํฐ๋ฅผ ์์ ์ฐฝ์ ํ์, ๋ฐ์ดํฐ ์ถฉ๋์ ํผํ๊ธฐ ์ํด Invoke ์ฌ์ฉ
richTextBox1.Invoke((MethodInvoker)delegate { richTextBox1.ScrollToCaret(); }); // ์คํฌ๋กค์ ์ ์ผ ์๋๋ก ์ค์
}
/// <summary>
/// ์ ์ Client ์ถ๋ ฅ
/// </summary>
/// <param name="userName">์ฌ์ฉ์๋ช
</param>
/// <param name="div">๊ตฌ๋ถ(I: ์
๋ ฅ, D: ์ญ์ )</param>
private void SetUserList(string userName, string div)
{
try
{
if (div.Equals("I"))
listBox1.Items.Add(userName);
else if (div.Equals("D"))
listBox1.Items.Remove(userName);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// ์ ์ก ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSendMessage_Click(object sender, EventArgs e)
{
string userName = "Admin";
string message = txtMessage.Text.Trim();
string displayMessage = "[" + userName + "] : " + message;
DisplayText(displayMessage);
SendMessageAll(message, userName, true); // ๋ชจ๋ Client์๊ฒ ์ ์ก
}
}
}
- HandleClient ์ฝ๋
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace TCPIP_Server
{
internal class HandleClient
{
TcpClient clientSocket = null;
public Dictionary<TcpClient, string> clientList = null;
public void startClient(TcpClient clientSocket, Dictionary<TcpClient, string> clientList)
{
this.clientSocket = clientSocket;
this.clientList = clientList;
Thread t_hanlder = new Thread(doChat);
t_hanlder.IsBackground = true;
t_hanlder.Start();
}
public delegate void MessageDisplayHandler(string message, string userName);
public event MessageDisplayHandler OnReceived;
public delegate void DisconnectedHandler(TcpClient clientSocket);
public event DisconnectedHandler OnDisconnected;
private void doChat()
{
NetworkStream stream = null;
try
{
byte[] buffer = new byte[1024];
string msg = string.Empty;
int bytes = 0;
int MessageCount = 0;
while (true)
{
MessageCount++;
stream = clientSocket.GetStream();
bytes = stream.Read(buffer, 0, buffer.Length);
msg = Encoding.Unicode.GetString(buffer, 0, bytes);
msg = msg.Substring(0, msg.IndexOf("$"));
if (OnReceived != null)
OnReceived(msg, clientList[clientSocket].ToString());
}
}
catch (SocketException se)
{
Trace.WriteLine(string.Format("DoChat - SocketException : {0}", se.Message));
if (clientSocket != null)
{
if (OnDisconnected != null)
OnDisconnected(clientSocket);
clientSocket.Close();
stream.Close();
}
}
catch (Exception ex)
{
Trace.WriteLine(string.Format("DoChat - Exception : {0}", ex.Message));
if (clientSocket != null)
{
if (OnDisconnected != null)
OnDisconnected(clientSocket);
clientSocket.Close();
stream.Close();
}
}
}
}
}
โป TCP Client ๊ตฌํ์ ์๋ ๊ฒ์๊ธ ์ฐธ๊ณ
[C#] TCP ์ฑํ ํ๋ก๊ทธ๋จ #2 Client ๊ตฌํ
ํด๋น ๊ฒ์๊ธ์ ์๋ ๊ฐ๋ฐํ๊ฒฝ์ ์ฌ์ฉํ์ฌ ์์ฑ๋จ IDE : VisualStudio 2022 Framework : .NET 7.0 Language : C# 11 โป ์ด์ ๊ฒ์๊ธ [C#] TCP ์ฑํ ํ๋ก๊ทธ๋จ #1 Server ๊ตฌํ ํด๋น ๊ฒ์๊ธ์ ์๋ ๊ฐ๋ฐํ๊ฒฝ์ ์ฌ์ฉํ์ฌ ์
ssvip.tistory.com
C# ์ํผ - TCP/IP ๋คํธ์ํฌ ํต์ ์ ๋ซ์ด๋ณด์
C# ์ํผ - TCP/IP ๋คํธ์ํฌ ํต์ ์ ๋ซ์ด๋ณด์ TCP/IP ๋คํธ์ํฌ ํต์ ์ ์ด๋ณด์ ์ ์ฅ์์๋ ๋ค์ ๋ณต์กํ ๊ฐ๋ ์ด๋ค. ๊ทธ๋ฌ๋ ์๋ฒ์ ํด๋ผ์ด์ธํธ๊ฐ ๋ฉ์ธ์ง๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ๊ฐ๋จํ ์ฑํ ํ๋ก๊ทธ๋จ๋ง์ด๋ผ๋ ์ฑ๊ณต
unininu.tistory.com
C# ์์ผ ํ๋ก๊ทธ๋๋ฐ - ๋น๋๊ธฐ ์ฑํ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ (1:n ํต์ )
์ง๋๋ฒ์ 1:1 ์์ผํ๋ก๊ทธ๋๋ฐ์ผ๋ก ์ฑํ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด์์ต๋๋ค. ์ฐธ๊ณ : http://yeolco.tistory.com/31 ์ด๋ฒ ์๊ฐ์๋ 1:n ๋น๋๊ธฐ ๋ฐฉ์ ์ฑํ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด๋ณด๊ฒ ์ต๋๋ค. ๊ฐ ํด๋ผ์ด์ธํธ ์์ผ๋ง๋ค
yeolco.tistory.com
[Program C#] ์๋ฒ-ํด๋ผ์ด์ธํธ ์ฑํ ํต์
ํ๋ก๊ทธ๋จ ์ค๋ช ์คํ ํ Connect ๋ฒํผ ํด๋ฆญ ํ ๋ฉ์์ง ์ ์ก ํ ํ๋ก๊ทธ๋จ ์์ฑ 1. handleClient.cs (์๋ฒ์์ ์ฌ์ฉํ๋ ํด๋์ค) class handleClient { TcpClient clientSocket = null; public Dictionary clientList = null; public void
it-jerryfamily.tistory.com
TCP ํด๋ผ์ด์ธํธ - C# ํ๋ก๊ทธ๋๋ฐ ๋ฐฐ์ฐ๊ธฐ (Learn C# Programming)
TCP ํด๋ผ์ด์ธํธ TcpClient ํด๋์ค .NET Framework์์ TCP ํด๋ผ์ด์ธํธ ํ๋ก๊ทธ๋จ์ ๊ฐ๋ฐํ๊ธฐ ์ํด์๋ System.Net.Sockets.TcpClient ํด๋์ค๋ฅผ ์ฌ์ฉํ ์ ์๋ค. TcpClient ํด๋์ค๋ ๋ด๋ถ์ ์ผ๋ก System.Net.Sockets.Socket ํด
www.csharpstudy.com
[C#] TCP ์์ผ ํต์
์ด๋ฒ ์๊ฐ์๋ CTCP ์์ผ ํต์ ์ ๊ตฌํํด๋ณด๊ธฐ์ ์์, TCP๊ฐ ๋ฌด์์ธ์ง๋ถํฐ ์ดํดํ๊ณ ๋์ด๊ฐ๋ ๊ฒ์ด ์ข๋ค๊ณ ์๊ฐํ๋ค. ์ฐ์ TCP๋ Transmission Control Protocol์ ์ค์๋ง์ผ๋ก, ์ฃผ๋ ํน์ง์ผ๋ก๋ ๋ค์๊ณผ ๊ฐ๋ค.
velog.io
'๊ฐ๋ฐ๐ป > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C#] ์ง๋ ฌํ&๋ง์ฌ๋ง (0) | 2023.07.01 |
---|---|
[C#] TCP ์ฑํ ํ๋ก๊ทธ๋จ #2 Client ๊ตฌํ (0) | 2023.06.12 |
[C#] ๋คํธ์ํฌ ํต์ - TCP&UDP (3) | 2023.06.11 |
[C#] ChatGPT ์ฑํ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ (2) | 2023.06.06 |
[C#] ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ Async, Await (0) | 2023.06.05 |