0

So around a week ago I started to test out Sockets in C# and I had an idea to make 1 to 1 chat application which could be opened if a message was sent through the NetworkStream, however, I ran into a problem where once the button is pressed, the form starts up but it is frozen.

This is the code used to start the messaging form

if(args[0] == "liveChat") 
{
    messagingforms msgfrms = new messagingforms(); // The actual messaging form
    msgfrms.Show(); // Shows the messaging form but when it shows, it freezes
    SendMessage("liveChat"); // This is a function I created to send a message through the NetworkStream
}                           //  to start the messaging form for the client

When the messaging form is shown, it starts a new thread which either starts to listen for clients of connects to a server. This is the Client code

// This is a snippet showing what happens when the form loaded
// There are variables such as ips to connect to, threads and NetworkStreams
private void messagingforms_Load(object sender, EventArgs e) // This is what happens when the messaging form loads
{
    ConnectionListenerThread = new Thread(ConnectionListener); 
    ConnectionListenerThread.Start(); // Starts the function below 
}

public static void ConnectionListener()  // This is the code for the client which tries to connect to the server
{
    tcpClient = new TcpClient();
    tcpClient.Connect(connectTo, 4535);
    dataStream = tcpClient.GetStream();
    MessageRecieverThread = new Thread(messageListener);
    MessageRecieverThread.Start(); // An infinite loop that sees if there are any incoming messages
    SendMessage("Test message"); // I did this to test if the connection was successfull, and it is but the form 
}                               //  still wouldnt start responding

Server code for the messaging form

private void messagingform_Load(object sender, EventArgs e)
{

    startConnection = new Thread(awaitConnection);
    startConnection.Start(); // Starts the thread that listens for connections; the function below.

}
public static void awaitConnection()
{
    tcpListener = new TcpListener(IPAddress.Any, MessagingPort); // Starts to accept any incoming clients
    tcpListener.Start(); // Starts the tcp listener
    tcpClient = tcpListener.AcceptTcpClient(); // If there is an incoming client then it accepts it
    dataStream = tcpClient.GetStream(); // Gets the networkStream
    tcpListener.Stop(); // Stops listening for clients
    startListeningForMessages = new Thread(messageListener); // Starts listening for any incoming messages
    startListeningForMessages.Start(); // Starts the thread
}

Quite new to stack overflow, so if the format needs fixing tell me.

Kev
  • 13
  • 5
  • To solve problems like this it is useful to check what is hanging the application. The typical way to do this is to "break all", and check the callstack of the various threads. Use the debug menu to to open the callstack and thread windows if you need to. I would probably recommend using the async methods rather than threads. This is a neater method, and reduces the risk of threading errors. – JonasH May 12 '20 at 15:06
  • @JonasH Ok, I am going to test out Async methods, however, I already tried "break all" and It doesn't seem to work anymore for me but I remember the first time, It was close to "external code" – Kev May 12 '20 at 15:23

0 Answers0