Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / February 2004

Tip: Looking for answers? Try searching our database.

Why connect refuse?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
zhimin - 20 Feb 2004 02:16 GMT
With C#, I had created two threads in one program. One is a TCP listener,
and the other is TcpClient. After the Listener thread started, the client
thread started try to connect to the listener, but I get a exception:
Unhandled Exception: System.Net.Sockets.SocketException: No connection could
be made because the target machine actively refused it
  at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
  at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
  at TetNet.Form2.Run() in e:\mycode\vs.net\c#\tetnet\tetnet\form2.cs:line
106

The following is my code:
Listener:
   IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

   TcpListener listener = new TcpListener(ipAddress, 8253);
   listener.Start();
   Console.WriteLine("Server created, listening.");
   int count = 0;
   while(true)
   {
    Console.Write("Waiting Connect: " + ++count);
    TcpClient client = listener.AcceptTcpClient();
    Console.WriteLine("  Connected " + count);
   }

Client:
  TcpClient client = new TcpClient("zhimin", 8253);

  string str = "Hello, Server";
  byte[] data = Encoding.ASCII.GetBytes(str);
  byte[] lens = this.IntToBytes(data.Length);
  byte[] zeros = this.IntToBytes(0);

  NetworkStream stream = client.GetStream();
  stream.Write(lens, 0, lens.Length);
  stream.Write(data, 0, data.Length);
  stream.Write(zeros, 0, zeros.Length);
  stream.Close();
  client.Close();
Ed Kaim [MSFT] - 20 Feb 2004 07:33 GMT
I'm a little rusty on TCP, but I think binding to "127.0.0.1" makes a server
that will only accept connections addressed to "127.0.0.1". To make a server
that accepts connections to the external IP address as well, you may need to
use "System.Net.IPAddress.Any" instead of the parsed "127.0.0.1".

> With C#, I had created two threads in one program. One is a TCP listener,
> and the other is TcpClient. After the Listener thread started, the client
[quoted text clipped - 35 lines]
>    stream.Close();
>    client.Close();
Chad Z. Hower aka Kudzu - 20 Feb 2004 09:33 GMT
>     IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
>
>     TcpListener listener = new TcpListener(ipAddress, 8253);

Why are you binding the listener to 127?

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
     "Programming is an art form that fights back"
zhimin - 21 Feb 2004 12:10 GMT
> >     IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
> >
> >     TcpListener listener = new TcpListener(ipAddress, 8253);
>
> Why are you binding the listener to 127?

The IP address 127.0.0.1 means the localhost, isn't it?
Justin Rogers - 21 Feb 2004 12:17 GMT
Yeah, it means localhost.  Normally when binding a TcpListener you just want to
do:

TcpListener listener = new TcpListener(IPAddress.Any, portNumber);

Also, if you really do want the localhost only bind then just do

TcpListener listener = new TcpListener(IPAddress.Loopback, portNumber);

Bit quicker, saves you some typing, etc...

Signature

Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

> > >     IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
> > >
[quoted text clipped - 3 lines]
>
> The IP address 127.0.0.1 means the localhost, isn't it?
Chad Z. Hower aka Kudzu - 21 Feb 2004 13:59 GMT
"zhimin" <xiaozhimin1978@163.com> wrote in news:#wKjdPH#DHA.2132
@TK2MSFTNGP10.phx.gbl:
> The IP address 127.0.0.1 means the localhost, isn't it?

Yes. But if you bind your LISTENER to it, it can then ONLY accept connections  
from the same machine. Normally you bind to all and it will then include your
external IPs.

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
     "Programming is an art form that fights back"
zhimin - 21 Feb 2004 15:27 GMT
Thank you, Justin Rogers and Chad Z. Hower aka Kudzu

But I think a TCP listener should need a port number only, why should I
provide a parameter include the information about IP address? And why the
construct method TcpListener(Int32) is deprecated in the new version?

Thanks!
Chad Z. Hower aka Kudzu - 21 Feb 2004 16:24 GMT
"zhimin" <xiaozhimin1978@163.com> wrote in news:u3N6M9I#DHA.1392
@tk2msftngp13.phx.gbl:
> But I think a TCP listener should need a port number only, why should I
> provide a parameter include the information about IP address? And why the

Because on machines with multiple IPs, sometimes you want to listen on just
the internal, or external IP. Or somtimes you want multiple servers on same
port but different IPs.

> construct method TcpListener(Int32) is deprecated in the new version?

No idea.

Just pass IPAddrAny unless you specifically want to bind to a specific IP.

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
     "Programming is an art form that fights back"

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.