使用BinaryReader/BinaryWriter建立一个聊天

本文关键字:一个 聊天 BinaryReader BinaryWriter 建立 使用 | 更新日期: 2023-09-27 18:13:10

你好,我正在尝试使用BinaryReader/BinaryWriter建立聊天,我进入了一个死胡同,我无法弄清楚如何使我的服务器将消息发送到所有连接的客户端..

我试过将所有客户端添加到列表中,并在列表上运行foreach循环将消息发送到每个连接的客户端,但没有锻炼。

服务器:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace server {
    internal class Program {
        public static int counter = 0;
        //List<Client> clientList = new List <Client>();
        private static readonly IPAddress sr_ipAddress = IPAddress.Parse("127.0.0.1");
        public TcpListener Listener = new TcpListener(sr_ipAddress, 8888);
        public static void Main(string[] args) {
            Program server = new Program();
            server.Start();
            Console.ReadKey();
        }
        public void Start() {
            Listener.Start();
            Console.WriteLine("Server started");
            StartAccept();
        }
        private void StartAccept() {
            Listener.BeginAcceptTcpClient(HandleAsyncConnection, Listener);
        }
        public void HandleAsyncConnection(IAsyncResult res) {
            StartAccept(); //listen for new connections again
            TcpClient clientSocket = Listener.EndAcceptTcpClient(res);
            Client client = new Client(clientSocket);
            client.StartClient();
        }
    }
    internal class Client {
        public TcpClient ClientSocket;
        public string CleintName{get; set;}
        public Client(TcpClient inClientSocket) {
            ClientSocket = inClientSocket;
            NetworkStream netStream = ClientSocket.GetStream();
            BinaryReader Listen = new BinaryReader(netStream);
            CleintName = Listen.ReadString();
        }
        public void StartClient() {
            Thread clientThread = new Thread(Chat);
            clientThread.Start();
            Console.WriteLine("New Client connected {0} => {1}", ClientSocket.GetHashCode(), CleintName);
        }
        private string _message;
        //private string _serverMessage;
        public void Chat() {
            NetworkStream netStream = ClientSocket.GetStream();
            BinaryReader listen = new BinaryReader(netStream);
            BinaryWriter send = new BinaryWriter(netStream);
            while (true) {
                //listening int1
                try {
                    _message = listen.ReadString();
                    Console.WriteLine("{0} :{1}", CleintName, _message);
                    send.Write(CleintName + ": " + _message);
                    //Send.Write(CleintName + " :" + _message);
                }
                catch (Exception ex) {
                    listen.Close();
                    send.Close();
                    netStream.Close();
                    Console.WriteLine(ex.Message);
                    return;
                }
            }
        }
    }
}
客户:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace tcpClient {
    internal class Program {
        private static readonly IPAddress s_ipAddress = IPAddress.Parse("127.0.0.1");
        private static readonly TcpClient s_client = new TcpClient();
        private static void Main(string[] args) {
            //Console.WriteLine("Press Enter to start");
            //Console.ReadLine();
            try {
                s_client.Connect(s_ipAddress, 8888);
            }
            catch (Exception ex) {
                Console.WriteLine("{0}", ex.Message);
                Console.ReadKey();
                return;
            }
            NetworkStream netStream = s_client.GetStream();
            BinaryReader listen = new BinaryReader(netStream);
            BinaryWriter send = new BinaryWriter(netStream);
            Console.WriteLine("Connected");
            Console.Write("Enter name: ");
            string name = Console.ReadLine();
            send.Write(name);
            Console.WriteLine("Chat started");
            while (true) {
                var message = Console.ReadLine();
                send.Write(message);
                //Console.WriteLine("{0}: {1}", name, message);
                Console.WriteLine(listen.ReadString());
            }
        }
    }
}

使用BinaryReader/BinaryWriter建立一个聊天

您的所有客户端都在var message = Console.ReadLine();上被阻塞,因此它们都不会继续向下查看传入消息是否已经到达。

您必须找到一种方法来使用异步I/O从控制台读取。但是,在用户输入时可能会收到传入消息,在这种情况下,输入的文本将与控制台上的传入消息混淆在一起。对于聊天应用程序来说,控制台不是一个很好的用户界面。

您的代码在客户端和服务器端都有问题。@Mike Nakis的回答已经涵盖了前者,现在我要讨论后者。

我试过将所有客户端添加到列表中,并在列表上运行foreach循环将消息发送到每个连接的客户端,但没有锻炼。

我不知道什么没有锻炼,但没有别的办法-你与连接的客户端保持某种类型的列表。客户端(这里的客户端是指客户端连接)必须保持对服务器的引用,并通知服务器收到消息。服务器将把消息广播给所有当前连接的客户端。

