(Walter Scott - Marmion poem)
I have a problem that I can’t seem to solve… (Again!)
As an experiment, I have connected a Netgear Universal WiFi Internet Adapter (WNCE2001) using a J11D ethernet module on my Fez.Spider.
The WNCE2001 is a bridge which I ‘Assume’ is bidirectional (read and write). See image snap1.gif
I am using a Windows TaskTray application that reads/writes to ‘Udp ports’. I can ‘write’ successfully to the Spider but I never can ‘read’ a message
at the TaskTray application sent by the Spider?
I have tried MANY different ways but always, I can send but never receive. I have used DHCP and Static IP with the same results.
I can always send but never receive.
Currently I favor using a static IP for the WNCE2001 as then I know what the IP should be.
Spider code used to send Udp
const int listenPort = 22000; //Text from TaskTray App.
const int sendPort = 22001; //Text to the tasktray App.
const string SpiderIP = "192.168.2.6"; //Spider - NETGEAR WNCE2001
const string subnetMask = "255.255.255.0";
const string gatewayAddress = "192.168.2.1";
//A socket that sends typically connects, and a socket that listens typically binds?
private void Sendbutton_ButtonPressed(Button sender, Button.ButtonState state)
{
Sendbutton.TurnLEDOff();
string s = "**A test message sent from SPIDER**";
byte[] sendbuf = Encoding.UTF8.GetBytes(s);
sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//Not working
IPAddress ipa = IPAddress.Parse(SpiderIP);
IPEndPoint ep = new IPEndPoint(ipa, sendPort);
//Also Not working
//IPEndPoint ep = new IPEndPoint(IPAddress.Any, sendPort);
//Neither makes a difference (or not using at all)
//sendSocket.Bind(ep);
//sendSocket.Connect(ep);
sendSocket.SendTo(sendbuf, ep);
sendSocket.Close();
display_T35.SimpleGraphics.DisplayText("Sending: " + s, NBFont, Colors.Black, 10, 154);
display_T35.SimpleGraphics.DisplayText(ep.ToString(), NBFont, Colors.Black, 10, 169);
Sendbutton.TurnLEDOn();
}
//
TaskTray App 'Listen" code
void SetupListener()
{
listenerThread = new Thread(new ThreadStart(ListenIncommingThread));
listenerThread.Start();
}
//
//WSAEADDRNOTAVAIL 10049
private void ListenIncommingThread()
{
readSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//// Did not work
//EndPoint anyEndPoint = new IPEndPoint(IPAddress.Any, 0);
//readSocket.Bind(anyEndPoint);
//ep = (EndPoint)anyEndPoint;
//// Did not work
EndPoint listenEP = new IPEndPoint(IPAddress.Parse(SpiderIP), listenPort);
//// WSAEADDRNOTAVAIL 10049 - using Bind - Cannot assign requested address.
//readSocket.Bind(listenEP);
readSocket.Connect(listenEP);
ep = (EndPoint)listenEP;
while (true)
{
if (readSocket.Poll(-1, SelectMode.SelectRead))
{
byte[] inBuffer = new byte[readSocket.Available];
int length = readSocket.ReceiveFrom(inBuffer, ref ep);
if (length > 0)
{
TestDataReceive(inBuffer, length);
readSocket.Close();
}
}
}
}
//
private void TestDataReceive(byte[] data, int length)
{
char[] text = new char[length];
for (int i = 0; i < length; i++)
{
text[i] = (char)data[i];
}
string s = new string(text);
if (useWriteLine) { Console.WriteLine("Data Received: " + s); }
int x = s.CompareTo("EXIT");
if (x == 0) //If 0 equal
{
if (useWriteLine) { Console.WriteLine("The Spider Application is no longer running"); }
}
}
//
Any suggestions?
Have a GREAT DAY!