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 7189659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:20:56+00:00 2026-05-28T19:20:56+00:00

I know this might have been asked a thousand times before, but I can’t

  • 0

I know this might have been asked a thousand times before, but I can’t seem to find any specific information about my case.

I have a C# client program that has to connect to other instances of the client over a LAN. To connect one client to another, I use a TcpListener/TcpClient aproach. Both instances have a listener and are able to create a new client to connect/listen to one another (it is independant of which instance started the connection).

To create the listener, I use the following bit of code:

// In the constructor:
listener = new TcpListener(IPAddress.Any, 32842);
listenThread = new Thread((ThreadStart)ListenForConnections);
listenThread.Name = "ListenThread";
listenThread.IsBackground = true;
listenThread.Start();

// Listening for connections:
private void ListenForConnections()
{
    listener.Start();
    Console.WriteLine("Started listening for connections");
    for (; ; )
    {
        if (listener.Pending())
        {
            using (TcpClient client = listener.AcceptTcpClient())
            {
                // My own layer over the TcpClient.
                AsyncTCPClient other = new AsyncTCPClient(client);
                Console.WriteLine("Connection from " + client.Client.RemoteEndPoint);
                other.Received += DataReceived;
                other.Exception += ExceptionOccurred;
                connections.Add("Player", other);
                other.Start();
            }
        }
        else
        {
            Thread.Sleep(5);
        }
    }
}

To create and connect to another client, I use the following bit of code:

public void Connect(IPEndPoint other)
{
    if (socket == null)
    {
        socket = new TcpClient(AddressFamily.InterNetwork);
        socket.Client.ReceiveBufferSize = 2 * 1024 * 1024;
    }
    // Should force-close the socket after 5 seconds if it can't be closed automatically.
    socket.LingerState = new LingerOption(true, 5);
    socket.BeginConnect(other.Address, other.Port, ConnectionCallback, other);
    IsConnecting = true;
}

The ConnectionCallback given as a parameter to BeginConnect looks like this:

private void ConnectionCallback(IAsyncResult result)
{
    IsConnecting = false;
    IsConnected = socket.Connected;
    if (IsConnected)
    {
        IPEndPoint connectedTo = (IPEndPoint)result.AsyncState;
        stream = socket.GetStream();
        if (Connected != null)
        {
            Connected(this, null);
        }
    }
    else
    {
        if (Exception != null)
        {
            RaiseException(new Exception("Unable to connect to host"));
        }
    }
}

However, everytime I get to the callback, the TcpClient failed to connect to the other instance and the Exception event is thrown. Now what I’ve found while searching around the internet (Google) is that it might have something to do with a firewall on either sides of the connection. But I’ve tested it with all firewalls off, so this can’t be that.

  • 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-05-28T19:20:57+00:00Added an answer on May 28, 2026 at 7:20 pm

    I don’t see Socket.EndConnect() being called in the callback.
    See this in MSDN:
    http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.endconnect.aspx

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

Sidebar

Related Questions

I know that this question might have been asked like 100 times, but, believe
I know this question might have been asked a few times already, but I
I know this might have been probably asked before but through out the net
I know that maybe this question has been asked before, but I can't seem
I know this question might have already been discussed a thousands of times but
I know that this question has been asked loads of times, but I have
I am sure this might have been asked before but I've been pulling my
I know this question has been asked before, but no one has given any
I know this has been posted several times, but I might have made a
I know this has been asked several times and I have read all the

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.