@Markus Freitag , based on my test, I reproduced your problem about the app will still be in an infinite loop when you close the server app.
I made some changes on your code about StartServer method, now it could work it well when you connected to client and click the close button.
Server code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
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);
Thread ThreadingServer;
public BinaryReader br;
public BinaryWriter bw;
private void Form1_Load(object sender, EventArgs e)
{
ThreadingServer = new Thread(StartServer);
ThreadingServer.Start();
}
private void THREAD_MOD(string teste)
{
txtStatus.Text += Environment.NewLine + teste;
}
private void StartServer()
{
try
{
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 clientStream = clientSocket.GetStream();
br = new BinaryReader(clientStream);
string receive = null;
receive = br.ReadString();
txtStatus.Invoke((MethodInvoker)delegate { txtStatus.Text += receive; });
}
catch
{
if (clientSocket.Connected == false)
{
MessageBox.Show("the connection is closed");
}
else
{
MessageBox.Show(ex.ToString());
}
clientSocket.Close();
}
}
}
catch (Exception ex)
{
if (clientSocket.Connected == false)
{
MessageBox.Show("the connection is closed");
}
else
{
MessageBox.Show(ex.ToString());
}
clientSocket.Close();
}
finally
{
ServerListener.Stop();
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
clientSocket.Close();
}
}
Client code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private TcpClient client;
public BinaryReader br;
public BinaryWriter bw;
private void Form1_Load(object sender, EventArgs e)
{
Thread myThread = new Thread(ClientA);
myThread.Start();
}
private void ClientA()
{
client = new TcpClient("127.0.0.1", 8888);
Console.WriteLine("Connected to server");
txtStatus.Invoke((MethodInvoker)delegate { txtStatus.Text = "Connected to server"; });
while (true)
{
try
{
NetworkStream clientStream = client.GetStream();
br = new BinaryReader(clientStream);
string receive = null;
receive = br.ReadString();
txtEnviar.Invoke((MethodInvoker)delegate { txtEnviar.Text = receive; });
}
catch
{
MessageBox.Show("the server connection is closed!");
break;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
NetworkStream clientStream = client.GetStream();
bw = new BinaryWriter(clientStream);
bw.Write(txtEnviar.Text);
}
}
Best regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.