Hello, even though I tried I could not connect with the FEZ Spider to a UDP server, for more codes I found on the forum I fail to connect with the platform, using the address 127.0.0.1 emulator running, but not well with the hardware.
I can successfully ping, and when I use the UDP server examples working properly but I can not run as a client.
I use the Hercules software to open the UDP server.
127.0.0.1 is not a valid network address. It’s not “routable” between devices. You need actual (fixed or DHCP allocated) network addresses for this to work. So I think you need to get the addressing sorted out before you can take this further…
Thanks Andre for your comment, I’ll put the code with the correct format.
Hello Brett, I use the 127.0.0.1 IP only for the emulator, using the configuration tools for the device, now has the IP to 192.168.1.9, and my server the 192.168.1.10 in the port 10001.
when I run the code in the emulator everything works properly, but not with the device, (changing the server IP and using the cross cable).
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using GHI.Premium.Net;
namespace UDP_Client_42
{
public class Program
{
public static void Main()
{
Socket SendingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//IPAddress Send_to_Address = IPAddress.Parse("192.168.1.10"); // Device
IPAddress Send_to_Address = IPAddress.Parse("127.0.0.1"); // Emulator
IPEndPoint sending_end_point = new IPEndPoint(Send_to_Address, 10001);
//NetworkStream ns = new NetworkStream(SendingSocket);
byte[] buff = Encoding.UTF8.GetBytes("prueba de socket");
try
{
//ns.WriteByte(55);
SendingSocket.SendTo(buff, sending_end_point);
}
catch (Exception Send_Ex)
{
Debug.Print(Send_Ex.Message);
}
Thread.Sleep(Timeout.Infinite);
}
}
}
I have small code that I usually use for testing UDP on EMX - JD11
You can copy it and just run. It should work. One locate on EMX, and another on PC
EMX: Send / receive
using System;
using Microsoft.SPOT;
using System;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Security;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net;
using Microsoft.SPOT.Net.NetworkInformation;
using GHI.Premium.Net;
using GHI.Premium.Hardware;
using GHINET = GHI.Premium.Net;
namespace Test_UDP_EMX
{
public class Program
{
public static GHINET.EthernetBuiltIn ethernet_J11D;
static InputPort button = new InputPort((Cpu.Pin)30, false, Port.ResistorMode.PullUp); // Please connect a button to pin 30, socket 8
static OutputPort led = new OutputPort((Cpu.Pin)47, true);
static Boolean isNetworkConfigReady = false;
public static void Main()
{
while (button.Read())
{
led.Write(true);
Thread.Sleep(50);
led.Write(false);
Thread.Sleep(50);
}
StartComunication();
}
static void Interface_NetworkAddressChanged(object sender, EventArgs e)
{
isNetworkConfigReady = false;
Debug.Print("New address for the Network Interface ");
Debug.Print("Is DhCp enabled: " + ethernet_J11D.NetworkInterface.IsDhcpEnabled);
Debug.Print("Is DynamicDnsEnabled enabled: " + ethernet_J11D.NetworkInterface.IsDynamicDnsEnabled);
Debug.Print("NetworkInterfaceType " + ethernet_J11D.NetworkInterface.NetworkInterfaceType);
Debug.Print("Network settings:");
Debug.Print("IP Address: " + ethernet_J11D.NetworkInterface.IPAddress);
Debug.Print("Subnet Mask: " + ethernet_J11D.NetworkInterface.SubnetMask);
Debug.Print("Default Gateway: " + ethernet_J11D.NetworkInterface.GatewayAddress);
Debug.Print("Number of DNS servers:" + ethernet_J11D.NetworkInterface.DnsAddresses.Length);
for (int i = 0; i < ethernet_J11D.NetworkInterface.DnsAddresses.Length; i++)
Debug.Print("DNS Server " + i.ToString() + ":" + ethernet_J11D.NetworkInterface.DnsAddresses[i]);
Debug.Print("------------------------------------------------------");
if (ethernet_J11D.NetworkInterface.IPAddress !="0.0.0.0")
isNetworkConfigReady = true;
}
static void StartComunication()
{
ethernet_J11D = new GHINET.EthernetBuiltIn();
if (!ethernet_J11D.IsOpen)
ethernet_J11D.Open();
if (!ethernet_J11D.NetworkInterface.IsDhcpEnabled)
ethernet_J11D.NetworkInterface.EnableDhcp();
ethernet_J11D.NetworkAddressChanged += Interface_NetworkAddressChanged;
GHI.Premium.Net.NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D);
while (isNetworkConfigReady == false) // wait for network is ready
{
Thread.Sleep(50);
}
System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress ip = new IPAddress(new byte[] { your PC ip });
IPEndPoint endPoint = new IPEndPoint(ip, 2000);
socket.Connect(endPoint);
String msg = "Hello PC, I am FEZ spider";
byte[] bytesToSend = Encoding.UTF8.GetBytes(msg);
while (true)
{
socket.SendTo(bytesToSend, bytesToSend.Length, SocketFlags.None, endPoint);
while (socket.Poll(2000000, SelectMode.SelectRead))
{
if (socket.Available > 0)
{
byte[] inBuf = new byte[socket.Available];
EndPoint recEndPoint = new IPEndPoint(IPAddress.Any, 0);
socket.ReceiveFrom(inBuf, ref recEndPoint);
if (!recEndPoint.Equals(endPoint))// Check if the received packet is from the 192.168.0.2
continue;
Debug.Print(new String(Encoding.UTF8.GetChars(inBuf)));
}
}
Thread.Sleep(100);
}
}
}
}
On PC: Send /receive
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace Test_UDP_PC
{
class Program
{
static void Main(string[] args)
{
Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint MyServiceEndPoint = new IPEndPoint(IPAddress.Any, 2000);
mySocket.Bind(MyServiceEndPoint);
while (true)
{
while (mySocket.Poll(200000, SelectMode.SelectRead))
{
if (mySocket.Available > 0)
{
byte[] inBuf = new byte[mySocket.Available];
EndPoint recEndPoint = new IPEndPoint(IPAddress.Any, 0);
mySocket.ReceiveFrom(inBuf, ref recEndPoint);
Console.WriteLine("Message From " + ((IPEndPoint)recEndPoint).Address.ToString());
Console.WriteLine(new string(Encoding.UTF8.GetChars(inBuf)));
String msg = "Hello FEZ Spider! I am PC";
byte[] bytesToSend = Encoding.UTF8.GetBytes(msg);
mySocket.SendTo(bytesToSend, bytesToSend.Length, SocketFlags.None,
(IPEndPoint)recEndPoint);
}
}
}
}
}
}