Hello. I want to make on the basis of just a permanent connection between the client and the server. But many questions arise. As I understand it, TcpListener in wiretapping mode allows you to get a TcpClient with which you can directly interact? But after connecting and messaging it closes. And I need to maintain an active connection, moreover, on the side of both the client and the server, to respond to every new message that comes from either side.
public class Server { private TcpListener serverListener = null; private NetworkCredential authCredential = null; private int port; public TcpClient client = null; public Server(int port, string userName, string password) { authCredential = new NetworkCredential(userName, password); this.port = port; } public async void StartServer() { IPAddress serverIp = IPAddress.Any; serverListener = new TcpListener(serverIp, port); serverListener.Start(); while (true) { try { using (client = await serverListener.AcceptTcpClientAsync()) { using (NetworkStream networkStream = client.GetStream()) { using (StreamReader streamReader = new StreamReader(networkStream, Encoding.UTF8, false,2048, true)) { string receivedMessage = streamReader.ReadToEndAsync(); } using (StreamWriter writer = new StreamWriter(networkStream)) { writer.Write("какой-то текст"); } } } } catch { } } } }