Java Android中的套接字仍然被卡住
我有一个用C#编写的服务器和一个用Android编写的客户端。如果我从客户端(Android)向服务器(c#)发送消息,从服务器向客户端发送消息,一切都会正常工作。如果我尝试从客户机、服务器和客户机发送一条消息,客户机仍然无法从服务器读取消息。有什么问题吗? 我的客户代码是:Java Android中的套接字仍然被卡住,java,android,tcp,Java,Android,Tcp,我有一个用C#编写的服务器和一个用Android编写的客户端。如果我从客户端(Android)向服务器(c#)发送消息,从服务器向客户端发送消息,一切都会正常工作。如果我尝试从客户机、服务器和客户机发送一条消息,客户机仍然无法从服务器读取消息。有什么问题吗? 我的客户代码是: sendBytes("HELLOX".getBytes()); readBytes(byDataReceived);//here it gets stucked ... try {
sendBytes("HELLOX".getBytes());
readBytes(byDataReceived);//here it gets stucked
...
try
{
int nrsend=sendBytes("HELLOX".getBytes());
readBytes(byDataReceived);
}
catch (Exception se)
{
Log.d("IdealLog","Exception: "+se.getMessage()+" ");
Toast.makeText(context, se.getMessage()+" " , 10000).show();
// MessageBox.Show(se.Message);
return false;
}
...
public static int readBytes(byte[] myByteArray) throws IOException
{
Log.d("IdealLog","readBytes-begin");
InputStream in = socket.getInputStream();
BufferedReader buffreader = new BufferedReader(new InputStreamReader(in));
String finalText = "";
String text = "";
while ((text = buffreader.readLine()) != null)
{
finalText += text;
}
myByteArray=new byte[myByteArray.length];
myByteArray=EncodingUtils.getAsciiBytes(finalText);
Log.d("IdealLog","Input Stream: "+finalText);
Log.d("IdealLog","TEST: "+EncodingUtils.getAsciiString(myByteArray));
Log.d("IdealLog","readBytes-end");
byDataReceived=myByteArray;
buffreader.close();
return myByteArray.length;//myByteArray.length;
}//readBytes end
public static int sendBytes(byte[] myByteArray) throws IOException
{
return sendBytes(myByteArray, 0, myByteArray.length);
}//sendBytes end
public static int sendBytes(byte[] myByteArray, int start, int len) throws IOException
{
if (len < 0)
{
throw new IllegalArgumentException("Negative length not allowed");
}
if (start < 0 || start >= myByteArray.length)
{
throw new IndexOutOfBoundsException("Out of bounds: " + start);
}
OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
// dos.writeInt(len);
if (len > 0)
{
dos.write(myByteArray, start, len);
}
int size=dos.size();
dos.flush();
return size;
}//sendBytes end
修改为:
//if i comment out from this 3 lines, everything is working fine
byte[] data3 = new byte[200];//this
client.Receive(data3);//this
Console.WriteLine("Received data from CLIENT TEST2: {0}", System.Text.ASCIIEncoding.ASCII.GetString(data3));//this
client.Send(data2, data2.Length, SocketFlags.None);
Console.WriteLine("Disconnected from {0}", clientep.Address);
client.Close();
socket.Close();
Console.ReadLine();
}
我忘了提到,当客户端被卡住时,只会打印以下行:readBytes Begin in the server(服务器)窗口中,我会得到以下内容:“Received data from client TEST1:HELLOX”(从客户端TEST1:HELLOX接收数据)在server(服务器)窗口中,我会得到以下内容:“Received data from client TEST1:HELLOX”在android日志中:readBytes Begini找到了解决我问题的方法:
//if i comment out from this 3 lines, everything is working fine
byte[] data3 = new byte[200];//this
client.Receive(data3);//this
Console.WriteLine("Received data from CLIENT TEST2: {0}", System.Text.ASCIIEncoding.ASCII.GetString(data3));//this
client.Send(data2, data2.Length, SocketFlags.None);
Console.WriteLine("Disconnected from {0}", clientep.Address);
client.Close();
socket.Close();
Console.ReadLine();
}