多端口监听不起作用

时间:2016-10-06 11:03:23

标签: c# serial-port

我有一个关于多端口监听的问题。我在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();

    }

}

1 个答案:

答案 0 :(得分:0)

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

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());