Sign in to follow this  

TcpListener won't accept connections

This topic is 1897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have two PCs: a desktop and a laptop. Both are running Windows 7. I've written a very basic class library that contains two classes designed around sending and receiving a simple string across the Interwebs. Now, the code within these classes isn't the problem. I know this because I've tested it and it works perfectly. Well, half perfectly.

I copied the library onto both my desktop and laptop and built a very simple console program on both ends for testing. When I set up my desktop as the server and my laptop as the client, the program ran perfectly. I will say this though. When I ran the program for the first time, Windows notified me about VSHOST.EXE and asked me if I wanted to allow it, which I did. Now, when I switched it, running the exact same programs but on opposite computers, my desktop (the client) couldn't form a connection, eventually giving me an 'Error 10060'. Curiously enough, when I ran the server program on my laptop for the first time, Windows didn't ask if I wanted to allow VSHOST.EXE.

Is Windows security preventing incoming connections? And, if it is, how can I ensure that any of my distributed applications won't suffer from this same problem? I plan on having each client also be a listener to listen for random updates (unless there is a better way to do this).

A little more info: The important Listener code
private TcpListener Listener;
Listener = new TcpLIstener(IPAddress.Any, 4040);
Listener.Start();
TcpClient Client = Listener.AcceptTcpClient(); <-Pretty sure the listener isn't making it past this point

Important Client code
public string IP; //pass an IP Address or hostname to this
TcpClient Client = new TcpClient();
IPostEntry Host = Dns.GetHostEntry(IP);
var Address = (from h in Host.AddressList
where h.AddressFamily == AddressFamily.InterNetwork
select h).First();
Client.Connect(Address.ToString(), Port);

try
{
NetworkStream ClientStream = Client.GetStream();
byte[] InfoBuffer = Encoding.ASCII.GetBytes(Info);
ClientStream.Write(InfoBuffer, 0, InfoBuffer.Length);

ClientStream.Close();
}
finally
{
Client.Close();
}

Share this post


Link to post
Share on other sites
Well "Listener.AcceptTcpClient(); <-Pretty sure the listener isn't making it past this point" does block until a connection request is received.
Your sending/receiving code also looks fine
It looks like a firewall is just blocking the connection, just look at firewall settings, i don't think you really can make sure your program will work though a firewall, especially if its listening for a connection, after all that's what they are designed for right? Edited by Xanather

Share this post


Link to post
Share on other sites
Sign in to follow this