0
내 멀티 스레드 서버에서 클라이언트라고하는 클래스가 있습니다.매개 변수를 사용하여 기존 클래스를 호출하는 방법?
내 질문은 어떻게 다른 클래스에서 지정된 클라이언트로 데이터를 보내나요? ServerMain 클래스의 Listen 함수는 다음과 같습니다.
public static List<Client> clients;
public static List<Thread> threads;
private void Listen()
{
clients = new List<Client>();
threads = new List<Thread>();
int id = 0;
while (true)
{
listenerSocket.Listen(0);
Log.Status(" Waiting for a connection...");
var commands = new ServerCommands();
//commands.Wait();
Client c1 = new Client(id, listenerSocket.Accept());
clients.Add(c1);
Log.Status("New Client Connected!");
Thread t = new Thread(c1.Start);
c1.SetThread = t;
t.Start();
id++;
}
}
그리고 나의 클라이언트 클래스 하나는 내가 뭔가 식별 실종이
foreach(Client item in ServerMain.clients) //Client clients
{
Console.WriteLine(item._id);
Console.WriteLine(item.Name);
Console.WriteLine(item._guid);
};
처럼 연결된 모든 클라이언트에 보낼 수있어 예
public class Client : IDisposable
{
public int _id;
public string _guid;
public string Name;
public Socket clientSocket;
private Thread thread;
public Client(int id, Socket socket)
{
this._id = id;
this._guid = Guid.NewGuid().ToString();
this.clientSocket = socket;
}
public Thread SetThread
{
set
{
this.thread = value;
}
}
public int Id
{
get
{
return this._id;
}
}
public void Receive()
{
byte[] buffer;
int readBytes;
while (clientSocket != null && clientSocket.Connected)
{
try
{
buffer = new byte[clientSocket.SendBufferSize];
readBytes = clientSocket.Receive(buffer);
if (readBytes > 0)
{
Packet p = new Packet(buffer);
if (p.Type != PacketType.Disconnect)
{
new Task(() => Received(p)).Start();
}
else
{
CloseConnection();
}
}
}
catch (SocketException e)
{
Console.WriteLine(e);
CloseConnection();
}
}
}
////////// Example Send Fuction ////////////
private void Register(User user)
{
var res = Handler.RegisterDo(user);
clientSocket.Send(res.ToBytes());
}
}
를 보내? 이드는 그것을 할 수 있었지만 (내가 생각한다) 어떻게 외부에서 호출 할 수 있습니까?
예 클라이언트 클래스의 Iam이 작동하지만 예를 들어 다른 클래스의 ID 0 인 클라이언트에 데이터를 보낼 수있는 방법을 알고 싶습니다. 또는 Client.SendDataTo (id, data)와 같은 것을 호출 할 수 있도록 클래스의 ID로 선택된 클라이언트 (소켓)를 어떻게 바꿀 수 있습니까 – heizung124
또는 이러한 종류의 동작을 구현하는 방법 – heizung124