Sending and receiving UDP packets between two programs on the same computer

后端 未结 8 1767
慢半拍i
慢半拍i 2020-12-02 12:37

Is it possible to get two separate programs to communicate on the same computer (one-way only) over UDP through localhost/127... by sharing the same port #?

We\'re

8条回答
  •  渐次进展
    2020-12-02 13:10

    Here is the full code from the answers by Tarnay Kálmán and sipwiz:

    The server code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace UdpBroadcastTest
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Console.WriteLine("Sender");
                // This constructor arbitrarily assigns the local port number.
    
                UdpClient udpClient = new UdpClient();
                udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                udpClient.Connect("localhost", 11000);
                try
                {
                    string message = String.Empty;
                    do
                    {
                        message = Console.ReadLine();
                        // Sends a message to the host to which you have connected.
                        Byte[] sendBytes = Encoding.ASCII.GetBytes(message);
    
                        udpClient.Send(sendBytes, sendBytes.Length);
                    } while (message != String.Empty);
    
                    udpClient.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
    
                Console.WriteLine("Press Any Key to Continue");
                Console.ReadKey();
            }
        }
    }
    

    The client code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace UdpReciever
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Receiver");
                // This constructor arbitrarily assigns the local port number.
                UdpClient udpClient = new UdpClient();
                udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, 11000));
                try
                {
                    //IPEndPoint object will allow us to read datagrams sent from any source.
                    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
    
                    string message = String.Empty;
                    do
                    {
    
                        // Blocks until a message returns on this socket from a remote host.
                        Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
                        message = Encoding.ASCII.GetString(receiveBytes);
    
                        // Uses the IPEndPoint object to determine which of these two hosts responded.
                        Console.WriteLine("This is the message you received: " +
                                                     message);
                        //Console.WriteLine("This message was sent from " +
                        //                            RemoteIpEndPoint.Address.ToString() +
                        //                            " on their port number " +
                        //                            RemoteIpEndPoint.Port.ToString());
                    }
                    while (message != "exit");
                    udpClient.Close();
                    //udpClientB.Close();
    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
    
                Console.WriteLine("Press Any Key to Continue");
                Console.ReadKey();
            }
        }
    }
    

提交回复
热议问题