gpt4 book ai didi

c# - 套接字 - Java 客户端,C# 服务器

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:40:03 24 4
gpt4 key购买 nike

我正在制作一个分为两部分的程序。

第 1 部分:在 PC 上运行的 C# 服务器套接字应用程序,监听命令并执行相应操作。

第 2 部分:在手机上运行的 Java 客户端套接字应用程序,它会在按下按钮时向 pc 发送命令。

目前,我可以从客户端向服务器发送命令,一切正常。但我的问题是:当我向服务器发送特定命令时,我希望服务器回复客户端,而客户端读取该回复。

只是,当客户端尝试读取时,它会超时。

Java 客户端程序:

class ClientThread implements Runnable 
{
public void run()
{
try
{
Socket socket = new Socket(serverIpAddress, serverPort);
socket.setSoTimeout(5000);

while (true)
{
try
{
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
Log.d("Nicklas", "Out it goes");
out.println(Command);

if (Command == "CMD:GetOptions<EOF>")
{
Log.d("Nicklas", "Getting options");
try
{
Log.d("Nicklas", "Line 1");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.d("Nicklas", "Line 2");
String answer = in.readLine();
Log.d("Nicklas", "answer = " + answer );
}
catch (Exception ee)
{
Log.d("Nicklasasasas", ee.toString());
}
}
break;

}

catch (Exception e)
{
Log.d("Nicklas", "CAE = " + e.toString());
break;

}
}
socket.close();
}
catch (ConnectException ee)
{
Log.d("Nicklas", "Kunne ikke forbinde");

}
catch (Exception e)
{
Log.d("Nicklasssssss", e.toString());
}
}
}

这被称为:

Thread cThread = new Thread(new ClientThread());
cThread.start();

并使用全局变量“Command”,它将包含不同的信息,具体取决于按下的按钮。

程序在“String answer = in.readline();”行失败除了“java.net.SocketTimeoutException”。

这是程序的 C# 服务器部分:

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));
clientThread.Start(client);
}
}

private void HandleClientComm(object client)
{
TcpClient tcpClient = (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));
string Input = (encoder.GetString(message, 0, bytesRead));
Input = Input.Trim();
object[] obj = new object[1];
obj[0] = Input;
if (Input == "CMD:GetOptions<EOF>")
{
try
{
byte[] buffer = encoder.GetBytes("CMD:Accepted");
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
MessageBox.Show("Client program asked for reply");
}
catch (Exception e)
{
MessageBox.Show("Oh it no work!: " + e.ToString());
}
}
else
{
Udfor(Input);
}


}

tcpClient.Close();
}

在 Form1() 中调用以下内容

this.tcpListener = new TcpListener(IPAddress.Any, 4532);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();

C# 服务器似乎工作正常,并显示消息框“客户端程序要求回复”

谁能发现错误?

最佳答案

我想通了!问题是 C#。当服务器发回命令“CMD:Accepted”时,它从未关闭套接字,因此 android 应用程序不知道它是否已完成读取!冲洗后立即关闭套接字 + 当然,如果我已经关闭了,就不再关闭它,成功了!

关于c# - 套接字 - Java 客户端,C# 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11154367/

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