下面是你的服务器代码的一个简单的版本,它就是这样做的:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace server
{
    internal class Server
    {
        private static readonly IPAddress sr_ipAddress = IPAddress.Parse("127.0.0.1");
        public static void Main(string[] args)
        {
            Server server = new Server();
            server.Start();
            Console.ReadKey();
        }
        TcpListener Listener = new TcpListener(sr_ipAddress, 8888);
        HashSet<Client> Clients = new HashSet<Client>();
        object syncGate = new object();
        public void Start()
        {
            Listener.Start();
            Console.WriteLine("Server started");
            StartAccept();
        }
        private void StartAccept()
        {
            Listener.BeginAcceptTcpClient(HandleAsyncConnection, Listener);
        }
        private void HandleAsyncConnection(IAsyncResult res)
        {
            StartAccept(); //listen for new connections again
            var clientSocket = Listener.EndAcceptTcpClient(res);
            var client = new Client(this, clientSocket);
            client.StartClient();
            lock (syncGate)
            {
                Clients.Add(client);
                Console.WriteLine("New Client connected {0} => {1}", client.ClientSocket.GetHashCode(), client.ClientName);
            }
        }
        internal void OnDisconnected(Client client)
        {
            lock (syncGate)
            {
                Clients.Remove(client);
                Console.WriteLine("Client disconnected {0} => {1}", client.ClientSocket.GetHashCode(), client.ClientName);
            }
        }
        internal void OnMessageReceived(Client sender, string message)
        {
            lock (syncGate)
            {
                Console.WriteLine("{0}: {1}", sender.ClientName, message);
                foreach (var client in Clients)
                    client.OnMessageReceived(sender, message);
            }
        }
    }
    internal class Client
    {
        public readonly Server Server;
        public TcpClient ClientSocket;
        public string ClientName { get; set; }
        public Client(Server server, TcpClient clientSocket)
        {
            Server = server;
            ClientSocket = clientSocket;
            var netStream = ClientSocket.GetStream();
            var listen = new BinaryReader(netStream);
            ClientName = listen.ReadString();
        }
        public void StartClient()
        {
            var clientThread = new Thread(Chat);
            clientThread.Start();
        }
        private void Chat()
        {
            try
            {
                var netStream = ClientSocket.GetStream();
                var listen = new BinaryReader(netStream);
                while (true)
                {
                    try
                    {
                        var message = listen.ReadString();
                        Server.OnMessageReceived(this, message);
                    }
                    catch (Exception ex)
                    {
                        listen.Close();
                        netStream.Close();
                        Console.WriteLine(ex.Message);
                        return;
                    }
                }
            }
            finally
            {
                Server.OnDisconnected(this);
            }
        }
        internal void OnMessageReceived(Client sender, string message)
        {
            var netStream = ClientSocket.GetStream();
            var send = new BinaryWriter(netStream);
            send.Write(sender.ClientName + ": " + message);
        }
    }
}

和一个快速的脏测试客户端:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
namespace Samples
{
    static class ChatClient
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var name = GetChatName();
            if (string.IsNullOrEmpty(name)) return;
            var ipAddress = IPAddress.Parse("127.0.0.1");
            var client = new TcpClient();
            try
            {
                client.Connect(ipAddress, 8888);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            var netStream = client.GetStream();
            var send = new BinaryWriter(netStream);
            send.Write(name);
            var form = new Form { Text = "Chat - " + name };
            var tbSend = new TextBox { Dock = DockStyle.Bottom, Parent = form };
            var tbChat = new TextBox { Dock = DockStyle.Fill, Parent = form, Multiline = true, ReadOnly = true };
            var messages = new List<string>();
            tbSend.KeyPress += (_s, _e) =>
            {
                if (_e.KeyChar == 13 && !string.IsNullOrWhiteSpace(tbSend.Text))
                {
                    send.Write(tbSend.Text);
                    tbSend.Text = string.Empty;
                    _e.Handled = true;
                }
            };
            Action<string> onMessageReceived = message =>
            {
                if (messages.Count == 100) messages.RemoveAt(0);
                messages.Add(message);
                tbChat.Lines = messages.ToArray();
            };
            var listener = new Thread(() =>
            {
                var listen = new BinaryReader(netStream);
                while (true)
                {
                    var message = listen.ReadString();
                    form.BeginInvoke(onMessageReceived, message);
                }
            });
            listener.IsBackground = true;
            listener.Start();
            Application.Run(form);
        }
        static string GetChatName()
        {
            var form = new Form { Text = "Enter name:", StartPosition = FormStartPosition.CenterScreen };
            var tb = new TextBox { Parent = form, Top = 8, Left = 8, Width = form.ClientSize.Width - 16, Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top };
            var okButton = new Button { Parent = form, Text = "OK", DialogResult = DialogResult.OK, Left = 8 };
            var cancelButon = new Button { Parent = form, Text = "Cancel", Left = okButton.Right + 8 };
            okButton.Top = cancelButon.Top = form.ClientSize.Height - okButton.Height - 8;
            okButton.Anchor = cancelButon.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
            form.AcceptButton = okButton;
            form.CancelButton = cancelButon;
            var dr = form.ShowDialog();
            return dr == DialogResult.OK ? tb.Text : null;
        }
    }
}