Exception: A socket operation has encountered a dead network

W

weird0

I am a beginner to socket programming to in c#. I know the concepts
as to how to program them. I get the above exception when i tried
writing the code for the server side on the line tcpListener.Start()
when i passed the localhost ip and port no. 4000:

Please help...

namespace Server
{
class Server
{
TcpListener tcpListener;
IPAddress ipAddress;
IPEndPoint remoteEP;

public Server(string ipaddress, int Port)
{
ipAddress = IPAddress.Parse(ipaddress);
tcpListener = new TcpListener(ipAddress,Port);
remoteEP = new IPEndPoint(ipAddress, Port);
}

public void Start()
{
try
{
tcpListener.Start(); // line of exception
}
catch (Exception ex)
{
MessageBox.Show("Problem:" + ex.Message);
}
}

public void Stop()
{
tcpListener.Stop();
}

}

}
 
M

Michael D. Ober

What is "ipaddress"? If it is an address that isn't valid for the local
machine, you will get this exception.

Mike Ober.
 
M

Michael D. Ober

Scan through the Socket classes and you will find a method that returns a
list of local IP addresses. If you use this method to get the IP address
for the local machine, you will avoid then entire issue of mis-typing your
local address as well as allow your code to work on a different system or
network.

Mike.
 
P

Peter Duniho

Scan through the Socket classes and you will find a method that returns a
list of local IP addresses. If you use this method to get the IP address
for the local machine, you will avoid then entire issue of mis-typing
your
local address as well as allow your code to work on a different system or
network.

I assumed he meant "127.0.0.1", the standard "localhost" IP address that
always refers to the local computer.

You're correct that it's possible to get a list of the local IP addresses
assigned to the local computer. However, it's very unusual for an
application to need those. What they need is the addresses of other
computers (and normally this is handled through normal DNS, or explicitly
typing the IP address for those other computers).

In particular (and this is the main reason I'm bothering to reply) most of
the time when someone is getting the list of local IP addresses, it's
because they incorrectly think that they need to give their own IP address
to some remote process. Not only is this not needed, doing so is likely
to break a variety of scenarios, most often involving NAT routers or proxy
servers (since in those cases, the IP address the local computer is aware
of is not the IP address the remote computer needs to use to communicate
with it).

You are probably aware of this, and the OP may even be aware of it but
just in case, I'm posting this information here. If nothing else,
hopefully someone else noticing the advice to get the local IP addresses
will also see this and avoid doing something they shouldn't. :)

Pete
 
W

weird0

But hey what you told about is a dial-up connection ? or it will work
for cable connection too?

Since, I use cable connection and i have a static IP:

"192.168.0.71"

Reply Plz
 
P

Peter Duniho

But hey what you told about is a dial-up connection ? or it will work
for cable connection too?

The API that will get you the installed network adapters' IP addresses
doesn't care what kind of adapter it is. You will get your _local_ IP
address for each adapter (most people usually only have one, but some
computers do have more than one network adapter installed, especially
laptops and newer PCs with 1394 built in, since those are sometimes mapped
to act as a network adapter).

For dial-up, your local IP address is sometimes the public Internet
routable address, and sometimes it's a DHCP address assigned by your ISP
and routed through their own version of a proxy server or NAT router.

If you have a broadband connection and are directly connection,
again...this could either be your public Internet address, or a
DHCP-assigned address routed through your ISP's servers.

If you have a broadband connection and it goes through a router in your
own home, then you are almost certainly using a NAT router and the
computer has a DHCP-assigned address rather than a public IP address.

The above is not even a complete enumeration of all the possibilities.
It's simply an attempt to illustrate the fact that getting your own
locally-assigned IP address is often not very useful, and in the typical
case is not even needed.
Since, I use cable connection and i have a static IP:

"192.168.0.71"

That's a LAN address, not routable on the Internet. You say it's a static
IP, but if so it's only static within your own LAN. You can use it within
your LAN, but other computers on the Internet won't be able to do anything
useful with that address.

Pete
 
M

Michael D. Ober

OP's code was setting up a listener socket. You need one of your local IPs
(other than 127.0.0.1) for an external system to be able to connect to it.
If you use 127.0.0.1 only applications on your local system can connect.

The code to do this without hard coding your IP address is

using System.Net.Sockets
using System.New

IPAddress ServerAddress = Dns.GetHostEntry(<string
machinename>).AddressList[0];
IPEndPoint LocalHost = new IPEndPoint(ServerAddress, <int port>);
TcpListener tcpServer = new TcpListener(LocalHost);

In VB 2005, here's working code:
Dim ServerAddress As IPAddress =
Dns.GetHostEntry(My.Computer.Name).AddressList(0)

Dim LocalHost As New IPEndPoint(ServerAddress, Port)

tcpServer = New TcpListener(LocalHost)



Mike.
 
M

Michael D. Ober

I wouldn't guarantee any IP address is static unless you are paying extra
for a static address. In that case, you would have a routable address, and
not an address in the 196.168.x.x range. Just because you haven't seen your
address change doesn't mean that it won't.

Mike.
 
P

Peter Duniho

OP's code was setting up a listener socket. You need one of your local
IPs
(other than 127.0.0.1) for an external system to be able to connect to
it.
If you use 127.0.0.1 only applications on your local system can connect.

All true statements. So?

Have you never used your localhost address for testing purposes?

It's not a good idea to specify _any_ specific IP address for a listening
socket anyway. But if one is going to do so, there's nothing
fundamentally wrong with using the localhost address versus some other
locally valid address.
 
M

Michael D. Ober

Peter Duniho said:
All true statements. So?

Have you never used your localhost address for testing purposes?

Yes I have.
It's not a good idea to specify _any_ specific IP address for a listening
socket anyway. But if one is going to do so, there's nothing
fundamentally wrong with using the localhost address versus some other
locally valid address.

If your network adapters, and thus, IP Addresses, are on different networks
and you want only traffic from one of the networks, you must specify the
address. On most systems, this isn't an issue, but it can be.

Mike.
 
P

Peter Duniho

If your network adapters, and thus, IP Addresses, are on different
networks
and you want only traffic from one of the networks, you must specify the
address. On most systems, this isn't an issue, but it can be.

Yup. There certainly are cases where binding to a specific IP address is
needed. I was speaking of the general case, and in any case none of that
suggests that the OP wasn't using the basic localhost address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top