将数据发送到任何连接的客户端

时间:2014-01-06 10:37:43

标签: c# sockets

我已创建服务器应用程序以接受来自客户端的连接。客户端连接到服务器后......他们可以在彼此之间发送数据和接收数据,但是当连接另一个客户端时...服务器无法将数据发送到连接的客户端。

我需要帮助如何在此服务器上保存连接的客户端...以及如何将数据发送到指定的客户端。

服务器类是:

namespace WindowsApplication11
{
    public partial class Form1 : Form
    {
        private TcpListener tcpListener;
        private Thread listenThread;

        public Form1()
        {
            InitializeComponent();
        }

        TcpListener myList;

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                myList = new TcpListener(IPAddress.Any, 8001);

                /* Start Listeneting at the specified port */
                myList.Start();
                while (true)
                {
                    TcpClient client = myList.AcceptTcpClient();

                    saveclient(" Client " + client.Client.RemoteEndPoint.ToString());

                    // - receive msg from client - 
                    byte[] bb = new byte[10000];
                    int kb = client.Client.Receive(bb);

                    string stringu = "";
                    for (int i = 0; i < kb; i++)
                        stringu += Convert.ToChar(bb[i]);


                    // - send msg to accepted client - 

                    Byte[] datat = System.Text.Encoding.ASCII.GetBytes("nje->    " + stringu);

                    NetworkStream stream = client.GetStream();

                    stream.Write(datat, 0, datat.Length);

                    stream.Flush();
                }
            }
            catch (Exception ea)
            {
                Console.WriteLine("Error..... " + ea.Message);
            }
        }

        void saveclient(string idlidhje)
        {
            try
            {
                System.IO.StreamWriter stw = System.IO.File.AppendText("d:\\clients.txt");
                string teksti = System.String.Format("{0:G}: {1}.", System.DateTime.Now, idlidhje);
                stw.WriteLine(teksti + "\n");
                stw.Close();
            }
            catch (Exception eks)
            {

            }
        }
    }
}

客户端类是:

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
         IPAddress addr = IPAddress.Any;

        byte[] data = new byte[1024];
        string input, stringData;
        TcpClient server;
        try
        {
             server = new TcpClient("127.0.0.1", 8001);


            // - send msg - 

             Byte[] datat = System.Text.Encoding.ASCII.GetBytes("Hello");

             NetworkStream stream = server.GetStream();

             stream.Write(datat, 0, datat.Length);

             stream.Flush();





             // - receive msg - 
             byte[] bb = new byte[10000];
             int kb = server.Client.Receive(bb);

             string stringu = "";
             for (int i = 0; i < kb; i++)
                 stringu += Convert.ToChar(bb[i]);




        }
        catch (SocketException fd)
        {
            MessageBox.Show("Cannot connect " + fd.Message.ToString());

        }
    }

1 个答案:

答案 0 :(得分:0)

罗伯特在SO上回答了类似的问题:

https://stackoverflow.com/a/15878306/17646

基本上,您必须将连接的客户端保留在列表中(每次while循环再次启动时,您的TcpClient对象'client'都会丢失),并且您需要线程或异步方法来处理不同的客户端。