I created class to communicate with TCP server like this:
public class Tcp_client_server_communication
{
private static TcpClient client;
private static String response = String.Empty;
public void Initialize(string ip, int port)
{
client = new TcpClient(ip, port);
}
public string BeginRead()
{
var buffer = new byte[16];
var ns = client.GetStream();
ns.BeginRead(buffer, 0, buffer.Length, EndRead, buffer);
}
public void EndRead(IAsyncResult result)
{
var buffer = (byte[])result.AsyncState;
var ns = client.GetStream();
var bytesAvalible = ns.EndRead(result);
MessageBox.Show(Encoding.ASCII.GetString(buffer));
BeginRead();
}
}
To call these methods I use:
.Initialize();
.BeginRead();
Its works fine but I want to show the every message from the server in a multi-line TextBox
not in a MessageBox
.
How can I do this?
Well you could create a List object to hold all the messages passed, then set a Timer to check every so often if the list has been added to. Remove what you've read every tick from the list after adding the message to the textbox.
This project is a little more complicated then that but here's one of my old projects I worked on in middle school as an example : https://github.com/Taloreal/Server-Essentials