Please give an example of UDP for Windows.

Status
Not open for further replies.

Lukashuk

Well-known member
Hello!
I began to study IP.
I downloaded the UDPSendReceiveString example and it works well on someone else's program.
My problem is that I need to write my own program for Windows. I am using Visual Studio C #, C ++.
My request is that I need a simple example of interaction on UDP for Windows.
I thank you in advance.
 
I wrote the UDP client.
Code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace UdpSample
{
    class Chat
    {
        private static IPAddress remoteIPAddress;
        private static int remotePort;
        private static int localPort;

        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                // We get the data necessary for the connection
                Console.WriteLine("Specify a local port");
                localPort = Convert.ToInt16(Console.ReadLine());

                Console.WriteLine("Specify Remote Port");
                remotePort = Convert.ToInt16(Console.ReadLine());

                Console.WriteLine("Specify Remote IP Address");
                remoteIPAddress = IPAddress.Parse(Console.ReadLine());

                // We create a stream for listening
                Thread tRec = new Thread(new ThreadStart(Receiver));
                tRec.Start();

                while (true)
                {
                    Send(Console.ReadLine());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An exception has occurred: " + ex.ToString() + "\n  " + ex.Message);
            }
        }

        private static void Send(string datagram)
        {
            // Create UdpClient
            UdpClient sender = new UdpClient();

            // Создаем endPoint по информации об удаленном хосте
            //We create endPoint on the information on a remote host
            IPEndPoint endPoint = new IPEndPoint(remoteIPAddress, remotePort);

            try
            {
                // Convert data to byte array
                byte[] bytes = Encoding.UTF8.GetBytes(datagram);

                // Send data
                sender.Send(bytes, bytes.Length, endPoint);
            }
            catch (Exception ex)
            {
                Console.WriteLine("An exception has occurred: " + ex.ToString() + "\n  " + ex.Message);
            }
            finally
            {
                // Закрыть соединение
                sender.Close();
            }
        }

        public static void Receiver()
        {
            // Create a UdpClient to read incoming data
            UdpClient receivingUdpClient = new UdpClient(localPort);

            IPEndPoint RemoteIpEndPoint = null;

            try
            {
                Console.WriteLine(
                   "\n-----------*******General chat*******-----------");

                while (true)
                {
                    // Waiting for a datagram
                    byte[] receiveBytes = receivingUdpClient.Receive(
                       ref RemoteIpEndPoint);

                    // Convert and display data
                    string returnData = Encoding.UTF8.GetString(receiveBytes);
                    Console.WriteLine(" --> " + returnData.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An exception has occurred: " + ex.ToString() + "\n  " + ex.Message);
            }
        }
    }
}

it works partially.
I do not get the answer "acknowledged".
what localPort should i enter?
many numbers cause an error, some ignore as if it is empty and creates its own
Teensy defines different things every time.
it seems the problem is here or somewhere else
Code:
 // We get the data necessary for the connection
                Console.WriteLine("Specify a local port");
                localPort = Convert.ToInt16(Console.ReadLine());
 
The issue was resolved.
The problem is with the firewall. At this stage I turned it off and my program worked.
 
Status
Not open for further replies.
Back
Top