Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:10:07+00:00 2026-06-02T13:10:07+00:00

I am in desperate need of help. I have essentially created a program that

  • 0

I am in desperate need of help. I have essentially created a program that (will use) encryption to send messages back and forth. The encryption part is working fine, however I am fairly new to Threads and I can not for the life of me get my Client/Server pieces of the application to line up. The chat program is direct IP connection using TCP, so each host is a client and a server. The issue I appear to be having, when I debug, is that the server thread is either not ready when the Client tries to connect to it, or if it is ready it will not relinquish the Thread so the Client can connect! I have been working on this for hours and it is very frustrating.. I hope someone can help! I’ve included my code below.
This is my code snippet from my MainForm, which constructs the Client and Server aspects:

private void InitializeComponent() {

        server = new ServerSide("127.0.0.1",7865);
        servThread = new Thread(new ThreadStart(server.begin));
        client = new ClientSide("127.0.0.1",7865);
        clientThread = new Thread(new ThreadStart(client.begin));
        servThread.Start();
        clientThread.Start();


        //servThread.Join();
        //clientThread.Join();

}

This is my ServerSide code:

public class ServerSide
{
    String IpString;
    int tcpPort;
    bool goodToGo = false;
    System.Net.IPAddress ipAddress = null;
    public ServerSide(String ip, int tcpPort)
    {
        IpString = ip;
        bool isValidIp = System.Net.IPAddress.TryParse(IpString, out ipAddress);
        if (isValidIp == true) // check if the IP is valid, set the bool to true if so
        {
            goodToGo = true;
        }
        else
        {
            goodToGo = false;
        }
    }

    public void begin()
    {
        try
        {
            IPAddress ipAd = IPAddress.Parse(IpString);

            /* Initializes the Listener */
            TcpListener myList = new TcpListener(ipAd, tcpPort);
            Socket s = null;
            /* Start Listening at the specified port */
            while (true)
            {
                myList.Start();

                if (myList.Pending())
                { 
                        s = myList.AcceptSocket();
                         break;
                }

            }

            String toReceive = "";
                    while (true)
                    {
                        byte[] b = new byte[4096];
                        int k = s.Receive(b);
                        for (int i = 0; i < k; i++)
                            toReceive += Convert.ToString((Convert.ToChar(b[i])));

                        // ASCIIEncoding asen = new ASCIIEncoding();

                        var form = MainForm.ActiveForm as MainForm;
                        if (form != null)
                        {
                            form.messageReceived(toReceive);
                        }
                        toReceive = "";
                    }
                }
        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
    }
}

}

ClientSide code:

 public class ClientSide
{
    private String IpString;
    private int tcpPort;
    private TcpClient tcpInt;
    private static Stream stm;
    private System.Net.IPAddress ipAddress = null;
    private bool goodToGo = false;

    public ClientSide(String ip, int tcpPort)
    {

        IpString = ip;
        this.tcpPort = tcpPort;

        bool isValidIp = System.Net.IPAddress.TryParse(IpString, out ipAddress);
        if (isValidIp == true)
        {
            goodToGo = true;
        }
        else
        {
            goodToGo = false;
        }
    }

    public void begin()
    {
        try
        {
            tcpInt = new TcpClient();
            // Console.WriteLine("Connecting.....");



            tcpInt.Connect(IpString, tcpPort);
            // use the ipaddress as in the server program

            // Console.WriteLine("Connected");

            // String str = Console.ReadLine();
            stm = tcpInt.GetStream();
        }

        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
    }


    public void sendMessage(String str)
    {
      //  stm = tcpInt.GetStream();

        ASCIIEncoding asen = new ASCIIEncoding();
        byte[] ba = asen.GetBytes(str);

        stm.Write(ba, 0, ba.Length);

      /**  byte[] bb = new byte[100];
        int k = stm.Read(bb, 0, 100);

        for (int i = 0; i < k; i++)
            Console.Write(Convert.ToChar(bb[i]));*/

    }
}

}

Once again.. what typically happens is my Client receives an error that the host actively refused the connection.. but I do not know how to time them. I am looking for how to have my server listening for TCP Connections, but still manage to go to my other thread to create the TCP Connecion to send the server (I am testing this on localhost currently).
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:10:09+00:00Added an answer on June 2, 2026 at 1:10 pm

    In the constructor of the ServerSide class, you forgot to initialize the tcpPort member. Because you forgot, the server will try to listen to port 0 and the client will try to connect to port 7865, and it will fail.

    Try adding this code to the constructor in the ServerSide class.

    this.tcpPort = tcpPort;
    

    As for the threads, you might have a problem if the client thread tries to connect before the server thread has started listening. You can use a loop to try to connect a number of times (let’s say 10), with a timeout (let’s say 1 second) if you don’t succeed. This way, you will retry 10 times and give up after that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really need help here. I'm desperate at this point. I have NSOperation that
First of all I am in DESPERATE need of help here PLEASE I will
I have a bunch of ZIP files that are in desperate need of some
I am using vb.net 2010 and I have created a program that uses sockets
I am in desperate need of help, I need to manage an application dependency
I'm in desperate need of help. I've been working on this for over 20
Desperate, please help. Will work for food :) I want to be able to
I am in desperate need for some algorithm help when combining lists inside lists.
I'm really desperate. I have WPF MVVM app and I send and recieve some
I desperately need help on this. I've have been looking all over for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.