I have a small program for this example.
I just ran again and it works fine.
Everything you need is change the line below as your PC address
IPAddress ip = new IPAddress(new byte[] {x,x,x,x}); /// This Address must be your PC address
PC:
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, 1234);
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 G120! I am PC";
byte[] bytesToSend = Encoding.UTF8.GetBytes(msg);
mySocket.SendTo(bytesToSend, bytesToSend.Length, SocketFlags.None,
(IPEndPoint)recEndPoint);
}
}
}
}
}
}
G120 -
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_G120
{
public class Program
{
//public static GHINET.EthernetBuiltIn ethernet_J11D;
static EthernetENC28J60 ethernet_NC28J60;// = new WiFiRS9110(SPI.SPI_module.SPI2, GHI.Hardware.G120.Pin.P1_10, GHI.Hardware.G120.Pin.P2_11, GHI.Hardware.G120.Pin.P1_9); // G120
static InputPort button = new InputPort((Cpu.Pin)22, 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_NC28J60.NetworkInterface.IsDhcpEnabled);
Debug.Print("Is DynamicDnsEnabled enabled: " + ethernet_NC28J60.NetworkInterface.IsDynamicDnsEnabled);
Debug.Print("NetworkInterfaceType " + ethernet_NC28J60.NetworkInterface.NetworkInterfaceType);
Debug.Print("Network settings:");
Debug.Print("IP Address: " + ethernet_NC28J60.NetworkInterface.IPAddress);
Debug.Print("Subnet Mask: " + ethernet_NC28J60.NetworkInterface.SubnetMask);
Debug.Print("Default Gateway: " + ethernet_NC28J60.NetworkInterface.GatewayAddress);
Debug.Print("Number of DNS servers:" + ethernet_NC28J60.NetworkInterface.DnsAddresses.Length);
for (int i = 0; i < ethernet_NC28J60.NetworkInterface.DnsAddresses.Length; i++)
Debug.Print("DNS Server " + i.ToString() + ":" + ethernet_NC28J60.NetworkInterface.DnsAddresses[i]);
Debug.Print("------------------------------------------------------");
if (ethernet_NC28J60.NetworkInterface.IPAddress !="0.0.0.0")
isNetworkConfigReady = true;
}
static void StartComunication()
{
//ethernet_J11D = new GHINET.EthernetBuiltIn();
ethernet_NC28J60 = new GHI.Premium.Net.EthernetENC28J60(SPI.SPI_module.SPI2, GHI.Hardware.G120.Pin.P1_17, GHI.Hardware.G120.Pin.P2_21, GHI.Hardware.G120.Pin.P1_14, 4000); // G120
//ethernet_J11D = new GHI.Premium.Net.EthernetENC28J60(SPI.SPI_module.SPI1, GHI.Hardware.EMX.Pin.IO15, GHI.Hardware.EMX.Pin.IO46, GHI.Hardware.EMX.Pin.IO6, 4000); // socket 9
if (!ethernet_NC28J60.IsOpen)
ethernet_NC28J60.Open();
if (!ethernet_NC28J60.NetworkInterface.IsDhcpEnabled)
ethernet_NC28J60.NetworkInterface.EnableDhcp();
ethernet_NC28J60.NetworkAddressChanged += Interface_NetworkAddressChanged;
GHI.Premium.Net.NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_NC28J60);
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[] {x,x,x,x}); /// This Address must be your PC address
IPEndPoint endPoint = new IPEndPoint(ip, 1234);
socket.Connect(endPoint);
String msg = "Hello PC, I am G120";
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);
}
}
}
}