93 lines
2.7 KiB
C#
93 lines
2.7 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Million_Update_Server
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
string log = "";
|
|
private TcpListener tcpListener;
|
|
private Thread listenThread = null;
|
|
IPAddress ipAddress = IPAddress.Parse("172.30.1.56");
|
|
int port = 6666;
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void writeLog(string text)
|
|
{
|
|
log = text+"\r\n" + log;
|
|
|
|
Invoke(new MethodInvoker(delegate ()
|
|
{
|
|
textBox_log.Text = log;
|
|
}));
|
|
}
|
|
|
|
private void button_ServerStart_Click(object sender, System.EventArgs e)
|
|
{
|
|
if(listenThread != null && listenThread.IsAlive)
|
|
{
|
|
writeLog("이미 실행중입니다.");
|
|
return;
|
|
}
|
|
// TcpListener 객체 생성
|
|
this.tcpListener = new TcpListener(ipAddress, port);
|
|
this.listenThread = new Thread(new ThreadStart(ListenForClients));
|
|
this.listenThread.Start();
|
|
writeLog("서버 활성화 합니다.");
|
|
}
|
|
|
|
private void ListenForClients()
|
|
{
|
|
this.tcpListener.Start();
|
|
|
|
while (true)
|
|
{
|
|
// 클라이언트 접속 대기
|
|
TcpClient client = this.tcpListener.AcceptTcpClient();
|
|
|
|
// 새로운 클라이언트가 접속했을 때 처리
|
|
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
|
|
clientThread.Start(client);
|
|
}
|
|
}
|
|
|
|
private void HandleClientComm(object client)
|
|
{
|
|
TcpClient tcpClient = (TcpClient)client;
|
|
NetworkStream clientStream = tcpClient.GetStream();
|
|
|
|
try
|
|
{
|
|
string data = "";
|
|
do
|
|
{
|
|
byte[] buffer = new byte[tcpClient.ReceiveBufferSize];
|
|
int bytesRead = clientStream.Read(buffer, 0, buffer.Length);
|
|
data += Encoding.UTF8.GetString(buffer, 0, bytesRead);
|
|
} while (clientStream.DataAvailable);
|
|
|
|
//var receivedPeople = JsonConvert.DeserializeObject<Million.SaveData.SavedResultData>(data);
|
|
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// 예외 발생 시 에러 메시지 출력
|
|
Console.WriteLine("Error: " + ex.Message);
|
|
}
|
|
|
|
// 클라이언트 연결 종료
|
|
tcpClient.Close();
|
|
}
|
|
}
|
|
}
|