使用BinaryReader/BinaryWriter构建聊天

ale*_*tor 1 c# tcpclient binaryreader binarywriter server-communication

您好我正在尝试使用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;
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

客户:

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());
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Mik*_*kis 5

您的所有客户端都被阻止,var message = Console.ReadLine();因此没有一个客户端进一步向下查看传入的消息是否已到达.

您必须找到一种方法来使用异步I/O从控制台读取.仍然,传入的消息可能在用户键入时到达,在这种情况下,键入的文本将与控制台上的传入消息一起出现乱码.控制台不是聊天应用程序的非常好的用户界面.