ํด๋น ๊ฒ์๊ธ์ ์๋ ๊ฐ๋ฐํ๊ฒฝ์ ์ฌ์ฉํ์ฌ ์์ฑ๋จ
IDE : VisualStudio 2022
Framework : .NET 7.0
Language : C# 11
โป ์ด์ ๊ฒ์๊ธ
[C#] TCP ์ฑํ ํ๋ก๊ทธ๋จ #1 Server ๊ตฌํ
ํด๋น ๊ฒ์๊ธ์ ์๋ ๊ฐ๋ฐํ๊ฒฝ์ ์ฌ์ฉํ์ฌ ์์ฑ๋จ IDE : VisualStudio 2022 Framework : .NET 7.0 Language : C# 11 1. TCP TCP์ ๋ํ ๋ด์ฉ์ ์๋ ๊ฒ์๊ธ์ ์ฐธ๊ณ [C#] ๋คํธ์ํฌ ํต์ - TCP&UDP 1. ์ฉ์ด์ ๋ฆฌ ์์ผ (Socket) :
ssvip.tistory.com
1. TCP
- TCP์ ๋ํ ๋ด์ฉ์ ์๋ ๊ฒ์๊ธ์ ์ฐธ๊ณ
[C#] ๋คํธ์ํฌ ํต์ - TCP&UDP
1. ์ฉ์ด์ ๋ฆฌ ์์ผ (Socket) : ๋คํธ์ํฌ ์์์ ๋์๊ฐ๋ ํ๋ก๊ทธ๋จ์ ์๋ฐฉํฅ ํต์ ์ ์๋ ํฌ์ธํธ ์์ผํต์ : ์์ผ์ ํตํด ์๋ฒ-ํด๋ผ์ด์ธํธ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ์๋ฐฉํฅ ์ฐ๊ฒฐ ์งํฅ์ฑ ํต์ TCP (Transmi
ssvip.tistory.com
2. ํ๋ก์ ํธ ์์ฑ
- ์์ ์์ฑํ๋ TCP_Project ์๋ฃจ์
์ TCP_Client ํ๋ก์ ํธ๋ฅผ ์์ฑ
- ํ๋ก์ ํธ๋ช : TCP_Client
- ์๋ฃจ์ ๋ช : TCP_Project
- ํ๋ ์์ํฌ : .NET 7.0
3. ๋์์ธ
- ์๋ ์ด๋ฏธ์ง์ ๊ฐ์ด ์ปจํธ๋กค๋ฌ๋ค์ ๋ฐฐ์นํ๋ค.
4. C# - TCP Client ์ฝ๋ ์์ฑํ๊ธฐ
- 4.1 ~ 4.10๋ฅผ ์ฐธ๊ณ ํ์ฌ TCP Client ์ฝ๋๋ฅผ ์์ฑํ๋ค.
4.1 ๋ค์์คํ์ด์ค ์์ฑ
- ํด๋น ํ๋ก์ ํธ์์ ์ฌ์ฉํ ๋ค์์คํ์ด์ค๋ฅผ ์ ์ธํ๋ค.
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
4.2 ์ ์ญ๋ณ์ ์ ์ธ
- ํด๋น ํ๋ก์ ํธ์์ ์ฌ์ฉํ ์ ์ญ ๋ณ์๋ค์ ์ ์ธํ๋ค.
TcpClient clientSocket = new TcpClient(); // ์์ผ
NetworkStream stream = default(NetworkStream);
string message = string.Empty;
private int PORT = 5000; // ํฌํธ ์ ๋ณด
private string USER_NAME = string.Empty;
private static string CONNECT_STATUS = "DISCONNECT"; // ์ฐ๊ฒฐ์ํ
4.3 IP&Port ์ค์
- Server์ IP์ Port ์ ๋ณด๋ฅผ ์ค์ ํ๋ค.
- IP์ Port๋ฅผ ์ฌ์ฉํ์ฌ TCP Server์ ์ฐ๊ฒฐํ๋ค.
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 = "Client";
txtIP.Text = "TCP Server์ IP ์
๋ ฅ";
txtPort.Text = PORT.ToString();
txtUserName.Text = GetPID();
txtStatus.Text = "๋๊ธฐ์ค...";
}
4.4 Server Connect
- ์๋ฒ ์ฐ๊ฒฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด TCP Server์ ์ฐ๊ฒฐ๋๋๋ก ์ฝ๋ ์์ฑ
- Connect() : TCP Server์ IP์ ์ฝ์๋ Port ์ ๋ณด๋ฅผ ์ด์ฉํ์ฌ Server์ ์ฐ๊ฒฐ
/// <summary>
/// ์๋ฒ ์ฐ๊ฒฐ ๋ฒํผ ํด๋ฆญ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
if (CONNECT_STATUS.Equals("DISCONNECT"))
{
Connect();
USER_NAME = txtUserName.Text.Trim();
txtStatus.Text = "์ฐ๊ฒฐ๋จ";
CONNECT_STATUS = "CONNECT";
}
else if (CONNECT_STATUS.Equals("CONNECT"))
{
return;
}
else
{
return;
}
}
catch (Exception ex)
{
MessageBox.Show("์๋ฒ๊ฐ ์คํ์ค์ด ์๋๋๋ค.", "์ฐ๊ฒฐ ์คํจ!");
}
message = "์ฑํ
์๋ฒ์ ์ฐ๊ฒฐ ๋์์ต๋๋ค.";
DisplayText(message);
byte[] buffer = Encoding.Unicode.GetBytes(USER_NAME + "$");
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
Thread t_handler = new Thread(GetMessage);
t_handler.IsBackground = true;
t_handler.Start();
}
/// <summary>
/// Connect ๋ฉ์๋
/// </summary>
private void Connect()
{
clientSocket.Connect(txtIP.Text.ToString(), PORT); // ์ ์ IP ๋ฐ ํฌํธ
stream = clientSocket.GetStream();
}
4.5 Server DisConnect
- ์ฐ๊ฒฐ ๋๊ธฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด TCP Server์ ์ฐ๊ฒฐ์ด ์ข ๋ฃ๋๋๋ก ์ฝ๋ ์์ฑ
- DisConnect() : ์ฐ๊ฒฐ๋์ด์๋ Server์ ์ฐ๊ฒฐ ์ข ๋ฃ
/// <summary>
/// ์ฐ๊ฒฐ ๋๊ธฐ ๋ฒํผ ํด๋ฆญ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDisConnect_Click(object sender, EventArgs e)
{
if (CONNECT_STATUS.Equals("DISCONNECT"))
{
MessageBox.Show("์ฐ๊ฒฐ๋ ์ํ๊ฐ ์๋๋๋ค.");
return;
}
else if (CONNECT_STATUS.Equals("CONNECT"))
{
DisConnect();
txtStatus.Text = "๋๊ธฐ์ค...";
CONNECT_STATUS = "DISCONNECT";
return;
}
else
{ }
}
/// <summary>
/// DisConnect ๋ฉ์๋
/// </summary>
private void DisConnect()
{
clientSocket = null;
byte[] buffer = Encoding.Unicode.GetBytes("LeaveChat" + "$");
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
}
4.6 ๋ฉ์ธ์ง ์ ์ก
- ์ ์กํ ๋ฉ์ธ์ง๋ฅผ ์ ๋ ฅํ๊ณ Enter ๋๋ ์ ์ก ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ ๋ ฅ๋ ๋ฉ์ธ์ง๊ฐ Server๋ก ์ ์ก๋๋ค.
/// <summary>
/// ์ ์ก ๋ฒํผ ํด๋ฆญ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSendMessage_Click(object sender, EventArgs e)
{
txtMessage.Focus();
byte[] buffer = Encoding.Unicode.GetBytes(txtMessage.Text + "$");
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
txtMessage.Text = "";
}
/// <summary>
/// ๋ฉ์ธ์ง TextBox KeyUp ์ด๋ฒคํธ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtMessage_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) // ์ํฐํค ๋๋ ์ ๋
btnSendMessage_Click(this, e);
}
4.7 ๋ฉ์ธ์ง ์์
- TCP Server์ ์ฐ๊ฒฐ ์, Thread๋ก ์คํํ GetMessage() ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ Server๋ก๋ถํฐ ๋ฉ์ธ์ง๋ฅผ ์์ ๋ฐ๋๋ค.
/// <summary>
/// ๋ฉ์ธ์ง ์์
/// </summary>
private void GetMessage() // ๋ฉ์ธ์ง ๋ฐ๊ธฐ
{
while (true)
{
stream = clientSocket.GetStream();
int BUFFERSIZE = clientSocket.ReceiveBufferSize;
byte[] buffer = new byte[BUFFERSIZE];
int bytes = stream.Read(buffer, 0, buffer.Length);
string message = Encoding.Unicode.GetString(buffer, 0, bytes);
DisplayText(message);
}
}
4.8 ํ๋ก๊ทธ๋จ ์ข ๋ฃ
- FormClosing() ์ด๋ฒคํธ๋ฅผ ์ด์ฉํ์ฌ ํ๋ก๊ทธ๋จ ์ข ๋ฃ ์, Server์ ์ฐ๊ฒฐ์ ์ข ๋ฃํ๋ค.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DisConnect();
Application.ExitThread();
Environment.Exit(0);
}
4.9 ๊ธฐํ ๋ฉ์๋
- DisplayText() : richTextBox์ ํ ์คํธ๋ฅผ ์ถ๊ฐํ๊ธฐ ์ํด ์ ์๋ ๋ฉ์๋
- GetPID() : ํ๋ก๊ทธ๋จ์ด ์คํ๋์ด์๋ ProcessID๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์๋, PID๋ฅผ USER_NAME์ผ๋ก ์ฌ์ฉ
private void DisplayText(string text) // Server์ ๋ฉ์ธ์ง ์ถ๋ ฅ
{
richTextBox1.Invoke((MethodInvoker)delegate { richTextBox1.AppendText(text + "\r\n"); }); // ๋ฐ์ดํ๋ฅผ ์์ ์ฐฝ์ ํ์, ๋ฐ๋์ invoke ์ฌ์ฉ. ์ถฉ๋ํผํจ.
richTextBox1.Invoke((MethodInvoker)delegate { richTextBox1.ScrollToCaret(); }); // ์คํฌ๋กค์ ์ ค ๋ฐ์ผ๋ก.
}
/// <summary>
/// Process ID ๊ฐ์ ธ์ค๊ธฐ
/// </summary>
/// <returns></returns>
private string GetPID()
{
string rtn = string.Empty;
Process currentProcess = Process.GetCurrentProcess();
rtn = currentProcess.Id.ToString();
return rtn;
}
4.10 ์คํ ๊ฒฐ๊ณผ
- ์คํ ๊ฒฐ๊ณผ๋ ์๋ ์ด๋ฏธ์ง์ ๊ฐ๋ค.
- IP ์ฃผ์ TextBox์๋ TCP Server์ IP๋ฅผ ์ ๋ ฅํ๋ค.
4.11 ์ ์ฒด ์์ค์ฝ๋
- TCP Client ์ฝ๋
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace TCP_Client
{
public partial class Form1 : Form
{
TcpClient clientSocket = new TcpClient(); // ์์ผ
NetworkStream stream = default(NetworkStream);
string message = string.Empty;
private int PORT = 5000; // ํฌํธ ์ ๋ณด
private string USER_NAME = string.Empty;
private static string CONNECT_STATUS = "DISCONNECT"; // ์ฐ๊ฒฐ์ํ
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 = "Client";
txtIP.Text = "TCP Server์ IP ์
๋ ฅ";
txtPort.Text = PORT.ToString();
txtUserName.Text = GetPID();
txtStatus.Text = "๋๊ธฐ์ค...";
}
private void InitForm()
{
txtIP.Text = "";
txtPort.Text = "";
txtMessage.Text = "";
richTextBox1.Text = "";
listBox1.Items.Clear();
}
/// <summary>
/// Process ID ๊ฐ์ ธ์ค๊ธฐ
/// </summary>
/// <returns></returns>
private string GetPID()
{
string rtn = string.Empty;
Process currentProcess = Process.GetCurrentProcess();
rtn = currentProcess.Id.ToString();
return rtn;
}
/// <summary>
/// DisConnect ๋ฉ์๋
/// </summary>
private void DisConnect()
{
clientSocket = null;
byte[] buffer = Encoding.Unicode.GetBytes("LeaveChat" + "$");
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
}
/// <summary>
/// Connect ๋ฉ์๋
/// </summary>
private void Connect()
{
clientSocket.Connect(txtIP.Text.ToString(), PORT); // ์ ์ IP ๋ฐ ํฌํธ
stream = clientSocket.GetStream();
}
/// <summary>
/// ์๋ฒ ์ฐ๊ฒฐ ๋ฒํผ ํด๋ฆญ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
if (CONNECT_STATUS.Equals("DISCONNECT"))
{
Connect();
USER_NAME = txtUserName.Text.Trim();
txtStatus.Text = "์ฐ๊ฒฐ๋จ";
CONNECT_STATUS = "CONNECT";
}
else if (CONNECT_STATUS.Equals("CONNECT"))
{
return;
}
else
{
return;
}
}
catch (Exception ex)
{
MessageBox.Show("์๋ฒ๊ฐ ์คํ์ค์ด ์๋๋๋ค.", "์ฐ๊ฒฐ ์คํจ!");
}
message = "์ฑํ
์๋ฒ์ ์ฐ๊ฒฐ ๋์์ต๋๋ค.";
DisplayText(message);
byte[] buffer = Encoding.Unicode.GetBytes(USER_NAME + "$");
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
Thread t_handler = new Thread(GetMessage);
t_handler.IsBackground = true;
t_handler.Start();
}
/// <summary>
/// ๋ฉ์ธ์ง ์์
/// </summary>
private void GetMessage() // ๋ฉ์ธ์ง ๋ฐ๊ธฐ
{
while (true)
{
stream = clientSocket.GetStream();
int BUFFERSIZE = clientSocket.ReceiveBufferSize;
byte[] buffer = new byte[BUFFERSIZE];
int bytes = stream.Read(buffer, 0, buffer.Length);
string message = Encoding.Unicode.GetString(buffer, 0, bytes);
DisplayText(message);
}
}
/// <summary>
/// ์ฐ๊ฒฐ ๋๊ธฐ ๋ฒํผ ํด๋ฆญ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDisConnect_Click(object sender, EventArgs e)
{
if (CONNECT_STATUS.Equals("DISCONNECT"))
{
MessageBox.Show("์ฐ๊ฒฐ๋ ์ํ๊ฐ ์๋๋๋ค.");
return;
}
else if (CONNECT_STATUS.Equals("CONNECT"))
{
DisConnect();
txtStatus.Text = "๋๊ธฐ์ค...";
CONNECT_STATUS = "DISCONNECT";
return;
}
else
{ }
}
/// <summary>
/// ์ ์ก ๋ฒํผ ํด๋ฆญ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSendMessage_Click(object sender, EventArgs e)
{
txtMessage.Focus();
byte[] buffer = Encoding.Unicode.GetBytes(txtMessage.Text + "$");
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
txtMessage.Text = "";
}
/// <summary>
/// ๋ฉ์ธ์ง TextBox KeyUp ์ด๋ฒคํธ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtMessage_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) // ์ํฐํค ๋๋ ์ ๋
btnSendMessage_Click(this, e);
}
private void DisplayText(string text) // Server์ ๋ฉ์ธ์ง ์ถ๋ ฅ
{
richTextBox1.Invoke((MethodInvoker)delegate { richTextBox1.AppendText(text + "\r\n"); }); // ๋ฐ์ดํ๋ฅผ ์์ ์ฐฝ์ ํ์, ๋ฐ๋์ invoke ์ฌ์ฉ. ์ถฉ๋ํผํจ.
richTextBox1.Invoke((MethodInvoker)delegate { richTextBox1.ScrollToCaret(); }); // ์คํฌ๋กค์ ์ ค ๋ฐ์ผ๋ก.
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DisConnect();
Application.ExitThread();
Environment.Exit(0);
}
}
}
C# ์ํผ - TCP/IP ๋คํธ์ํฌ ํต์ ์ ๋ซ์ด๋ณด์
C# ์ํผ - TCP/IP ๋คํธ์ํฌ ํต์ ์ ๋ซ์ด๋ณด์ TCP/IP ๋คํธ์ํฌ ํต์ ์ ์ด๋ณด์ ์ ์ฅ์์๋ ๋ค์ ๋ณต์กํ ๊ฐ๋ ์ด๋ค. ๊ทธ๋ฌ๋ ์๋ฒ์ ํด๋ผ์ด์ธํธ๊ฐ ๋ฉ์ธ์ง๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ๊ฐ๋จํ ์ฑํ ํ๋ก๊ทธ๋จ๋ง์ด๋ผ๋ ์ฑ๊ณต
unininu.tistory.com
'๊ฐ๋ฐ๐ป > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C#] C#์์ C++ DLL ์ฌ์ฉ - ๋ง์ฌ๋ง (Marshalling) (3) | 2023.07.10 |
---|---|
[C#] ์ง๋ ฌํ&๋ง์ฌ๋ง (0) | 2023.07.01 |
[C#] TCP ์ฑํ ํ๋ก๊ทธ๋จ #1 Server ๊ตฌํ (4) | 2023.06.11 |
[C#] ๋คํธ์ํฌ ํต์ - TCP&UDP (3) | 2023.06.11 |
[C#] ChatGPT ์ฑํ ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ (2) | 2023.06.06 |