【问题标题】:C# Read data sent by serverC#读取服务器发送的数据
【发布时间】:2019-05-10 07:02:55
【问题描述】:

我正在 Visual Studio 上使用 C# 开发一个聊天框应用程序。我有一个控制台服务器和一个带有图形界面的聊天。 我成功发送数据:聊天框-> 服务器。还有服务器 - > 聊天框,但仅适用于发送信息的聊天框(当聊天框发送信息时,服务器必须将其发送给所有人,但它仅适用于发送者)。 我希望所有应用程序都收到信息。 实际上 : 应用程序 1 -> 服务器 服务器 -> 应用程序 1 不是应用程序 2 我不明白为什么。感谢您未来的帮助。

        public void start()
    {
        TcpListener l = new TcpListener(new IPAddress(new byte[] { 127, 0, 0, 1 }), port);
        l.Start();

        while(true)
        {
            TcpClient comm = l.AcceptTcpClient();
            Console.WriteLine("Connection established @" + comm);
            new Thread(new Receiver(comm).doOperation).Start();
        }
    }

class Receiver
    {
        private TcpClient comm;

        public Receiver(TcpClient s)
        {
            comm = s;
        }

        public void doOperation()
        {
            Semaphore s = new Semaphore(1, 1);
          while (true)
             {
                s.WaitOne();
                NetworkStream nwStream = comm.GetStream();
                byte[] buffer = new byte[comm.ReceiveBufferSize];

                //---read incoming stream---
                int bytesRead = nwStream.Read(buffer, 0, comm.ReceiveBufferSize);

                //---convert the data received into a string---
                string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                Console.WriteLine("Received " + dataReceived);

                s.Release();
                s.WaitOne();

                Console.WriteLine("Send to all : " + dataReceived);
                nwStream.Write(buffer, 0, bytesRead);
                s.Release();


            }

        }
    }

我在聊天框中收到信息:

public Form1(string h, int p)
    {
       ...

        comm = new TcpClient(h, p);
        nwStream = comm.GetStream();


        Thread th1 = new Thread(receive);
        th1.Start();



    }

    public void receive()
    {
        while (true)
        {
            byte[] bytesToRead = new byte[comm.ReceiveBufferSize];
            int bytesRead = nwStream.Read(bytesToRead, 0, comm.ReceiveBufferSize);
            MessageBox.Show("Appli info received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));

        }
    }

【问题讨论】:

    标签: c# multithreading server tcplistener


    【解决方案1】:

    您在 [服务器 客户端] 之间使用什么样的协议?

    需要在两端实现WebSocket协议。服务器将能够向客户端发送事件。

    在协议识别问题之后,您只需要构建/调用事件到 C# 后端和 reveivers javascript 端。

    【讨论】:

    • 我使用 TCP。服务器能够将事件发送给客户端,但仅限于发送它的那个人。如果我运行 2 个应用程序并且第一个应用程序发送一个事件,服务器会将他发回,但不会发送给第二个应用程序。
    • 取决于您如何管理活动连接。这次我有同样的主题。但是您的应用程序只存储客户端会话,当我们想要发送事件时,我们只需迭代客户端会话 ID 并发送事件,因为 WebSocket 协议允许。
    • 我该怎么做?为了帮助我,我使用了这个:stackoverflow.com/questions/10182751/…
    • 您可以构建一个 List 服务器对象。连接服务器时添加每个客户端,离开时删除。如果您需要向每个连接的客户端发送消息,请使用 foreach 在此对象上循环并向所有人发送消息。这是一个websocket主题stackoverflow.com/questions/10200910/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多