多线程TCP服务器回显所有客户端

时间:2016-06-25 15:24:58

标签: c# multithreading sockets tcp server

我一直致力于TCP NAT Punch Through服务器/客户端聊天室设置。我已经成功地在UDP中进行了设置,现在我想为TCP做同样的事情,因为我的套接字atm有点粗糙。

客户端可以正常连接和聊天并接收回声的任何方式,但根据连接数量,它将回显给基于连接的客户端数量发送消息的同一客户端。例如,3个客户端连接client2发送ping它回送给客户端2,3次。它应该通过所有连接用户的IPEndPoint列表循环并执行sock.SendTo(data,data.Length,clients [i]);

但它回复发送邮件的客户端。不知道我做错了什么,但我以为我做得很好。但这困扰着我。对我所做错的一些指导将不胜感激。

private static void Main(string[] args)
    {
        //create a socket
        //create a end point to listen on
        //bind
        //listen
        //accept the socket on a new socket
        //receive
        //store the connection in a list
        //ping back on their socket
        //go back to listening state

        Console.Title = " TCP NAT PT Server";

        StartServer();
    }

    private static void StartServer()
    {
        Thread ListenThread = new Thread(new ThreadStart(Listen));
        ListenThread.Start();
        Console.WriteLine("ListenThread: " + ListenThread.ThreadState);
    }

    private static void Listen()
    {
        try
        {
            server.Bind(ep);
            server.Listen(0);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        while (true)
        {
            try
            {
                Socket handler = server.Accept();

                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientCommunication));
                clientThread.Start((object)handler);
                Console.WriteLine("ReceiveThread: " + clientThread.ThreadState);

                string rstr = handler.RemoteEndPoint.ToString();
                string[] rdata = rstr.Split(':');

                IPEndPoint rep = new IPEndPoint(IPAddress.Parse(rdata[0]), int.Parse(rdata[1]));
                clients.Add(rep);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }

    private static void HandleClientCommunication(object Client)
    {
        Socket sock = (Socket)Client;
        byte[] data = new byte[1024];
        string str = string.Empty;

        while (true)
        {
            try
            {
                int rec = sock.Receive(data, SocketFlags.None);
                Array.Resize(ref data, rec);
                str = Encoding.ASCII.GetString(data);
                Console.WriteLine(sock.RemoteEndPoint.ToString() + " >> " + str);

                //Console.WriteLine(clients.Count);
                //Console.WriteLine(clients[0].Address + ":" + clients[0].Port);

                string temp = Encoding.ASCII.GetString(data);
                temp = "Echo: " + temp;
                data = Encoding.ASCII.GetBytes(temp);
                Console.WriteLine("Data.Length: " + temp.Length);

                for (int i = 0; i < clients.Count; i++)
                {
                    sock.SendTo(data, data.Length, SocketFlags.None, clients[i]);
                    //sock.Send(data, data.Length, SocketFlags.None);
                }

                data = new byte[1024];
            }
            catch (Exception e)
            {
                //socket error
                Console.WriteLine(e.Message);
            }
        }

1 个答案:

答案 0 :(得分:1)

问题出在这一行:

 sock.SendTo(data, data.Length, SocketFlags.None, clients[i]);

根据SendTo method doc

  

如果您使用的是面向连接的协议,则必须先进行   通过调用Connect方法或建立远程主机连接   使用Accept方法接受传入连接请求

您正在使用连接到端点A的套接字通过TCP将字节发送到端点B,这实际上不会起作用。

因此,不要将端点保留在客户端中,而是在listen方法中保留一个套接字列表:

while (true)
        {
            try
            {
                Socket handler = server.Accept();

                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientCommunication));
                clientThread.Start((object)handler);
                Console.WriteLine("ReceiveThread: " + clientThread.ThreadState);

                clients.Add(handler);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

并更改发送消息的循环:

for (int i = 0; i < clients.Count; i++)
{
    clients[i].Send(data);
}

提醒:为了避免错误,您需要在客户端套接字关闭后从客户端列表中删除项目。