3

C# 客户端无法将消息写入服务器,我认为将字节解析为字符串是错误的。我通过 ipaddress 和端口使用 connect 方法在客户端类中连接。但是我开始编译服务器没有改变,但客户端会关闭。

这是我的服务器:

class Server
{
    private TcpListener tcpListener;
    private Thread listenThread;

    public Server()
    {
        this.tcpListener = new TcpListener(IPAddress.Parse("10.0.0.44"), 3000);
        this.listenThread = new Thread(new ThreadStart(ListenForClients));
        this.listenThread.Start();
    }

    private void ListenForClients()
    {
        this.tcpListener.Start();

        while (true)
        {
            //blocks until a client has connected to the server
            TcpClient client = this.tcpListener.AcceptTcpClient();

            //create a thread to handle communication
            //with connected client
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            Thread clientThreadSend = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
            clientThreadSend.Start(client);
        }
    }

    private void HandleClientComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client;
        TcpClient sendClient = (TcpClient)client;
        NetworkStream clientStream = tcpClient.GetStream();

        byte[] message = new byte[4096];
        int bytesRead;

        while (true)
        {
            bytesRead = 0;

            try
            {
                //blocks until a client sends a message
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch
            {
                //a socket error has occured
                break;
            }

            if (bytesRead == 0)
            {
                //the client has disconnected from the server
                break;
            }

            //message has successfully been received
            ASCIIEncoding encoder = new ASCIIEncoding();
            System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));

            Console.WriteLine("To: " + tcpClient.Client.LocalEndPoint);
            Console.WriteLine("From: " + tcpClient.Client.RemoteEndPoint);
            Console.WriteLine(encoder.GetString(message, 0, bytesRead));
            clientStream.Flush();
            sendToClient("Test back", sendClient);
        }

        tcpClient.Close();
    }

    public void sendToClient(String line, TcpClient client)
    {
        try
        {
            NetworkStream stream = client.GetStream();
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer = encoder.GetBytes(line + "\0");

            stream.Write(buffer, 0, buffer.Length);
            stream.Flush();
        }
        catch
        {
            Console.WriteLine("Client Disconnected." + Environment.NewLine);

        }
    }

    public void broadcast(String line, TcpClient thisClient)
    {
        sendToClient(line, thisClient);
    }

    static void Main(string[] args)
    {
        new Server();
    }
}

这是我的客户:

public class Client
{
    TcpClient _tcpClient;

    // Connects the client to a server at the specified
    // IP address and port.
    public void Connect(IPAddress address, int port)
    {
        _tcpClient = new TcpClient();

        IPEndPoint serverEndPoint = new IPEndPoint(address, port);

        _tcpClient.Connect(serverEndPoint);

        // Create a thread to read data sent from the server.
        ThreadPool.QueueUserWorkItem(
           delegate
           {
               Read();
           });
    }

    // Sends bytes to the server.
    public void Send(byte[] buffer)
    {
        _tcpClient.GetStream().Write(
           buffer, 0, buffer.Length);

        _tcpClient.GetStream().Flush();
    }

    public void Read()
    {
        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] buffer = new byte[4096];
        int bytesRead;
        while (true)
        {
            bytesRead = _tcpClient.GetStream().Read(buffer, 0, 4096);
            Console.WriteLine(encoder.GetString(buffer, 0, bytesRead));
        }
    }

    public void Dispose()
    {
        _tcpClient.Close();
    }

    public static void Main(string[] args)
    {
        Client c = new Client();
        c.Connect(IPAddress.Parse("10.0.0.44"), 3000);
    }
}
4

1 回答 1

0

僵局

client没有发送数据,也无法发送任何数据,因为它被阻止了serversend

bytesRead = clientStream.Read(message, 0, 4096);
于 2012-12-09T18:44:18.600 回答