TCP server socket.Listen() --> Invalid operation exception

Hello,
I fail getting a simple TCP server running on a SC-20100S-A.
After creating the socket and configuring it I can never pass serverSocket.Listen() method. I am out of ideas. I’d really appreciate some help. Thanks!

VS Output:
Program Started
Init Ethernet
The network address has changed.
IP: 192.168.9.1
MAC: 0x02:0x33:0x44:0x55:0x66:0x01
Network available: True
#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (4) ####
#### Message:
#### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::Listen [IP: 0000] ####
#### System.Net.Sockets.Socket::Listen [IP: 0017] ####
#### IPTE.Demon_MCU.Ethernet::StartTcpServer [IP: 003d] ####
#### IPTE.Demon_MCU.Ethernet+<>c::<NetworkController_NetworkLinkConnectedChanged>b__2_0 [IP: 0009] ####
Exception thrown: ‘System.InvalidOperationException’ in GHIElectronics.TinyCLR.Devices.Network.dll
An unhandled exception of type ‘System.InvalidOperationException’ occurred in GHIElectronics.TinyCLR.Devices.Network.dll

Code extract:

            // create a socket to listen for incoming connections
            Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Tcp);
            serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            serverSocket.Bind(new IPEndPoint(IPAddress.Any, 4097));
            serverSocket.Listen(int.MaxValue);

            var remoteSocket = serverSocket.Accept();
            remoteSocket.Send(new byte[2] { 0x0, 0x1 }) ;

start with SocketType.Stream for TCP.

1 Like

Welcome to the community @cazedo

Like Mike said, Dgram is UDP not TCP.

Hello Mike and Gus_Issa, thanks a lot for the quick help!!!
Yes, I had a UDP server running before, modified it and did not check the type… now it is running perfectly.
I am very happy.

2 Likes