我已经构建了一个使用UDP
和TCP
套接字的聊天应用程序,首先让我引导您完成应用程序的机制:
从Android设备我发送一个UDP
数据包到一个组地址,其中有多个Windows设备收听它,当Windows设备收到数据包时,它会检查它是否有相同的电话号码所以我使用手机数字作为识别设备的唯一密钥,如果它具有我发送数据包的相同电话号码,它将使用密钥回复说:"ImReadyNow"
通过TCP
套接字发送这将使Android设备接受连接并充当该Windows设备的服务器,当我第一次开始制作该应用程序时,我遇到了类似的问题,来自Windows设备的响应键返回了它之前和之后的一些垃圾数据我解决了这个问题,通过在按摩时附加一个按键,然后在释放后拆分它,这样我遇到的旧问题就是当windows设备发送"Hello"
安卓设备时
"????Hello"
我通过在第一次按摩时附上钥匙来解决这个问题
我将收到"????$@!Hello"
然后我将收到的字符串分成两部分并仅显示按摩,这对我来说很好,直到我发现有时Android设备只收到垃圾数据这种情况随机发生,就像Windows设备发送"你好"那么"嗨"那么"你好"
我可以接受前两次按摩,但第三次按摩将收到"?????"
正如我告诉你它随机发生所以任何人都知道为什么会发生这种情况?
调试后br.readLine()
方法有时会返回"????"
,这就像我随机说的那样。
这是我使用的代码:
private class ChatClientThread extends Thread {
@Override
public void run() {
/*Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream=null;*/
// clientSocket=null;
try {
String data=EmpPhoneNumber;
DatagramChannel channel = DatagramChannel.open();
DatagramSocket socket = channel.socket();
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(52276));
socket.setBroadcast(true);
InetAddress group = InetAddress.getByName(
"224.0.1.2");
System.out.println("HELLO");
System.out.println(data);
DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(),
group, 52276);
serverSocket=new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(4125));
int i=0;
while(i<=30){
Thread.sleep(200);
socket.send(packet);
i++;
}
while(true){
clientSocket = serverSocket.accept();
BufferedReader br= new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
strAcceptConnection= br.readLine();
System.out.println(strAcceptConnection);
if(!strAcceptConnection.contains("$@!")){
updateTextView("<font color=\"#0000A0\"> <b>"+empNameChat+": </b></font>"+"Error in recieving msg");
}else{
String parts[]= strAcceptConnection.split("[$@!]+");
if(!parts[1].equals("ImReadyNoW")){
updateTextView("<font color=\"#0000A0\"> <b>"+empNameChat+": </b></font>"+parts[2]);
}}}
} catch (UnknownHostException e) {
e.printStackTrace();
final String eString = e.toString();
EmpDetails.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(EmpDetails.this, eString, Toast.LENGTH_LONG).show();
}
});
} catch (IOException e) {
e.printStackTrace();
final String eString = e.toString();
EmpDetails.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(EmpDetails.this, eString, Toast.LENGTH_LONG).show();
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
} finally {
EmpDetails.this.runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
}
}
public void sendMessage(View view) {
final String msg= et.getText().toString();
et.setText("");
new Thread(new Runnable() {
public void run() {
Socket sock= null;
try {
sock= new Socket(clientSocket.getInetAddress().getHostAddress(), 4125);
Log.v("str", "send sock: "+sock.getInetAddress().getHostAddress()+sock.isConnected()+Integer.toString(sock.getPort()));
} catch (UnknownHostException e) {
Log.v("ERRORTAG", ""+e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.v("ERRORTAG", ""+e.getMessage());
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
try {
DataOutputStream out= new DataOutputStream(sock.getOutputStream());
if(!msg.equals("")){
out.writeUTF("XX$@!"+MainActivity.empIdFromD+"$@!"+msg);
out.flush();
updateTextView("<font color=\"#800000\"> <b>"+"You"+": </b></font>" + msg);}
}
catch (IOException e) {
Log.v("ERRORTAG", ""+e.getMessage());
e.printStackTrace();
}
}
}).start();
}
public void updateTextView(String message) {
final String msg= message;
uiHandle.post(new Runnable() {
public void run() {
textView.setMovementMethod(new ScrollingMovementMethod());
Log.v("TAG", "updating textview");
//textView.append(msg+"\n");
textView.append(Html.fromHtml(msg));
textView.append("\n");
Log.v("TAG", "updated textview");
ScrollView sc= (ScrollView)findViewById(R.id.scrollView1);
sc.fullScroll(View.FOCUS_DOWN);
}
});
}
收到的字符串如何: logcat的:
PS。另一方面,Windows设备正确接收所有按摩。