gpt4 book ai didi

C# TCP 服务器无法响应客户端

转载 作者:可可西里 更新时间:2023-11-01 02:51:40 25 4
gpt4 key购买 nike

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

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

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

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

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);
}


}

连接处理程序的代码:

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 ...";
}
}

最佳答案

您似乎忘记启动receive 线程:

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

关于C# TCP 服务器无法响应客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30637884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com