Basically trying to setup my base functionality that I have used in all my arduino setups.
I listen for a UDP message, then store the IP of the sender to send data back...idea is the application sends a GO commands over UDP the sensors start blaring and send json back to the application.
My issue iss the my socket.RemoteEndPoint is always saying the IPAddress is 0.0.0.0, so now I don't know where to send these messages to.
I'm using an example that I found on here for sending a receiving messages, it might be more than I need but just trying to get this working then remove the overkill. So with the example below the listener.RemoteEndPoint is always saying 0.0.0.0 for the IP address. Please help, just to better explain what I am doing I will put the arduino code below as well.
Thanks everyone, loving the netduino so far happy to get away from c/c++ mashup of the arduino!!!
#region The actual worker thread code /// <summary> /// This is a method which does the work of the worker thread. /// It is a bit like the program's main() function (but is not static). /// It waits (sleeps) for a UDP message to be received and passes it using the /// supplied Interface. /// </summary> private void Main() { int bytes = 0; // Holds number of bytes received var rxBuff = new byte[100]; // Holds received message // Convert IP address and port to an EndPoint IPEndPoint localEndPoint = new IPEndPoint(localIpAddress, port); // Create a "UDP" socket. Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // Bind the socket to the local endpoint and listen for incoming UDP datagrams. try { listener.Bind(localEndPoint); } #region Catch exceptions catch (SocketException se) { SocketErrorCodes errorCode = (SocketErrorCodes)se.ErrorCode; switch (errorCode) { case SocketErrorCodes.NetworkIsDown: Debug.Print("Socket Exception on creation: NetworkIsDown"); break; default: Debug.Print("Socket Exception error: " + errorCode); break; } } #endregion // TODO: If the network was down set up a timer to try again in a while. // Now that we have bound the socket to liste on the port, set a timeout for receive operations // We could just wait for ever, but having a timeout may be useful. listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000); // Loop forever while (true) { bytes = 0; string cause = "unknown"; // TODO: Currently we assume that one whole message will be received in one go // This may not be the case - consider using a length for the first byte and just // read the length at first, followed by the correct amount of following bytes. // Without doing this we might receive less than a whole message or even // parts of two messages. try { //Debug.Print("Attempt to listen on " + localEndPoint.ToString()); bytes = listener.Receive(rxBuff); } #region catch exceptions catch (SocketException se) { SocketErrorCodes errorCode = (SocketErrorCodes)se.ErrorCode; switch (errorCode) { case SocketErrorCodes.NetworkIsDown: // The network is not connected. cause = "NetworkIsDown"; //Debug.Print("Socket Exception on receive: NetworkIsDown"); // Ignore error and carry on break; case SocketErrorCodes.ConnectionTimedOut: // Connection timed out - e.g. nothing received cause = "ConnectionTimedOut"; //Debug.Print("Socket Exception on receive: ConnectionTimedOut"); // Ignore error and carry on break; default: Debug.Print("Socket Exception error: " + errorCode); break; } } #endregion if (bytes == 0) { // Nothing may have been received if a timeout or an error occured. Debug.Print("Nothing received: reason - " + cause); } else { // Create a copy of the message that is the correct length var msg = new byte[bytes]; for (int i = 0; i < bytes; i++) { msg[i] = rxBuff[i]; } // Use interface to pass message to main thread IPEndPoint ip = listener.RemoteEndPoint as IPEndPoint; receiver.SetRemoteIP(ip.Address); receiver.MsgRx(msg); } } } #endregion
void receiveData() { remoteAddress = udp.remoteIP(); // Store IP as Remote IP udp.read(packetBuffer,5); // Read the packet into buffer // **For Testing Only** Serial.println(F("Contents:")); Serial.println(packetBuffer); // **Process Messages** if (strcmp(packetBuffer,"GOGO") == 0) { receivedStart = true; } else if (strcmp(packetBuffer,"STOP") == 0) { receivedStart = false; } }