Click here to Skip to main content
15,881,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When I run the program it says:

"System.Net.Sockets.SocketException: "Normally, each socket address (protocol, network address, or port) may only be used once"

It shows an error here:

socketCmdText = new UdpClient(cmdTextPort);

-The socketCmdText is also has a value of null

This is my code:

C#
UdpClient socketCmd;
        UdpClient socketCmdText;
        private const int cmdTextPort = 6666;
        private const int cmdPort = 6676;
        private double tempSensTop = 0;
        private double tempSensBottom = 0;
        private double tempProcessor = 0;
        private String firmware = "";
        private String streamType = "";
        private String camIP = "";

 public override void initializeJob()
        {
           
                if (TempDevice == temperatureGauge.udp)
                {
                    camIP = parameters[0];
                    socketCmdText = new UdpClient(cmdTextPort);
                    socketCmdText.BeginReceive(new AsyncCallback(OnUdpDataCmdText), socketCmdText);
                    socketCmd = new UdpClient(cmdPort);
                socketCmdText.BeginReceive(new AsyncCallback(OnUdpDataCmd), socketCmd);
                }
}



This is where Im sending:


C#
private void requestData()
        {
                           IPAddress _camip = IPAddress.Parse(camIP);
                IPEndPoint _ep = new IPEndPoint(_camip, cmdTextPort);
                byte[] _sendbuf = Encoding.ASCII.GetBytes("Cmd_ISL_GetCurrentSensorTemperature");
                socketCmdText.Send(_sendbuf, _sendbuf.Length, _ep);
          
            byte[] _sendbuf2 = Encoding.ASCII.GetBytes("Get_Temp");
                socketCmdText.Send(_sendbuf2, _sendbuf2.Length, _ep);
           
}


void OnUdpDataCmdText(IAsyncResult result)
        {
            try
            {
                // this is what had been passed into BeginReceive as the second parameter:
                UdpClient socket = result.AsyncState as UdpClient;
                // points towards whoever had sent the message:
                IPEndPoint source = new IPEndPoint(IPAddress.Any, cmdTextPort);
                // get the actual message and fill out the source:
                byte[] message = socket.EndReceive(result, ref source);
                // do what you'd like with `message` here:

// schedule the next receive operation once reading is done:
                socket.BeginReceive(new AsyncCallback(OnUdpDataCmdText), socket);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error TemperatureMesurement-Job OnUdpDataCmdTxt " + jobName + " : " + e.Message);
            }
        }


C#
void OnUdpDataCmd(IAsyncResult result)
        {
            try
            {
                // this is what had been passed into BeginReceive as the second parameter:
                UdpClient socket = result.AsyncState as UdpClient;
                // points towards whoever had sent the message:
                IPEndPoint source = new IPEndPoint(IPAddress.Any, cmdPort);
                // get the actual message and fill out the source:
                byte[] message = socket.EndReceive(result, ref source);
                // do what you'd like with `message` here:
                string[] receiveString = BitConverter.ToString(message).Split('-');

                if (camIP == source.Address.ToString())
                {
                    if (string.Concat(receiveString).Substring(0, 16) == "0201001001000002")
                    {
                        streamType = receiveString[8] + receiveString[9];
                    }
                }
                // schedule the next receive operation once reading is done:
                socket.BeginReceive(new AsyncCallback(OnUdpDataCmd), socket);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error TemperatureMesurement-Job OnUdpDataCmd " + jobName + " : " + e.Message);
            }
        }


What I have tried:

Any help will be appreciated. Trying since a long time to get rid of the problem, i tried using another port, closing it in task manager but it doesnt seem to work. But it doesnt
Posted
Updated 24-Sep-19 8:46am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900