C#TCP Server无法响应客户端

时间:2015-06-04 07:44:31

标签: c# tcp networkstream tcpserver

所以我现在的工作是创建一个可以同时响应并连接到多个客户端的服务器,这就是为什么我要创建一个线程服务器和一些测试客户端来检查的原因。

客户端可以通过按下按钮连接到服务器,它会向服务器发送消息,服务器将消息发送到其接口然后响应客户端。最后,客户端将收到响应并将其放到其界面上。

我现在遇到的问题是服务器可以从服务器接收消息并显示,但是服务器到客户端的响应不成功,即使它们是相似的。从调试器看,客户端似乎陷入了Read过程。

以下是我的服务器代码。我认为问题出在连接处理程序和客户端。我现在被困了,请告诉我我做错了什么@@

public class Server
{
    TcpListener serverSocket = new TcpListener(IPAddress.Any, 3000);
    TcpClient clientSocket = default(TcpClient);
    int counter = 0;
    private exampleCallback callback;

    public Server(exampleCallback Callback)
    {
        callback = Callback;
    }

    public void initServer()
    {
        serverSocket.Start();

        //Call back to main thread to update display
        if (callback != null)
            callback(">> Server Initialized!");

        Console.WriteLine(">> Server Initialized!");
    }

    public void waitConnection()
    {
        int counter = 0;
        while (true)
        {
            counter += 1;
            clientSocket = serverSocket.AcceptTcpClient();

            if (callback != null)
                callback(">> Client number: " + counter + " started!");

            Console.WriteLine(">> Client number: " + counter + " started!");

            handleClient handerler = new handleClient(new exampleCallback(receiveCallback), clientSocket, counter);
            Thread clientThread = new Thread(new ThreadStart(handerler.startConv));
            clientThread.Start();
        }
    }

    public void closeServer()
    {
        clientSocket.Close();
        serverSocket.Stop();
    }

    private void receiveCallback(string message)
    {
        callback(message);
    }


}

Connection Handerler的代码:

public class handleClient
{
    private exampleCallback callback2;
    TcpClient clientSocket;
    int clientNo;

    public handleClient(exampleCallback Callback, TcpClient client, int clNo)
    {
        callback2 = Callback;
        clientSocket = client;
        clientNo = clNo;
    }

    public void startConv()
    {
        int requestCount = 0; 
        byte[] byteFrom = new byte[10025];
        string XMLfromClient = null;
        string dataFromClient = null; 
        string serverResponse = null;
        string serverXMLResponse = null;
        NetworkStream serverStream = clientSocket.GetStream();

        requestCount = 0;
        while (true)
        {
            try
            {
                requestCount++;

                //Receive requerst from client
                serverStream.Read(byteFrom, 0, (int)clientSocket.ReceiveBufferSize);
                XMLfromClient = System.Text.Encoding.ASCII.GetString(byteFrom);
                callback2("\n >> From Client :" + clientNo + ". Message: \n" + XMLfromClient);


                //Console.WriteLine(">> From Client :" + clientNo + ". Message: \n" + XMLfromClient);

                //Send acknowledgement to client
                serverResponse = "Server to Client(" + clientNo + "). Request time: " + requestCount;  
                byte[] byteTo = Encoding.ASCII.GetBytes(serverResponse);
                serverStream.Write(byteTo, 0, byteTo.Length);

                serverStream.Flush();
                Console.WriteLine(">> " + serverResponse);
            }
            catch (Exception ex)
            {
                callback2(ex.ToString()); 
            }
        }
    }
}

最后是客户端大小的代码:

public partial class Form1 : Form
{
    System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
    NetworkStream serverStream;

    public Form1()
    {
        InitializeComponent();
    }

    public void msg(string mesg)
    {
        this.Display.Text += Environment.NewLine + " >> " + mesg;
    }

    private void SendButton_Click(object sender, EventArgs e)
    {
        serverStream = clientSocket.GetStream();
        byte[] outStream = System.Text.Encoding.ASCII.GetBytes("World Domination");
        serverStream.Write(outStream, 0, outStream.Length);
        serverStream.Flush();
        Thread receive = new Thread(new ThreadStart(ReceiveMess));

    }

    private void ReceiveMess()
    {
        serverStream = clientSocket.GetStream();
        byte[] inStream = new byte[10025];
        serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
        string returndata = System.Text.Encoding.ASCII.GetString(inStream);
        msg("Data from Server : " + returndata);
    }
    private void connectButton_Click(object sender, EventArgs e)
    {
        msg("Client Started");
        clientSocket.Connect("127.0.0.1", 3000);
        this.Display.Text = "Client Socket Program - Server Connected ...";
    }
}

2 个答案:

答案 0 :(得分:1)

您似乎忘了启动receive主题:

private void SendButton_Click(object sender, EventArgs e)
{
    // ... skipped .........
    Thread receive = new Thread(new ThreadStart(ReceiveMess));
    receive.Start(); // <--- try adding this line
}

答案 1 :(得分:0)

  

服务器将消息发送到其接口然后响应客户端

那么服务器是GUI应用程序吗?

基于代码,您似乎在GUI线程上有阻塞操作(例如 if(Integer.parseInt(s.toString())<=Integer.parseInt(tvBQuantity.getText().toString())) { //update string value in your model. //tvAQuantity.setText(s.toString()); } else { //update string value in your model. //tvAQuantity.setText(tvBQuantity.getText()); } )。这将失败。 GUI线程将阻塞(并且UI将无响应),这意味着工作人员对IO的更新将阻止在UI上等待(所有UI操作必须在主线程上发生)。

另外为每个连接启动一个新线程是个坏主意。线程很贵。

您需要从头开始重写(客户端和服务器)。

  • 在GUI线程/
  • 上避免阻止操作(如果你想要最大性能,甚至是文件IO)
  • 所有套接字操作都应该在线程池中运行(让系统为您管理线程)。
  • 避免在后台线程中等待GUI(更喜欢AcceptTcpClient而不是BeginInvoke)。

更好的方法是使用Invoke:系统将为您管理线程切换。