如何在超时时结束循环,TCPServer套接字C#

时间:2018-06-26 06:11:00

标签: c# sockets tcp tcpserver

我正在创建一个TCP服务器/客户端。对于这个项目,我只需要运行此功能60秒钟,然后它将终止。有人可以指导我修复此代码吗?

public static void SendTCPServer(string content)
    {
        Stopwatch timer = new Stopwatch();
        timer.Start();

        byte[] data = new byte[1024];
        IPEndPoint ipep = new IPEndPoint(IPAddress.Any,
                               8080);

        Socket newsock = new
            Socket(AddressFamily.InterNetwork,
                        SocketType.Stream, ProtocolType.Tcp);

        newsock.Bind(ipep);
        newsock.Listen(10);
        while (timer.Elapsed.TotalSeconds < 60)
        {
            Console.WriteLine("Waiting for a client...");
        Socket client = newsock.Accept();
        IPEndPoint clientep =
                     (IPEndPoint)client.RemoteEndPoint;


            Console.WriteLine("Connected with {0} at port {1}",
                            clientep.Address, clientep.Port);


            string welcome = content;
            data = Encoding.ASCII.GetBytes(welcome);
            client.Send(data, data.Length,
                              SocketFlags.None);

            Console.WriteLine("Disconnected from {0}",
                              clientep.Address);
            client.Close();
            newsock.Close();
            return;
        }
        timer.Stop();
        //client.Close();
        newsock.Close();
        return;
    }

1 个答案:

答案 0 :(得分:0)

您可以为此使用线程。

 public class Parameters
 {
    public Socket _socket;
    public string content;
 }

//calling part
  Thread listenerThread = new Thread(new ParameterizedThreadStart(SendTCPServer));
        Socket newsock = new
           Socket(AddressFamily.InterNetwork,
                       SocketType.Stream, ProtocolType.Tcp);
        listenerThread.Start(new Parameters { _socket = newsock, content = "Welcome" 
 });

        Thread.Sleep(60000);
            newsock.Dispose();
//calling part end
 public static void SendTCPServer(object obj)
    {
        try
        {
            Stopwatch timer = new Stopwatch();
            timer.Start();

            byte[] data = new byte[1024];
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any,
                                   8080);

            Parameters param = obj as Parameters;


            Socket newsock = param._socket;
            newsock.Bind(ipep);
            newsock.Listen(10);
            while (timer.Elapsed.TotalSeconds < 60)
            {
                Console.WriteLine("Waiting for a client...");
                Socket client = newsock.Accept();
                IPEndPoint clientep =
                             (IPEndPoint)client.RemoteEndPoint;


                Console.WriteLine("Connected with {0} at port {1}",
                                clientep.Address, clientep.Port);


                string welcome = param.content;
                data = Encoding.ASCII.GetBytes(welcome);
                client.Send(data, data.Length,
                                  SocketFlags.None);

                Console.WriteLine("Disconnected from {0}",
                                  clientep.Address);
                client.Close();
                newsock.Close();
                return;
            }
            timer.Stop();
            //client.Close();
            newsock.Close();
            return;
        }
        catch (Exception ex)
        {


        }

    }