Problems with sending a UDP packet ( static IPAdress)

I have a Spider, and I added the module: Ethernet_J11D.
When I trying to send a UDP packet I get an Exception (while receiving UDP packets goes fine, without no problems).

the major code:

    // GTM.Module definitions
    Gadgeteer.Modules.GHIElectronics.Ethernet_J11D ethernet_J11D1;
    Gadgeteer.Modules.GHIElectronics.UsbClientDP usbClientDP;

    public static void Main()
    {
        //Important to initialize the Mainboard first
        Mainboard = new GHIElectronics.Gadgeteer.FEZSpider();			

        Program program = new Program();
        program.InitializeModules();
        program.ProgramStarted();
        program.Run(); // Starts Dispatcher
    }

    private void InitializeModules()
    {   
        // Initialize GTM.Modules and event handlers here.		
        usbClientDP = new GTM.GHIElectronics.UsbClientDP(1);
	
        ethernet_J11D1 = new GTM.GHIElectronics.Ethernet_J11D(7);

    }

   Socket Sender;
    Socket Receiver;
    // This method is run when the mainboard is powered up or reset.   
    void ProgramStarted()
    {
        
        ethernet_J11D1.Interface.Open();

        NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D1.Interface);

        //not working
        // ethernet.UseStaticIP(ethernet.NetworkSettings.IPAddress, ethernet.NetworkSettings.SubnetMask
        //                      , ethernet.NetworkSettings.GatewayAddress, ethernet.NetworkSettings.DnsAddresses);

        ethernet_J11D1.Interface.NetworkInterface.EnableStaticIP(IPAddress.GetDefaultLocalAddress().ToString(), "255.255.0.255",
            ethernet_J11D1.NetworkSettings.GatewayAddress);//, ethernet.NetworkInterface.DnsAddresses);

        //not working
        ethernet_J11D1.NetworkUp += new Module.NetworkModule.NetworkEventHandler(EthernetNetworkUp);

        Sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        Receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        Receiver.Bind(new IPEndPoint(IPAddress.Any, 12340));
        
        try
        {
            //a valid end point
            var endP = new IPEndPoint(new IPAddress(new byte[] {198, 168, 0, 10}), 44544);

            //throws the excption
            Sender.SendTo(new byte[] { 22, 2, 2 }, endP);
        }
        catch (Exception e)
        {
            Debug.Print(e.Message);
        }

the exception:
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::sendto [IP: 0000] ####
#### System.Net.Sockets.Socket::SendTo [IP: 0022] ####
#### System.Net.Sockets.Socket::SendTo [IP: 0011] ####
#### GadgeteerApp2.Program::ProgramStarted [IP: 00be] ####
#### GadgeteerApp2.Program::Main [IP: 0015] ####
#### SocketException ErrorCode = 10065
#### SocketException ErrorCode = 10065
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10065
#### SocketException ErrorCode = 10065

my SW/FW versions:
Microsoft .NET Micro Framework v4.2 (QFE2)

SDK Versions:
GHI Premium SDK v1.0.2
GHI OSHW SDK v1.0.3
GHI .NET Gadgeteer v1.6.12.0

Libraries Versions:
Premium libs v4.2.9.0
OSHW libs v4.2.4.0

Firmware Version:
EMX (spider) v4.2.9.0, TinyBooter v4.2.9.0

@ Matan - There has been many posts recently on how to initialize an Ethernet adapter with the 4.2 SDK. The process changed with 4.2. You seem to be mixing 4.1 and 4.2 initialization code.

Try following the initialization procedure from this project. http://www.tinyclr.com/codeshare/entry/651

@ Matan - Welcome to the forum!

In addition to the above.

Try to move your sending code into a separate thread or one of the event handlers. ProgramStarted method is not the right place for it.

andre, thanks for the tip.


// GTM.Module definitions
Gadgeteer.Modules.GHIElectronics.Ethernet_J11D ethernet_J11D1;
Gadgeteer.Modules.GHIElectronics.UsbClientDP usbClientDP;

public static void Main()
{
//Important to initialize the Mainboard first
Mainboard = new GHIElectronics.Gadgeteer.FEZSpider(); 

Program program = new Program();
program.InitializeModules();
program.ProgramStarted();
program.Run(); // Starts Dispatcher
}

private void InitializeModules()
{ 
// Initialize GTM.Modules and event handlers here. 
usbClientDP = new GTM.GHIElectronics.UsbClientDP(1);

ethernet_J11D1 = new GTM.GHIElectronics.Ethernet_J11D(7);

}

Socket Sender;
Socket Receiver;
// This method is run when the mainboard is powered up or reset. 
void ProgramStarted()
{

ethernet_J11D1.Interface.Open();

NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D1.Interface);

//not working
// ethernet.UseStaticIP(ethernet.NetworkSettings.IPAddress, ethernet.NetworkSettings.SubnetMask
// , ethernet.NetworkSettings.GatewayAddress, ethernet.NetworkSettings.DnsAddresses);

ethernet_J11D1.Interface.NetworkInterface.EnableStaticIP(IPAddress.GetDefaultLocalAddress().ToString(), "255.255.0.255",
ethernet_J11D1.NetworkSettings.GatewayAddress);

//not working
ethernet_J11D1.NetworkUp += new Module.NetworkModule.NetworkEventHandler(EthernetNetworkUp);

Sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Receiver.Bind(new IPEndPoint(IPAddress.Any, 12340));

try
{
//a valid end point
var endP = new IPEndPoint(new IPAddress(new byte[] {198, 168, 0, 10}), 44544);

//throws the excption
Sender.SendTo(new byte[] { 22, 2, 2 }, endP);
}
catch (Exception e)
{
Debug.Print(e.Message);
}


Architect,
You’re absolutely right. I have an original code, that includes hundred of lines, and there the receiver and the sender are managed in separate threads/event handlers. I had a specific problem with the

Socket.SendTo

method. So to be specific, a stared a new little project, that focuses on my problem.
In bottom line, I have a problem in this little program, that I want to solve.

Mike, as I mentioned, I’m working on that latest versions of the SW\FW. My code is a syntax that compiles and runs. If there is “old” syntax, you can blame the dll, that still saves that the old syntax… (or the developers that didn’t removed it from him…)

[quote]Try following the initialization procedure from this project. http://www.tinyclr.com/codeshare/entry/651
[/quote]

Mike, thanks. I will check it out.

andre, Thanks. I will check it out.

Socket error 10065 indicates that server is unreachable.

[quote]the correct code to setup a static ip would be
ethernet_J11D1.NetworkInterface.EnableStaticIP(“192.168.0.10”, “255.255.255.0”, “192.168.0.1”);[/quote]

Andre, your code worked. Thanks!