내 소켓 서버는 지금까지 매우 간단하다 :TCP C# 소켓 서버에서 여러 활성 연결을 처리하는 방법은 무엇입니까?
public static void listen()
{
TcpListener server = null;
IPAddress address = IPAddress.Parse("127.0.0.1");
try
{
server = TcpListener.Create(5683);
server.Start();
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
while (true)
{
Thread.Sleep(10);
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Accepted Client");
Thread thread = new Thread (new ParameterizedThreadStart(SwordsServer.ClientHandler));
thread.IsBackground = true;
thread.Start(client);
}
}
public static void ClientHandler(object c)
{
TcpClient client = (TcpClient)c;
NetworkStream netstream = client.GetStream();
bool connected = true;
while (connected)
{
Thread.Sleep(10);
try
{
byte[] bytes = new byte[client.ReceiveBufferSize];
netstream.Read(bytes, 0, bytes.Length);
Console.WriteLine("got data");
netstream.Write(bytes, 0, bytes.Length);
}
catch (Exception e)
{
connected = false;
Console.WriteLine(e);
Console.WriteLine(e.StackTrace);
}
}
}
내 질문은, 개념적인 수준에서, 당신은 어떻게 각각의 고유 한 client
을 감시 할 특정 클라이언트에 다른 스레드에서 업데이트를 보낼 수 있습니까?
예를 들어 특정 클라이언트에 대한 데이터가있는 경우이를 브로드 캐스팅하지 않고 어떻게 클라이언트에 전달합니까? 또는 클라이언트가 더 이상 연결되어 있지 않으면 어떻게 알 수 있습니까?
미리 도움을 청하십시오!
목록 개체 목록을 추가하십시오. 클라이언트. –
jdweng
하지만 그 목록에있는 특정 TCP 클라이언트를 어떻게 참조 할 수 있습니까? 나는 어떻게 그들을 구분할 것인가? – MrDysprosium
한 가지 방법은 클라이언트의 IP 주소를 사용하는 것입니다. 또는 각 클라이언트에게 ID를 제공하십시오. – jdweng