Java 使用android和PC的TCP客户端和服务器程序的性能问题
我已经编写了一个java TCP服务器程序。我将在我的电脑上运行这个程序。我已经编写了一个java TCP客户端程序,我将在android emulator上运行它。我将使用ip地址10.0.2.2连接到服务器,因为我使用的是android emulator。但是性能很差。服务器在近8-10分钟后接收到客户端发送的数据。并且模拟器没有从服务器接收任何数据。请看看哪里出了问题 TCP服务器(在PC中运行): TCP客户端(在ANDROID EMULATOR中运行): 你能检查一下你的模拟器吗? 另外,您是否可以看到,计算机上运行的进程并不多,因为这些进程可能会占用您希望模拟器消耗的CPU周期Java 使用android和PC的TCP客户端和服务器程序的性能问题,java,android,tcp,android-emulator,socket.io,Java,Android,Tcp,Android Emulator,Socket.io,我已经编写了一个java TCP服务器程序。我将在我的电脑上运行这个程序。我已经编写了一个java TCP客户端程序,我将在android emulator上运行它。我将使用ip地址10.0.2.2连接到服务器,因为我使用的是android emulator。但是性能很差。服务器在近8-10分钟后接收到客户端发送的数据。并且模拟器没有从服务器接收任何数据。请看看哪里出了问题 TCP服务器(在PC中运行): TCP客户端(在ANDROID EMULATOR中运行): 你能检查一下你的模拟器吗? 另
套接字(字符串主机,int端口)
可以无限期地阻塞,直到建立到主机的初始连接Socket s = new Socket();
s.connect(new InetSocketAddress(host, port), timeout);
PrintWriter
,因为它将作为套接字的低级字节和字符数据之间的桥梁
例如:
服务器的代码
public class ServerTest {
ServerSocket s;
public void go() {
try {
s = new ServerSocket(44457);
while (true) {
Socket incoming = s.accept();
Thread t = new Thread(new MyCon(incoming));
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class MyCon implements Runnable {
Socket incoming;
public MyCon(Socket incoming) {
this.incoming = incoming;
}
@Override
public void run() {
try {
PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
true);
InputStreamReader isr = new InputStreamReader(
incoming.getInputStream());
BufferedReader br = new BufferedReader(isr);
String inp = null;
boolean isDone = true;
System.out.println("TYPE : BYE");
System.out.println();
while (isDone && ((inp = br.readLine()) != null)) {
System.out.println(inp);
if (inp.trim().equals("BYE")) {
System.out
.println("THANKS FOR CONNECTING...Bye for now");
isDone = false;
s.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
try {
s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ServerTest().go();
}
}Socket s = new Socket();
s.connect(new InetSocketAddress(host, port), timeout);
public class ServerTest {
ServerSocket s;
public void go() {
try {
s = new ServerSocket(44457);
while (true) {
Socket incoming = s.accept();
Thread t = new Thread(new MyCon(incoming));
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class MyCon implements Runnable {
Socket incoming;
public MyCon(Socket incoming) {
this.incoming = incoming;
}
@Override
public void run() {
try {
PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
true);
InputStreamReader isr = new InputStreamReader(
incoming.getInputStream());
BufferedReader br = new BufferedReader(isr);
String inp = null;
boolean isDone = true;
System.out.println("TYPE : BYE");
System.out.println();
while (isDone && ((inp = br.readLine()) != null)) {
System.out.println(inp);
if (inp.trim().equals("BYE")) {
System.out
.println("THANKS FOR CONNECTING...Bye for now");
isDone = false;
s.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
try {
s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ServerTest().go();
}