例如,QQ默认指定4000端口进行发送数据流,如何指定这个发送端口?
如果无法指定,请问如何获取当前所使用的发送的端口?
我使用的是UdpClient

解决方案 »

  1.   

    http://www-900.ibm.com/developerWorks/cn/java/l-jchat/index.shtml
    http://www.liva.com.cn/web/articles/2004/11/27/1101543149187.shtml
      

  2.   

    指定端口号
    比如:
    自己发送端口:1111 接收端口:2222
    对方发送端口:2222 接收端口:1111
    这样便可以实现通信了
    例子
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;namespace Pinbor.ChatApp
    {
    class Chat
    {
    private static IPAddress remoteIPAddress;
    private static int remotePort;
    private static int localPort; [STAThread]
    static void Main(string[] args)
    {
    try
    {
    //Get neccessary data for connection
    Console.WriteLine("Enter Local Port");
    localPort = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Enter Remote Port");
    remotePort = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Enter Remote IP address");
    remoteIPAddress = IPAddress.Parse(Console.ReadLine()); Thread tRec = new Thread(new ThreadStart(Receiver));
    tRec.Start(); while(true)
    {
    Send(Console.ReadLine());
    }
    }
    catch(Exception e)
    {
    Console.WriteLine(e.ToString());
    }
    } private static void Send(string datagram)
    {
    //Create UdpClint
    UdpClient sender = new UdpClient(); //Create IPEndPoint with details of remote host
    IPEndPoint endPoint = new IPEndPoint(remoteIPAddress,remotePort); try
    {
    //Convert data byte array
    byte[] bytes = Encoding.ASCII.GetBytes(datagram); //Send data
    sender.Send(bytes,bytes.Length,endPoint);
    }
    catch(Exception e)
    {
    Console.WriteLine(e.ToString());
    }
    finally
    {
    //Close connection
    sender.Close();
    }
    } public static void Receiver()
    {
    //Create a UdpClient for reading incoming data.
    UdpClient receivingUdpClient = new UdpClient(localPort); //IPEndPoint with remote host information
    IPEndPoint RemoteIpEndPoint = null; try
    {
    Console.WriteLine("----------******** Ready for chat ********----------"); while(true)
    {
    //Wait for datagram
    byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint); //Convert and display data
    string returnData = Encoding.ASCII.GetString(receiveBytes);
    Console.WriteLine("-" + returnData.ToString());
    }
    }
    catch(Exception e)
    {
    Console.WriteLine(e.ToString());
    }
    }
    }
    }
      

  3.   

    http://www-900.ibm.com/developerWorks/cn/java/l-jchat/index.shtml
    http://www.liva.com.cn/web/articles/2004/11/27/1101543149187.shtml
      

  4.   

    下面的示例使用主机名 www.contoso.com 在端口 11000 上建立 UdpClient 连接
    UdpClient udpClient = new UdpClient();
        try{
             udpClient.Connect("www.contoso.com", 11000);
             Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
          
             udpClient.Send(sendBytes, sendBytes.Length);
             UdpClient udpClientB = new UdpClient();
             udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000);         IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);         Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); 
             string returnData = Encoding.ASCII.GetString(receiveBytes);         Console.WriteLine("This is the message you received " +
                                          returnData.ToString());
             Console.WriteLine("This message was sent from " +
                                         RemoteIpEndPoint.Address.ToString() +
                                         " on their port number " +
                                         RemoteIpEndPoint.Port.ToString());          udpClient.Close();
              udpClientB.Close();
              
              }  
           catch (Exception e ) {
                      Console.WriteLine(e.ToString());
            }
      

  5.   

    Pinbor(来客心动) 自己发送端口:1111 接收端口:2222
    对方发送端口:2222 接收端口:1111
    你的代码如何能够指定发送端口
      

  6.   

    clientprivate UdpClient server;
    IPEndPoint ipep = new IPEndPoint(IPAddress.Any,  9000);
    server = new UdpClient (ipep);
    server.Send(data, intLength, "127.0.0.1", 9050);serverprivate UdpClient server;private IPEndPoint ipep = null;
    private IPEndPoint sender = null;ipep = new IPEndPoint(IPAddress.Any, 9050);
    sender = new IPEndPoint(IPAddress.Any, 9050);server = new UdpClient(ipep);data = server.Receive(ref sender);
    也就是说,两端都设置为server,然后在Send的时候指定目的IP和Port就可以了