It is late, and I may be missing something, but it seems that something is funny with the Poll method.
Here is the code for an echo server:
u
sing System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Net.Sockets;
using GHIElectronics.NETMF.Net.NetworkInformation;
using System.Text;
//using Socket = GHIElectronics.NETMF.Net.Sockets.Socket;
namespace EchoServer
{
public class EchoServer
{
private static int sessionCount = 0;
private const int MAX_SESSIONS = 4;
private static Socket newSessionSocket = null;
public static void Main()
{
const Int32 c_port = 80;
byte[] ip = { 192, 168, 1, 201 };
byte[] subnet = { 255, 255, 255, 0 };
byte[] gateway = { 192, 168, 1, 1 };
byte[] mac = { 43, 185, 44, 2, 206, 127 };
WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di9);
NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);
NetworkInterface.EnableStaticDns(new byte[] { 192, 168, 1, 1 });
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, c_port);
server.Bind(localEndPoint);
server.Listen(1);
Debug.Print("*** Echo Server Listening");
while (true)
{
// check for four running sessions
if (sessionCount < MAX_SESSIONS)
{
// Wait for a client to connect.
newSessionSocket = server.Accept();
Debug.Print("New Sesion");
Thread newSession = new Thread(new ThreadStart(SessionThread));
newSession.Start();
Thread.Sleep(0);
Interlocked.Increment(ref sessionCount);
}
else
{
Thread.Sleep(250);
}
}
}
static private void SessionThread()
{
byte[] buffer = new byte[1024];
Socket socket = newSessionSocket;
socket.ReceiveTimeout = -1;
while (true)
{
try
{
if (socket.Poll(-1, SelectMode.SelectRead))
{
int bytesToRead = socket.Available;
Debug.Print("bytesToRead: " + bytesToRead.ToString());
if (bytesToRead == 0)
{
throw new Exception("Remote Disconnect");
}
else
{
int bytesRead = socket.Receive(buffer, bytesToRead, SocketFlags.None);
socket.Send(buffer, bytesRead, SocketFlags.None);
}
}
else
{
Debug.Print("Poll False");
}
}
catch (Exception ex)
{
socket.Close();
Interlocked.Decrement(ref sessionCount);
break;
}
}
}
}
}
The server is listening on port 80. I used the MS telnet program from the command line and did a connect to the server. (telnet 192.168.1.201 80).
The connection is accepted. After a few seconds, I type a character. The character is received at the server, and echoed. It appears back at the PC.
When the code then issues the poll again, the poll method return with a true and when the Available property is checked it shows zero bytes are available, which is a remote hangup indication.
The server then closes the socket.
The telnet program then says the remote host has disconnected. The old finger pointing problem
After receiving the first byte, and sending it back, I did not expect the poll method to return true with a zero data available property on the second call to it.
I normally never use the poll method, but I am using it now to get around the Receive() blocking question. I might not understand its use.