Main Site Documentation

[TCP Client for .Net Server socket error 10053]


#1

Hi everyone,
i’m trying to build a client server application using Fez Spider as Client and a console .Net application as server. The main issue on the socke.Send. The device results as connected, after a while a make a send call on the socket but I’ve ALWAYS the SocketError =10053.
I’ve disabled firewall, antivirus and all possible stuff but the problem does not change. I post the code for client and server

ServerSide

 using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace SimpleServer
{
    
    public class SynchronousSocketListener
    {

        // Incoming data from the client.
        public static string data = null;

        public static void StartListening()
        {
            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            // Establish the local endpoint for the socket.
            // Dns.GetHostName returns the name of the 
            // host running the application.
            IPAddress ServerIP = new IPAddress(new byte[] { 192, 168, 1, 3 });
            IPEndPoint localEndPoint = new IPEndPoint(ServerIP, 7777);
           

            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and 
            // listen for incoming connections.
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(100);

                // Start listening for connections.
                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");
                    Console.WriteLine(ServerIP.ToString());
                    Thread.Sleep(1000);
                    // Program is suspended while waiting for an incoming connection.
                    Socket handler = listener.Accept();
                    data = null;

                    if (handler.Connected)
                    {
                        Console.WriteLine("Connected");
                    }
                    // An incoming connection needs to be processed.
                    while (true)
                    {
                        bytes = new byte[1024];
                        int bytesRec = handler.Receive(bytes);
                        data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                        if (data.IndexOf("4") > -1)
                        {
                            break;
                        }
                    }

                    // Show the data on the console.
                    Console.WriteLine("Text received : {0}", data);

                    // Echo the data back to the client.
                    byte[] msg = Encoding.ASCII.GetBytes(data);

                    handler.Send(msg);
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            Console.WriteLine("\nPress ENTER to continue...");
            Console.Read();

        }

        public static int Main(String[] args)
        {
            StartListening();
            return 0;
        }
    }
}

Simple client

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace GadgeteerClientTest
{
    public partial class Program
    {
        void ProgramStarted()
        {
            Debug.Print("Program started");

            if (!ethernetJ11D.NetworkInterface.Opened)
            {
                Debug.Print("Opening interface");
                ethernetJ11D.NetworkInterface.Open();
                Debug.Print("Interface opened");
            }
            ethernetJ11D.UseStaticIP("192.168.0.8", "255.255.255.0", "192.168.0.1");
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("192.168.1.3"), 7777);
            ethernetJ11D.UseThisNetworkInterface();

            new Thread(() =>
            {
                try
                {
                    Thread.Sleep(3000);
                    PrintNetworkState();
                    Debug.Print("Connecting socket");
                    socket.Connect(endpoint);
                    Debug.Print("Socket connected");
                    byte[] msg = new byte[] { 0, 1, 2, 3, 4 };
                    socket.Send(msg);
                }
                catch (SocketException e) { Debug.Print("SocketException: " + e.ErrorCode); }
                catch (Exception e) { Debug.Print(e.GetType().ToString() + ": " + e.Message); }
                finally { PrintNetworkState(); }

            }).Start();
        }

        void PrintNetworkState()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("Up=");
            builder.Append(ethernetJ11D.IsNetworkUp);
            builder.Append("; ");
            builder.Append("Connected=");
            builder.Append(ethernetJ11D.IsNetworkConnected);
            builder.Append("; ");
            builder.Append("IP=");
            builder.Append(ethernetJ11D.NetworkInterface.IPAddress);
            builder.Append("; ");
            builder.Append("Mask=");
            builder.Append(ethernetJ11D.NetworkInterface.SubnetMask);
            builder.Append("; ");
            builder.Append("GW=");
            builder.Append(ethernetJ11D.NetworkInterface.GatewayAddress);
            Debug.Print(builder.ToString());
        }
          
        
    }
}


#2

See here
https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
for scket error codes.
They are the same for Winodws and NETMF


#3

ALso: Iguess you are writing a Gadgeteer application? I’m not verry familiar with them.
I guess void ProgramStarted() is called onece at Startup.
The Problem is that you create your socket as a local variable.
Also your thread which has a reference to the socket Exits immediatelly after the send.

After that, no one has an reference to your socket anymore, so the garbage collector Releases it, and therefor Closes the Connection.
you should store your socket as a static field in the Program class.


#4

I’ve done what you suggest in another version of the client. I see in wireshark that packets for establish the connection are sended but retrasmitted.