Question

I am trying to write a simple UDP program in C# which sends and receives data on localhost. I am a beginner in C# but much better at MATLAB so instead of writing the server and the client in C#, I decided to send data using C# and receive it in MATLAB.

I tried two approaches to send data. Using Socket class worked, but using UdpClient class failed.

Before running this code, I run the MATLAB code to set the callback function to print the received datagram.

Only one region is active in each run. I comment out the other one.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace udp1
{
    class Program
    {
        const int port = 62745; //Chosen at random
        static void Main(string[] args)
        {
            string str = "Hello World!";
            byte[] sendBytes = Encoding.ASCII.GetBytes(str);

            #region 1 Send data using socket class
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
            sock.SendTo(sendBuff, ipEndPoint);
            Console.ReadLine();
            #endregion

            #region 2 Send data using UdpClient class
            UdpClient sendingClient = new UdpClient(port);
            sendingClient.Send(sendBytes, sendBytes.Length);
            #endregion
        }
    }
}

I am getting

Only one usage of each socket address (protocol/network address/port) is normally permitted

error when I run the code in region 2.

However, when I run the code in region 1 everything works as I expect and I receive the data in MATLAB without any problems.


Here is my MATLAB code. I have used this code in other applications, so I highly doubt there is anything wrong with it.

fclose(instrfindall); %Close all udp objects
%UDP Configuration
udpConfig.ipAddress = '127.0.0.1';
udpConfig.portAddress = 62745;

udpObj = udp(udpConfig.ipAddress, udpConfig.portAddress, ...
    'LocalPort',        udpConfig.portAddress, ...
    'ByteOrder',        'bigEndian');

set(udpObj, 'datagramTerminateMode', 'on');
set(udpObj, 'datagramReceivedFcn', {@cbDataReceived, udpObj});

fopen(udpObj);

And the callback function:

function cbDataReceived(hObj, eventdata, udpObj)
    bytesAvailable = get(udpObj, 'BytesAvailable');
    receivedDatagram = fread(udpObj, bytesAvailable);
    disp(char(receivedDatagram));
end

So, why am I getting the error in UdpClient case and not getting it in Socket case? Is there a way to avoid that error?

Was it helpful?

Solution

I understand that you are using the same port for both MATLAB and C# on the same computer. Thus, operating system does not allow to open the same port from different applications.

UDP allows sending and receiving datagrams from different ports, so use different ports for different applications if both applications are running on the same computer.

UdpClient sendingClient = new UdpClient(62746); // Some different port to listen
sendingClient.Send(sendBytes, sendBytes.Length, ipEndPoint);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top