Answer the question
In order to leave comments, you need to log in
How to organize a "client-server" architecture on TCP for CCI?
I am developing a training project in c#: a CCG like HearthStone. The network protocol used is TCP.
There are some sketches.
Server class.
public class TCPServer
{
private readonly TcpListener _server;
private readonly List<Message> _messages;
public TCPServer(int port = 8888)
{
var localAddr = IPAddress.Parse("127.0.0.1");
_server = new TcpListener(localAddr, port);
Clients = new List<Client>();
}
public ICollection<Client> Clients { get; set; }
public void Start()
{
_server.Start();
var thread = new Thread(Listen);
thread.Start();
}
private void Listen()
{
while (true)
{
var tcpclient = _server.AcceptTcpClient();
var client = new Client(tcpclient);
Clients.Add(client);
}
}
public void Stop()
{
_server.Stop();
}
public void SendMessage(Message message)
{
throw new NotImplementedException();
}
public void ReadMessage(Message message)
{
throw new NotImplementedException();
}
}
public class Client
{
private readonly TcpClient _tcpClient;
public Client(TcpClient client)
{
_tcpClient = client;
}
public string Identificator { get; set; }
public IMessageConverter<string> Converter { get; set; }
public void Connect()
{
//_tcpClient.Connect(_adress,_port);
}
public void Disconnect()
{
_tcpClient.Close();
}
public void SendMessage(Message message)
{
var stream = _tcpClient.GetStream();
var data = Converter.ConvertToBytes(message);
stream.Write(data, 0, data.Length);
}
public void ReadMessage(Message message)
{
var stream = _tcpClient.GetStream();
var data = new byte[64];
var bytes = 0;
do
{
bytes += stream.Read(data, 0, data.Length);
} while (stream.DataAvailable);
}
}
public class Message
{
public byte[] Content { get; set; }
public int Length { get; set; }
}
public interface IMessageConverter<out T>
{
byte[] ConvertToBytes(Message message);
T ConvertFromBytes(Message message);
}
Answer the question
In order to leave comments, you need to log in
1. You have an extra thread for Accept. Where is it better to make the connection of new clients through BeginAcceptTcpClient
2. Further, it is also better to use asynchronous requests
var stream = tcpClient.GetStream();
var asyncResult = stream.BeginRead(buffer, 0, READ_BUFFER_SIZE, ReadStreamCallback, null);
, which will allow you to easily manage the lifetime of the client. For a study project, it doesn't matter. These questions will only matter in production.
To begin with, it makes sense to make a mechanism that will somehow work.
For example, Client.ReadMessage will have to be rewritten a couple more times, pretty scratching the back of the head, so that it outputs a Message without bugs at the output.
So, don’t worry about streams, do what is easier, clearer and more convenient for you.
And only then, after the lapse of time, you will remake it into a cooler version. Work there for 10 minutes.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question