use*_*334 6 c# java sockets client android
我正在制作一个分为两部分的程序.
第1部分:C#server-socket在PC上运行的应用程序,监听命令并相应地执行操作.
第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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这称为:
Thread cThread = new Thread(new ClientThread());
cThread.start();
Run Code Online (Sandbox Code Playgroud)
并使用全局变量"Command",它将包含不同的信息,具体取决于按下的按钮.
该程序在"String answer = in.readline();"行中失败 除了"java.net.SocketTimeoutException".
这是程序的C#Server部分:
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();
}
Run Code Online (Sandbox Code Playgroud)
在Form1()中调用以下内容
this.tcpListener = new TcpListener(IPAddress.Any, 4532);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
Run Code Online (Sandbox Code Playgroud)
C#服务器似乎工作正常,并显示消息框"客户端程序要求回复"
谁能发现错误?
我想到了!问题出在 C# 上。当服务器发回命令“CMD:Accepted”时,它从未关闭套接字,因此 Android 应用程序不知道它是否已完成读取!冲洗后立即关闭插座+当然不会再次关闭它,如果我已经这样做了,就成功了!
| 归档时间: |
|
| 查看次数: |
6402 次 |
| 最近记录: |