I need to use udp and tcp connections in my application, TcpClient/TcpListenerit will rarely be active, but using udp will be the main one.
This is the server code:
static void Main(string[] args)
{
TcpListener listener = new TcpListener(IPAddress.Any, 25655);
listener.Start();
Socket sck = listener.AcceptTcpClient().Client;
UdpClient udpServer = new UdpClient(1100);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
var data = udpServer.Receive(ref remoteEP);
string result = Encoding.UTF8.GetString(data);
Console.WriteLine(result);
Console.Read();
}
And this is the Client:
static void Main(string[] args)
{
TcpClient client = new TcpClient("127.0.0.1", 25655);
Socket sck = client.Client;
UdpClient udpclient = new UdpClient();
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1100);
udpclient.Connect(ep);
byte[] data = UTF8Encoding.UTF8.GetBytes("Hello");
udpclient.Send(data,data.Length);
}
First, I establish a Tcp connection, then I try to connect and send data from the client to the server. From the breakpoint I add, I see that the Tcp part is working correctly, the client ends the program, but on the server it hangs in the receiving part, var data = udpServer.Receive(ref remoteEP);
for example, no data was received. When I delete part of the tcp code (the first 2 lines from the server and client), it works fine, it displays a message about the results.
Does anyone know why they were unable to receive data from the client?
Thanks in advance.