Android socket通信能发数据但不能接收到数据?
android做客户端,PC做服务端(C#编写),android发数据PC端可以收到,但PC端返回的数据android却接收不到,(android端为真机,非模拟器)奇怪...
android 做客户端,PC 做服务端(C#编写),
android 发数据PC端可以收到,但PC端返回的数据android却接收不到,(android端为真机,非模拟器)
奇怪的是,完全相同的java socket通信代码,放在android下收不到数据,
但建一个pc的java工程,还是这段代码,却可以收到数据?!
try {
Socket stSocket = new Socket("192.168.0.101",30000);
stSocket.getOutputStream().write(cmd.getBytes());
stSocket.getOutputStream().flush();
System.out.println("receiving..");
BufferedReader br=new BufferedReader(new InputStreamReader(stSocket.getInputStream()));
String rev;
rev=br.readLine();
System.out.println( "received:"+rev);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 展开
android 发数据PC端可以收到,但PC端返回的数据android却接收不到,(android端为真机,非模拟器)
奇怪的是,完全相同的java socket通信代码,放在android下收不到数据,
但建一个pc的java工程,还是这段代码,却可以收到数据?!
try {
Socket stSocket = new Socket("192.168.0.101",30000);
stSocket.getOutputStream().write(cmd.getBytes());
stSocket.getOutputStream().flush();
System.out.println("receiving..");
BufferedReader br=new BufferedReader(new InputStreamReader(stSocket.getInputStream()));
String rev;
rev=br.readLine();
System.out.println( "received:"+rev);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 展开
2个回答
展开全部
我C#项目中做过同样的Android移动Socket通信。
Android客户端:
SocketClient对象receive函数就调用读取函数,当然之前是打开了Socket连接。
public String receive() throws IOException{
BufferedReader reader = new BufferedReader(
new InputStreamReader(client.getInputStream()));
String txt=reader.readLine();
return txt;
}
Activity页面使用任务不间断监听接收。
public class SocketReceiveTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... arg0) {
SocketClient client = SocketClient.getInstance();
while (true)
{
try {
Thread.sleep(5000);
String re = client.receive();
if (re == null || (re != null && re.equals(""))) {
continue;
}
if (isCancelled())
return null;
//TODO:处理接收到消息
} catch (SocketException e) {
//服务端断开,启动重连任务
if (e.getMessage().contains("ECONNRESET")){
reconnectTask = new ReconnectServerTask();
reconnectTask.execute((Void) null);
}
return null;
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
}
}
展开全部
我C#项目中做过同样的Android移动Socket通信。
Android客户端:
SocketClient对象receive函数就调用读取函数,当然之前是打开了Socket连接。
12345678public String receive() throws IOException{ BufferedReader reader = new BufferedReader( new InputStreamReader(client.getInputStream())); String txt=reader.readLine(); return txt;}
Activity页面使用任务不间断监听接收。123456789101112131415161718192021222324252627282930313233343536373839public class SocketReceiveTask extends AsyncTask<Void, Void, Void>{ @Override protected Void doInBackground(Void... arg0) { SocketClient client = SocketClient.getInstance(); while (true) { try { Thread.sleep(5000); String re = client.receive(); if (re == null || (re != null && re.equals(""))) { continue; } if (isCancelled()) return null; //TODO:处理接收到消息 } catch (SocketException e) { //服务端断开,启动重连任务 if (e.getMessage().contains("ECONNRESET")){ reconnectTask = new ReconnectServerTask(); reconnectTask.execute((Void) null); } return null; } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } } }}
Android客户端:
SocketClient对象receive函数就调用读取函数,当然之前是打开了Socket连接。
12345678public String receive() throws IOException{ BufferedReader reader = new BufferedReader( new InputStreamReader(client.getInputStream())); String txt=reader.readLine(); return txt;}
Activity页面使用任务不间断监听接收。123456789101112131415161718192021222324252627282930313233343536373839public class SocketReceiveTask extends AsyncTask<Void, Void, Void>{ @Override protected Void doInBackground(Void... arg0) { SocketClient client = SocketClient.getInstance(); while (true) { try { Thread.sleep(5000); String re = client.receive(); if (re == null || (re != null && re.equals(""))) { continue; } if (isCancelled()) return null; //TODO:处理接收到消息 } catch (SocketException e) { //服务端断开,启动重连任务 if (e.getMessage().contains("ECONNRESET")){ reconnectTask = new ReconnectServerTask(); reconnectTask.execute((Void) null); } return null; } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } } }}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询