FreeRTOS Windows Demo - Socket Communication Problem with Local Application

Hi all,

Is it possible to communicate FreeRTOS Windows Demo with any windows app on local PC?

I use FreeRTOS Socket Demo in the link below. I am trying to communicate this app by using a Socket connection with the other C# Console App in same PC.

It is my method for socket listening code "vCreateTCPServerSocket:"

void vCreateTCPServerSocket()
{
struct freertos_sockaddr xClient, xBindAddress;
Socket_t xListeningSocket, xConnectedSocket;

static const TickType_t xReceiveTimeOut = portMAX_DELAY;
const BaseType_t xBacklog = 20;

xListeningSocket = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP);

configASSERT(xListeningSocket != FREERTOS_INVALID_SOCKET);

FreeRTOS_setsockopt(xListeningSocket,0,FREERTOS_SO_RCVTIMEO,&xReceiveTimeOut,sizeof(xReceiveTimeOut));
//xBindAddress.sin_addr = FreeRTOS_inet_addr("192.168.1.15");
//xBindAddress.sin_addr = FreeRTOS_inet_addr_quick(192,168,1,15);
xBindAddress.sin_port = FreeRTOS_htons(3002);

socklen_t xSize = sizeof(xClient);
BaseType_t returnV    al_bind = FreeRTOS_bind(xListeningSocket, &xBindAddress, sizeof(xBindAddress));
    //binding successfully
BaseType_t returnVal_listen = FreeRTOS_listen(xListeningSocket, xBacklog);
    //listening successfully
for (;; )
  {

    xConnectedSocket = FreeRTOS_accept(xListeningSocket, &xClient, &xSize);
    //cannot connect succesfully
        configASSERT(xConnectedSocket != FREERTOS_INVALID_SOCKET);
     
        if (xConnectedSocket != FREERTOS_INVALID_SOCKET)
        {
        //Sending part.
        } 
   }
  }
 }

But there is a problem when FreeRTOS accepting and taking the xConnected socket.

Here is my C# Client, Console_Client():

public static void Console_Client()
{
    try
    {
        IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
        IPAddress ipAddr = IPAddress.Parse("192.168.1.15");
        IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 3002);
        Socket sender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);


        while (true)
        {
            try
            {
                Console.WriteLine("Bağlanmaya çalışıyor..");
                sender.Connect(localEndPoint);

                Console.WriteLine("Socket connected to -> {0} ",
                sender.RemoteEndPoint.ToString());
                byte[] messageSent = Encoding.ASCII.GetBytes("Test Client<EOF>");
                int byteSent = sender.Send(messageSent);
          
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();

            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}

Here is my C# Server, Console_Server():

    public static void Console_Server()
    {
        IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
        IPAddress ipAddr = IPAddress.Parse("192.168.1.15");
        IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 3002);
        Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(10);
            Console.WriteLine("Waiting connection ... ");
            Socket clientSocket = listener.Accept();
            while (true)
            {
                byte[] bytes = new Byte[1024];
                string data = null;

                while (true)
                {
                    int numByte = clientSocket.Receive(bytes);
                    data += Encoding.ASCII.GetString(bytes,0, numByte);
                    if (data.IndexOf("<EOF>") > -1)
                        break;
                }
                Console.WriteLine("Text received -> {0} ", data);
                byte[] message = Encoding.ASCII.GetBytes("Test Server");

                clientSocket.Send(message);
                //clientSocket.Shutdown(SocketShutdown.Both);
                //clientSocket.Close();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

Despite this Console_Client communicating with my Console_Server normally it is not communicate with the FreeRTOS side. Is there any hardware-dependent issue that I don’t know or Is it possible to communicate FreeRTOS Socket with Console Socket in the Windows PC.

I’m pretty certain you can communicate between a FreeRTOS+TCP socket and a WinSock socket as we used to do that a lot when testing the stack. However, even though your connection is to the same computer, you will still need to set the “network interface to use” compile time constant to a valid Ethernet port, and have the Ethernet cable plugged in (virtual Ethernet ports provided you can force the port to recognise that a cable is plugged in).

1 Like

Yes indeed, you should be able to communicate with the WinSim application from another application that uses WinSock sockets.

Before you try out an external application, I would try to ping it from the same laptop. Lookup the actual IP-address received from DHCP in the logging, and try e.g.: ping 192.168.x.x

If that doesn’t work, could you try to ping it from another device in the same network (LAN)?

Once pinging works, I expect that other communication will also work.

On my own ( Windows 10 ) laptop, I can ping it directly, and I can also use WinSock to communicate with it. Other users say that they’re only able to contact their WinSim device from a different laptop or device.

And like Richard said, please check the value of configNETWORK_INTERFACE_TO_USEin your FreeRTOSConfig.h. Normally it should point to the LAN adapter that connects you to the Internet.

1 Like

I tried to set my compile time constant to a valid Ethernet port, and I have had the ethernet cable plugged in. But it didn’t work. After I read the Selecting the Network Interface post again, I realize that I should think a little bit different from the normal Socket programming paradigm while I using FreeRTOS. When I try to set my simulator’s MAC address and the IP address different from my Ethernet adapter it did work successfully. So, the root reason of failing socket communication in FreeRTOS demo is to not see FreeRTOS Simulator as a virtual local adapter, but it is.

Thanks and regards.

I made the same mistake before, I thought that the laptop’s IP- and MAC-address could be used by the WinSim application as well.

Thanks for reporting this back.

1 Like

Hi again,

I also tried these steps in my desktop computer with normal network switch and result is fail. My firewall is down. And other prerequisites in [ Using the FreeRTOS Windows Port ] was ensured correctly.

  • All steps succesfully gained to " Selecting the Network Interface".

  • FreeRTOS_IPInit is successful.

  • Selecting the Network Interface console screen is displaying my manually adjusted values successfully.

  • I am also able to listen my virtual ip’s data communication in wireshark that originated and fired by below operation flow :
    { vApplicationIPNetworkEventHook - > vStartSimpleUDPClientServerTasks initializing and data commution is successful in background. }

void vStartSimpleUDPClientServerTasks( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )
{
	/* Create the client and server tasks that do not use the zero copy
	interface. */
	xTaskCreate( prvSimpleClientTask, "SimpCpyClnt", usStackSize, ( void * ) ulPort, uxPriority, NULL );
	xTaskCreate( prvSimpleServerTask, "SimpCpySrv", usStackSize, ( void * ) ulPort, uxPriority + 1, NULL );
	
	xTaskCreate( prvSimpleZeroCopyUDPClientTask, "SimpZCpyClnt", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority, NULL );
	xTaskCreate( prvSimpleZeroCopyServerTask, "SimpZCpySrv", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority + 1, NULL );
}

Despite all these things, I am not even able to ping my WinSim. I could’t give meaning to this situation.

Dear @htibosch, did you meet this status before ?

Thanks and Regards.