C#tcp server-client,无法发送消息

时间:2012-12-09 18:38:27

标签: c# tcp client-server

C#客户端无法向服务器写消息,我认为解析字节到字符串是坏的。我通过ipaddress和port在connect类中连接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);
    }
}

1 个答案:

答案 0 :(得分:0)

<强>死锁

您的client 发送数据,server也无法send任何数据,因为它被阻止

bytesRead = clientStream.Read(message, 0, 4096);