如何知道用于发送消息的UDP端口

时间:2016-06-03 15:48:34

标签: c# udp

我有一个程序将一条消息发送到服务器,服务器识别该消息对其进行解释,并将消息发送回它在开始时收听的端口。所以假设客户端从端口5000发送。服务器将从端口5000读取消息,并将消息发送回客户端用于发送消息的端口,以便客户端可以接收它。我现在的问题是我不想为服务器修复端口(我不希望服务器总是发送回客户端到端口1000),所以我怎么知道我的客户端用来发送消息的端口每一次?所以在我的客户端,我的UdpClient Listener = new UdpClient(xxx)[(xxxx)是端口号]每次都可以用新值填充?它用于发送消息的端口的值

感谢

这是我的计划:

private const int sendPort = 9999;
    static void Main(string[] args)
    {
        Boolean done = false;
        IPAddress send_to_address = IPAddress.Parse("xx.xxx.xxx.xxx");
        IPEndPoint sending_end_point = new IPEndPoint(send_to_address, sendPort);
        IPEndPoint receiving_end_point = new IPEndPoint(IPAddress.Any, 0);
        UdpClient Listener = new UdpClient(WHAT TO PUT HERE???);
    while (!done)
    {
        Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        Console.WriteLine("Enter text to send");
        string text_to_send = Console.ReadLine();
        // the socket object must have an array of bytes to send.
        // this loads the string entered by the user into an array of bytes.
        byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);

        // Remind the user of where this is going.
        Console.WriteLine("sending to address: {0} port: {1}", sending_end_point.Address, sending_end_point.Port);
        sending_socket.SendTo(send_buffer, sending_end_point);
        Console.WriteLine("Message has been sent to the broadcast address");
        Console.WriteLine("Now we are waiting for a message back from the Listener");

        // here we receive the Message from the Server
        Byte[] ByteFromListener = Listener.Receive(ref receiving_end_point);
        string datafromreceiver = Encoding.ASCII.GetString(ByteFromListener);
Console.WriteLine(Datafrom receiver)



        }

    }
}

}

1 个答案:

答案 0 :(得分:0)

答案很简单。只需要使用相同的套接字来接收消息。

以下是代码:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

class Program
{
    private const int sendPort = 9999;
    static void Main(string[] args)
    {
        Boolean done = false;
        IPAddress send_to_address = IPAddress.Parse("10.128.105.177");
        IPEndPoint sending_end_point = new IPEndPoint(send_to_address, sendPort);
        EndPoint receiving_end_point = new IPEndPoint(IPAddress.Any, 0);
    while (!done)
    {
        Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        Console.WriteLine("Enter text to send");
        string text_to_send = Console.ReadLine();
        // the socket object must have an array of bytes to send.
        // this loads the string entered by the user into an array of bytes.
        byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);

        // Remind the user of where this is going.
        Console.WriteLine("sending to address: {0} port: {1}", sending_end_point.Address, sending_end_point.Port);
        sending_socket.SendTo(send_buffer, sending_end_point);
        Console.WriteLine("Message has been sent to the broadcast address");
        Console.WriteLine("Now we are waiting for a message back from the Listener");


        Byte[] ByteFromListener = new byte[8000];
        sending_socket.ReceiveFrom(ByteFromListener, ref receiving_end_point);

        string datafromreceiver;
        datafromreceiver = Encoding.ASCII.GetString(ByteFromListener).TrimEnd('\0');          

        //Here we print the message from the server
        Console.WriteLine(datafromreceiver.ToString());



        }

    }
}

}