WSAENETDOWN SocketException on a FEZ Spider kit - c#

I'm trying to write a TCP server in .NETMF 4.3 for my FEZ Spider kit.
public partial class Program
{
void ProgramStarted()
{
ethernetJ11D.NetworkInterface.Open();
ethernetJ11D.UseStaticIP("192.168.0.8", "255.255.255.0", "192.168.0.1");
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
EndPoint endPoint = new IPEndPoint(IPAddress.Any, 7777);
serverSocket.Bind(endPoint);
serverSocket.Listen(10);
new Thread(() =>
{
while (true)
{
Debug.Print("Network up: " + ethernetJ11D.IsNetworkUp);//true
Debug.Print("Network connected: " + ethernetJ11D.IsNetworkConnected);//true
System.Net.Sockets.Socket clientSocket = serverSocket.Accept();//exception!
new Thread(new Request(clientSocket).Process);
}
}).Start();
}
The serverSocket.Accept method throws a SocketException with error code 10050. This page says it's WSAENETDOWN: Network is down. However the ethernetJ11D's properties state that the connection is up and I can ping the device without any problems. What am I doing wrong?
EDIT
When I tried running the following client I got the same exception on the socket.Connect call.
ethernetJ11D.NetworkInterface.Open();
ethernetJ11D.UseStaticIP("192.168.0.8", "255.255.255.0", "192.168.0.1");
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("192.168.0.5"), 7777);
socket.Connect(endpoint);
Debug.Print("connected");
byte[] msg = new byte[] { 7, 77, 222 };
socket.Send(msg);
EDIT 2
I've tried to implement the UDP client instead. It doesn't work either.

I've solved the problem:
Change IPAddress.Any to a static IP,e.g. IPAddress.Parse("192.168.0.8").
Delay Bind by adding Thread.Sleep(3000) right before it. It seems that the interface's network settings configuration takes some time.

Related

C# Unable to connect to localhost

I'm writing a very simple .NET TCP Server and very simple TCP Client that should both run on the same machine (Window 10 Home PC) and connect each other (for testing purposes only).
In the server I'm waiting for the connection in this way:
public static void StartListening()
{
string hostname = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, Properties.Settings.Default.Port);
Socket listener = new Socket(
ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(Properties.Settings.Default.Port);
while (true)
{
allDone.Reset();
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
public static void AcceptCallback(IAsyncResult ar)
{
allDone.Set();
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
var frames = Directory.EnumerateFiles(Properties.Settings.Default.FramesPath);
foreach (string filename in frames)
{
Console.Write("Sending frame: {}...", filename);
SendFile(handler, filename);
Thread.Sleep(Properties.Settings.Default.FrameDelay);
}
}
In the client I'm creating a connection in this way:
private void startClient(string host, int port)
{
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(host), port);
ClientTCP = new TcpClient();
ClientTCP.Connect(serverEndPoint);
Reader = new StreamReader(ClientTCP.GetStream());
Listen = true;
listner = Listener();
}
startClient is called in this way.
startClient(txtAddr.Text, (int)int.Parse(txtPort.Text));
When I run the client setting host variable to the current machine name (the same the server retrieve trough Dns.GetHostName() I got this exception:
An invalid ip address was specified.
I tried using 127.0.0.1 and I got:
Connection could not be established. Persistent rejection of the target computer 127.0.0.1:5002
I tried with localhost and I got
An invalid ip address was specified
again. I tried with the IP address assigned to WiFi and I got again
Connection could not be established. Persistent rejection of the target computer 192.168.10.11:5002
I'm sure the computer network works since I'm using it for many other things including connecting to local TCP services and I'm sure both client and server code works since on a different PC I'm able to connect them setting localhost as client host variable value. Why I'm unable to use loopback connections in my code?
Where did I fail?
P.S. I allowed connection to server binary in Windows firewall rules, I also allowed outgoing connection for client binary also.
For anyone willing to inspect the server code here it is:
Server code
You are using the constructor with the "IPEndPoint" parameter. This constructor does not connect to the server automatically and you must call the "Connect" method before using the socket. That is why you are getting the error message "Operation not allowed on unconnected sockets". Also, you have provided a wrong IPEndPoint for client.
Please try this on client:
private void startClient(string host, int port)
{
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(host), port);
ClientTCP = new TcpClient();
ClientTCP.Connect(serverEndPoint);
Reader = new StreamReader(ClientTCP.GetStream());
Listen = true;
listner = Listener();
}
You may need some exception handling here, but it should work now.
UPDATE:
The ipHostInfo.AddressList may have more than one addresses and just one of them is what you wanted. It is not necessarily the first one. I specified this manually and it works:
//string hostname = Dns.GetHostName();
//IPHostEntry ipHostInfo = Dns.GetHostEntry("127.0.0.1");
//IPAddress ipAddress = ipHostInfo.AddressList[0];
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 5002);

Send a UDP message to another network using socket

