By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

C# TcpListener rejecting connections

P: 28
I am working on a simple tcp server written in C#. The problem is that If I initialize it with local IP, it does not accept foreign connections (through static IP).
My PC has two IP's, local and static, how can I connect to it from some other location through static IP, as well as from within the LAN using local IP.

Expand|Select|Wrap|Line Numbers
  1. IPAddress ipAd = IPAddress.Parse("");
  2. TcpListener myList = new TcpListener(ipAd, 8089);
  3. Console.WriteLine("The local End point is: " + myList.LocalEndpoint);
  4. Console.WriteLine("Waiting for a connection.....");
  5. string replyMsg = "Message Received";
  7. while(shouldListen == true)
  8. {
  9.     myList.Start();
  10.     Socket s = myList.AcceptSocket();
  11.     Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
  12.     byte[] b = new byte[100];
  13.     int k = s.Receive(b);
  14.     Console.WriteLine(k.ToString() + " Bytes recieved.");
  15.     for (int i = 0; i < k; i++)
  16.     Console.Write(Convert.ToChar(b[i]));
  17.     ASCIIEncoding asen = new ASCIIEncoding();
  18.     s.Send(asen.GetBytes(responseMsg));
  19.     Console.WriteLine("\nSent Acknowledgement.");
  20.     s.Close();
  21. }
  22. myList.Stop();
if I try to connect it via its static IP, the client shows "connection refused...".
Mar 5 '09 #1
Share this Question
Share on Google+
1 Reply

Expert 5K+
P: 7,872
Why did you use IPAddress.Parse("") ? That is telling your listener to only accept connections coming from that IP address
Generally when setting up a listener you would use IPANY (IPAddress.Any)
Mar 9 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.