Reputation: 1
I´m trying to make a UDP server in c# ussing Microsoft Visual Studio. Few days ago, I connected properly the client and the server, and I have been able to read and process the received data. However, I modify to add a new socket couple to the UDP server. And from this moment until now never works again. Always I receive the same exception (10060).
UDP SERVER
public class Global
{
//////// UDP Server Slave 1
public const int port = 5060;
public static UdpClient serverSocket = null;
public static IPEndPoint clientEndpoint = null;
public static byte[] receivedData = null;
public static bool UDPListening = false;
public static string[] values;
//////// UDP Server Slave 2
public const int port2 = 5061;
public static UdpClient serverSocket2 = null;
public static IPEndPoint clientEndpoint2 = null;
public static byte[] receivedData2 = null;
public static bool UDPListening2 = false;
public static string[] values2;
//////// UDP Server Slave 2
public const int port3 = 5062;
public static UdpClient serverSocket3 = null;
public static IPEndPoint clientEndpoint3 = null;
public static byte[] receivedData3 = null;
public static bool UDPListening3 = false;
public static string[] values3;
}
private void HMI_Load(object sender, System.EventArgs e)
{
// Function executed (timer2_Tick) every timer2.Interval (comms)
System.Windows.Forms.Timer timer2 = new System.Windows.Forms.Timer();
timer2.Interval = 10; //ms
timer2.Start();
timer2.Tick += timer2_Tick;
}
private void timer2_Tick(object sender, EventArgs e)
{
if (Global.UDPListening == true)
{
//Receive UDP Slave1
Global.serverSocket = new UdpClient(Global.port);//Initializes a new instance of the UdpClient class.
Global.clientEndpoint = new IPEndPoint(IPAddress.Parse("172.20.80.35"), 5060);//Initializes a new instance of the IPEndPoint class with the specified address and port number.
try
{
Global.serverSocket.Connect(Global.clientEndpoint);//The Connect method sets a default remote host with the value specified in the endPoint parameter.
//Once established, there is no need to specify a remote host on each call to the Send method.
//Handle Data
Global.serverSocket.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
Global.receivedData = Global.serverSocket.Receive(ref Global.clientEndpoint);//Returns a UDP datagram sent by a remote host
string receivedMessage = Encoding.ASCII.GetString(Global.receivedData);//We convert in one string the data of datagram
char delimiter = '/';//we declare the variable in which we will look to separate the data from the string
Global.values = receivedMessage.Split(delimiter);//with Split function we can separate the string in substrings. We save it in a string structure
}
catch (SocketException)
{
}
//Receive UDP Slave2
Global.serverSocket2 = new UdpClient(Global.port2);//Initializes a new instance of the UdpClient class.
Global.clientEndpoint2 = new IPEndPoint(IPAddress.Any, 5061);//Initializes a new instance of the IPEndPoint class with the specified address and port number.
try
{
Global.serverSocket2.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
Global.receivedData2 = Global.serverSocket2.Receive(ref Global.clientEndpoint2);//Returns a UDP datagram sent by a remote host
Global.serverSocket2.Connect(Global.clientEndpoint2);//The Connect method sets a default remote host with the value specified in the endPoint parameter.
//Once established, there is no need to specify a remote host on each call to the Send method.
//Handle Data
string receivedMessage2 = Encoding.ASCII.GetString(Global.receivedData2);//We convert in one string the data of datagram
char delimiter2 = '/';//we declare the variable in which we will look to separate the data from the string
Global.values2 = receivedMessage2.Split(delimiter2);//with Split function we can separate the string in substrings. We save it in a string structure
}
catch (SocketException)
{
}
//Receive UDP Slave3
Global.serverSocket3 = new UdpClient(Global.port3);//Initializes a new instance of the UdpClient class.
Global.clientEndpoint3 = new IPEndPoint(IPAddress.Any, 5062);//Initializes a new instance of the IPEndPoint class with the specified address and port number.
try
{
Global.serverSocket2.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
Global.receivedData3 = Global.serverSocket3.Receive(ref Global.clientEndpoint3);//Returns a UDP datagram sent by a remote host
Global.serverSocket3.Connect(Global.clientEndpoint3);//The Connect method sets a default remote host with the value specified in the endPoint parameter.
//Once established, there is no need to specify a remote host on each call to the Send method.
//Handle Data
string receivedMessage3 = Encoding.ASCII.GetString(Global.receivedData3);//We convert in one string the data of datagram
char delimiter3 = '/';//we declare the variable in which we will look to separate the data from the string
Global.values3 = receivedMessage3.Split(delimiter3);//with Split function we can separate the string in substrings. We save it in a string structure
}
catch (SocketException)
{
}
if (Global.values == null)
{
richTextBox1.Text += "No Client Connected\n\r";
//Global.UDPListening = false;
}
UDP Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography; namespace UdpClientProgram
{
class Program
{
/*
FOR TESTING THE SERVER
*/ //public const string password = "HyPNotiC"; static void Main(string[] args)
{
string matrizDatos = "HYP01/1.1/2.1/3.1/4.1/5.1/6.1/7.1/8.1/9.1/10.1/11.1/12.1/13.1/14.1/15.1/16.1/1/1";
//string matrizDatos = "HYP01/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/1/1";
string matrizDatos2 = "HYP02/16.1/15.1/14.1/13.1/12.1/11.1/10.1/9.1/8.1/7.1/6.1/5.1/4.1/3.1/2.1/1.1/1/1";
string[] matriz = new string[999]; while (true)
{ Console.WriteLine(matrizDatos);
byte[] packet;
UdpClient client = new UdpClient();
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("172.20.80.34"), 5060);
client.Connect(ipep);
//packet = Encoding.ASCII.GetBytes(cadena1);
packet = Encoding.ASCII.GetBytes(matrizDatos);
client.Send(packet, packet.Length); Console.WriteLine(matrizDatos2);
byte[] packet2;
UdpClient client2 = new UdpClient();
IPEndPoint ipep2 = new IPEndPoint(IPAddress.Parse("172.20.80.34"), 5061);
client2.Connect(ipep2);
//packet = Encoding.ASCII.GetBytes(cadena1);
packet2 = Encoding.ASCII.GetBytes(matrizDatos2);
client2.Send(packet2, packet2.Length); Thread.Sleep(1000);
}
}
} }
I know that I have some lines without use, and also I know that I´m not ussing the socket of the 3 Slave.
Always I have the same excepttion. I´m not connecting to the remote host.
With Wireshark I have checked that I´m receiving some packets with the proper message. The client is in one Pc and the server in other.
Ty in advance.
I tryed to receive mesages from a client. I have the clients in one Pc and the server in other. Always I´m receiving the same exception (10060). I can´t connect weith the clients.
Upvotes: 0
Views: 226