I've been making a sample program wherein the user can broadcast messages using sockets and UDP connection. It was successful in LAN but I can't broadcast my messages to other networks (e.g. 10.15.1.11's message to 10.11.1.23). Here's my sample code:
Listener:
bworker = sender as BackgroundWorker;
Socket _ListenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint _ListenerEndPoint = new IPEndPoint(IPAddress.Any, _port);
_ListenerSocket.EnableBroadcast = true;
_ListenerSocket.Bind(_ListenerEndPoint);
//_ListenerSocket.Connect(MulticastIP, _port);
_ListenerSocket.Ttl = 255;
_ListenerSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(MulticastIP));
while (true)
{
byte[] msg = new byte[1024];
_ListenerSocket.Receive(msg);
string StringData = Encoding.Unicode.GetString(msg);
bworker.ReportProgress(0, StringData);
}
Sender:
Socket _ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_ClientSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(MulticastIP));
_ClientSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, int.Parse(ttl));
IPEndPoint _ClientEndPoint = new IPEndPoint(MulticastIP, _port);
_ClientSocket.Connect(_ClientEndPoint);
byte[] MsgByte = new byte[1024];
MsgByte = Encoding.Unicode.GetBytes(txtmsg.Text);
_ClientSocket.Send(MsgByte);
Variables:
public const int _port = 8041;
public const string ttl = "255";
public IPAddress MulticastIP = IPAddress.Parse("239.0.0.222");
Thanks.
The router between you and the other LAN probably refuses to forward packets with multicast destination IPs. To handle multicast properly, the router itself has to be multicast-aware and implement protocols such as PIM (for coordinating multicast between routers) and IGMP (for coordinating multicast with end-hosts)
Your router probably doesn't forward multicast packages. In order for multicast to work all routers along the path of communication must me multicast enabled. Ping only requires the router to forward ping packages so that will really only tell you if you can reach the other computer at all. Take a look at this article to learn more about multicasting in C#.

Suddenly server cannot connect due to SocketException

I am using this server example from MSND: http://msdn.microsoft.com/en-us/library/fx6588te.aspx
and from some reason my server refuse to connect at this function at _listener.Bind(localEndPoint); with error Only one usage of each socket address (protocol/network address/port) is normally permitted
Until a few minutes ago I had no problems Sarver connect without problems and suddenly it happens
public static void StartListening(IPAddress ipAddress, int port)
{
_isServerRunning = true;
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".
//IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
//IPAddress ipAddress = IPAddress.Parse("192.168.0.100"); //ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
_listener.Bind(localEndPoint);
_listener.Listen(100);
while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
_listener.BeginAccept(
new AsyncCallback(AcceptCallback),
_listener);
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
//Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
What does this error means ?
It most likely means that the local port is still reserved. This can be idther because another process is still runnoing and holding the sockewt open, or because the timeout period defined by th dSO_LINGER socket option is not timed out. I'm not familiar with c# but there should be a way to set th elinger timeout.
You can tell the current status of the socket by using the netstat command.
I think you can find some solutions here http://blogs.msdn.com/b/dgorti/archive/2005/09/18/470766.aspx

Sending data over UDP to a destination with multiple clients open

I am currently using this function to send data from the server to the clients
private static void send_message(string ip, string message)
{
byte[] packetData = System.Text.UTF8Encoding.UTF8.GetBytes(message);
int port = 11000;
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ip), port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.SendTo(packetData, ep);
}
But this would mean a destination IP/port can only have one client open to receive the data, because having two clients open would mean one client can retrieve data that was meant for another (if I'm correct).. how do I solve this?
Receiving function:
private static Int32 port = 11000;
private static UdpClient udpClient = new UdpClient(port);
public static void receive_threaded()
{
Thread t = new Thread(() =>
{
while (true)
{
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, port);
byte[] content = udpClient.Receive(ref remoteIPEndPoint);
if (content.Length > 0)
{
string message = Encoding.UTF8.GetString(content);
parseMessage(message);
}
}
});
t.Start();
}
1) You should implement some kind of protocol so that your server has a "well known" port to accept connections. Use this port to inform your client ANOTHER port where the client must connect. Use a different port for each client.
Your client conects to the server at 11000. Your server assigns a unique port for the client, let's say 11001 for the firts client. Then the server opens a connection at 11001. The client closes connection at 11000 and opens a new connection at 11001 to receive the data.
2) Why UDP?
I don't see why you need to open a new socket at all. You already have each client's address and port, from the first packet they sent you. Just send a packet to that address:port. I absolutely don't get the other suggestion of setting up extra ports either.

Trouble getting sockets to connect in windows7 64bit

Hey. I've been searching around for a solution to this problem with no luck. I was wondering if this is a known issue when switching socket code from WinXP 32 bit to Win7 64 bit. I have a fairly simple socket routine which works fine in WinXP 32bit, but the socket.connect call is throwing the exception "No connection could be made because the target machine actively refused it 127.0.0.1:48000"
I've added an exception to the win7 firewall for the program, and doubled checked to make sure the rule it added was allowing all ports.
The code I use to setup these simple sockets is as follows:
Listening Socket:
byte[] bytes = new Byte[8192];
IPHostEntry ipHostInfo = Dns.GetHostEntry("localhost");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 48000);
_ListenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
_ListenerSocket.Bind(localEndPoint);
_ListenerSocket.Listen(1000);
while (_Running)
{
_ListenerSync.Reset();
_ListenerSocket.BeginAccept(new AsyncCallback(AcceptCallback), _ListenerSocket);
_ListenerSync.WaitOne();
}
_ListenerSocket.Shutdown(SocketShutdown.Both);
_ListenerSocket.Close();
}
Connecting Socket:
IPAddress _IP;
IPAddress.TryParse("127.0.0.1", out _IP)
Socket tTarget = null;
if (tTarget == null)
{
tTarget = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
tTarget.Connect(_IP, 48000);
_Connected = true;
byte[] tBuffer = new byte[8192];
string tRecvBuff = "";
while (_Connected)
{
int tRecv = tTarget.Receive(tBuffer);
//{ does stuff here }
}
Seems like everything works until tTarget.Connect(), where it pauses for a second and then throws the exception listed above. AcceptCallback is never called.
Thanks.
Based on your comment your listening on IPV6. Instead of
ipHostInfo.AddressList[0]
try
ipHostInfo.AddressList.ToList().Find(p=>p.AddressFamily==AddressFamily.InterNetw‌​ork);

Categories