多端口监听不工作

本文关键字:工作 监听 | 更新日期: 2023-09-27 18:11:44

我有一个关于多个端口侦听的问题。我在CSharp上写了这段代码。我可以用这个代码发送两个端口的消息。这一次成功了。如何更改此代码以使其多次工作?代码块在这里:

服务器端:

class ListenPorts
{
    Socket[] scon;
    IPEndPoint[] ipPoints;
   internal ListenPorts(IPEndPoint[] ipPoints)
    {
        this.ipPoints = ipPoints;
        scon = new Socket[ipPoints.Length];
    }
    public void beginListen()
    {
        for (int i = 0; i < ipPoints.Length; i++)
        {
            scon[i] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            scon[i].Bind(ipPoints[i]);
            Thread thread = new Thread(threadListen);
            thread.Start(scon[i]);
        }
    }
    public void threadListen(object objs)
    {
        Socket scon = objs as Socket;
        byte[] data = new byte[1024];
        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
        EndPoint Remote = (EndPoint)(sender);
        try
        {
            scon.Listen(100);
            Socket newSocket = scon.Accept();
            newSocket.ReceiveFrom(data, ref Remote);
           // scon.ReceiveFrom(data, ref Remote);
        }
        catch (SocketException ex)
        {
            Console.WriteLine(ex.Message);
        }
       Console.WriteLine(scon.LocalEndPoint.ToString() + "IP {0}: ", Remote.ToString());
    }
}

main方法用于调用类来监听端口。

class Program
{
    static void Main(string[] args)
    {
        IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
        IPEndPoint ipPoint1 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8081);
        IPEndPoint[] ipPoints = new IPEndPoint[2] { ipPoint, ipPoint1 };
        ListenPorts lp = new ListenPorts(ipPoints);
        Console.WriteLine("Begin Listen");
        lp.beginListen();
    }

}

客户端:

class Program
{
    static void Main(string[] args)
    {
        byte[] data = new byte[1024];
        //TCP Client
        Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName());
        //Set the IP address of the server, and its port.
        IPEndPoint ipep1 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
        IPEndPoint ipep2 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8081);
        string welcome = "Hello! ";
        try
        {
            Socket server1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
           data = Encoding.ASCII.GetBytes(welcome);
           server1.Connect(ipep1);
           server1.Send(Encoding.ASCII.GetBytes(welcome));
           server1.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Socket server2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        data = Encoding.ASCII.GetBytes(welcome);
        server2.Connect(ipep2);
        server2.Send(Encoding.ASCII.GetBytes(welcome));
        server2.Close();
    }

}

多端口监听不工作

您需要在侦听器代码中使用循环。

try
{
    while (true)
  {
      scon.Listen(100);
      Socket newSocket = scon.Accept();
      var size= newSocket.ReceiveFrom(data, ref Remote);
      Console.WriteLine($"Received {size} bytes on socket {scon.LocalEndPoint}");
  }
}
catch (SocketException ex)
{
    Console.WriteLine(ex.Message);
}
Console.WriteLine(scon.LocalEndPoint.ToString() + "IP {0}: ", Remote.ToString());