Hello,
I am looking for a good example server client.
With reconnection in case of error, both sides.
When I send a request, I want a response in a certain time.
How can I achieve this?
When I close the application everything should close cleanly. However, I am in an infinite loop. What is the best way to do this?
Client
namespace Client
{
public partial class Client : Form
{
public Client()
{
InitializeComponent();
}
TcpClient MyTcpClient = new TcpClient();
//Declare and Initialize the IP Adress
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
//Declare and Initilize the Port Number;
int PortNumber = 8888;
private void Client_Load(object sender, EventArgs e)
{
Console.WriteLine("Connecting.....");
try
{
MyTcpClient.Connect(ipAd, PortNumber);
txtStatus.Text += Environment.NewLine + "Connected";
txtStatus.Text += Environment.NewLine + "Enter the string to be transmitted";
}
catch { }
}
private void btnSend_Click(object sender, EventArgs e)
{
String str = txtEnviar.Text + "$";
Stream stm = MyTcpClient.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
txtStatus.Text += Environment.NewLine + "Transmitting...";
//Console.WriteLine("Transmitting.....");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100); // ** Timeout ? reconnect....
string Response = Encoding.ASCII.GetString(bb);
txtStatus.Text += Environment.NewLine + "Response from server: " + Response;
}
}
}
Server
namespace Server
{
public partial class Server : Form
{
public Server()
{
InitializeComponent();
}
//Declare and Initialize the IP Address
static IPAddress ipAd = IPAddress.Parse("127.0.0.1");
//Declare and Initilize the Port Number;
static int PortNumber = 8888;
/* Initializes the Listener */
TcpListener ServerListener = new TcpListener(ipAd, PortNumber);
TcpClient clientSocket = default(TcpClient);
private void Server_Load(object sender, EventArgs e)
{
Thread ThreadingServer = new Thread(StartServer);
ThreadingServer.Start();
}
private void THREAD_MOD(string teste)
{
txtStatus.Text += Environment.NewLine + teste;
}
private void StartServer()
{
Action<string> DelegateTeste_ModifyText = THREAD_MOD;
ServerListener.Start();
Invoke(DelegateTeste_ModifyText, "Server waiting connections!");
clientSocket = ServerListener.AcceptTcpClient();
Invoke(DelegateTeste_ModifyText, "Server ready!");
while (true) // Operator press close the application or cancel -- However, I am in an infinite loop. What is the best way to do this?
{
try
{
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[20];
networkStream.Read(bytesFrom, 0, 20);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Invoke(DelegateTeste_ModifyText, $"From Client: {dataFromClient} !");
string serverResponse = "Received!";
Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
}
catch
{
ServerListener.Stop();
ServerListener.Start();
Invoke(DelegateTeste_ModifyText, "Server waiting connections!");
clientSocket = ServerListener.AcceptTcpClient();
Invoke(DelegateTeste_ModifyText, "Server ready!");
}
}
}
}
}

