473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Exception: A socket operation has encountered a dead network

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();
}

}

}

Jul 8 '07 #1
11 4527
What is "ipaddress"? If it is an address that isn't valid for the local
machine, you will get this exception.

Mike Ober.

"weird0" <am********@gmail.comwrote in message
news:11*********************@r34g2000hsd.googlegro ups.com...
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();
}

}

}


Jul 8 '07 #2
Thanks...

I just wrote one digit wrong in my localhost ip address by mistake.
Jul 9 '07 #3
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.

"weird0" <am********@gmail.comwrote in message
news:11*********************@g4g2000hsf.googlegrou ps.com...
Thanks...

I just wrote one digit wrong in my localhost ip address by mistake.


Jul 12 '07 #4
On Wed, 11 Jul 2007 21:16:19 -0700, Michael D. Ober
<obermd.@.alum.mit.edu.nospamwrote:
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
Jul 12 '07 #5
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

Jul 12 '07 #6
On Wed, 11 Jul 2007 23:28:01 -0700, weird0 <am********@gmail.comwrote:
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
Jul 12 '07 #7
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.

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Wed, 11 Jul 2007 21:16:19 -0700, Michael D. Ober
<obermd.@.alum.mit.edu.nospamwrote:
>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

Jul 12 '07 #8
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.
"weird0" <am********@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
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

Jul 12 '07 #9
On Thu, 12 Jul 2007 07:33:50 -0700, Michael D. Ober
<obermd.@.alum.mit.edu.nospamwrote:
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.
Jul 12 '07 #10

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 12 Jul 2007 07:33:50 -0700, Michael D. Ober
<obermd.@.alum.mit.edu.nospamwrote:
>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?
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.
Jul 12 '07 #11
On Thu, 12 Jul 2007 12:38:51 -0700, Michael D. Ober
<obermd.@.alum.mit.edu.nospamwrote:
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.
Jul 13 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David | last post by:
Hi, Ive been trying to work this out for the past 2 days now and im not getting anywhere fast. The problem i have is that i am using Asynchronous sockets to create a Socket Client library....
4
by: Frank Meng | last post by:
Hi. I am trying a csharp sample from http://www.codeproject.com/csharp/socketsincs.asp . (Sorry I didn't post all the source codes here, please get the codes from above link if you want to try)....
4
by: Qingdong Z. | last post by:
I have an asynchronous Server Socket to push data to client (Socket.BeginSend) when data is available, Meanwhile, the client socket use Synchronous Client Socket to receive the data. I have two...
1
by: Simon M | last post by:
Hi, I'm not sure where to post this as it's about windows forms, web forms, socket communication and embedded objects. I am developing a smart client which is embedded in a webform. This...
6
by: Aero | last post by:
Hi, My window application written in C# is throwing following exception while connecting to one FTP location The type initializer for System.Net.Sockets.Socket threw an exception This exe is...
6
Frinavale
by: Frinavale | last post by:
Hi there, I'm not sure if anyone here can help me but I'm pretty desperate at this point. I've developed a web application that sends emails periodically. Everything works fine in the test...
10
by: John Nagle | last post by:
Here are three network-related exceptions. These were caught by "except" with no exception type, because none of the more specific exceptions matched. This is what a traceback produced: 1....
2
by: kodart | last post by:
Introduction Performance is the main concern to most server application developers. That’s why many of them anticipate using .NET platform to develop high performance server application regardless...
2
by: senglory | last post by:
My WCF: public interface IWorkbookService { DataTable GetDownpayments(KeyValuePair<int, string> sgm); } class WorkbookService : IWorkbookService
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.