Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用android和PC的TCP客户端和服务器程序的性能问题_Java_Android_Tcp_Android Emulator_Socket.io - Fatal编程技术网

Java 使用android和PC的TCP客户端和服务器程序的性能问题

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中运行): 你能检查一下你的模拟器吗? 另

我已经编写了一个java TCP服务器程序。我将在我的电脑上运行这个程序。我已经编写了一个java TCP客户端程序,我将在android emulator上运行它。我将使用ip地址10.0.2.2连接到服务器,因为我使用的是android emulator。但是性能很差。服务器在近8-10分钟后接收到客户端发送的数据。并且模拟器没有从服务器接收任何数据。请看看哪里出了问题

TCP服务器(在PC中运行):

TCP客户端(在ANDROID EMULATOR中运行):

你能检查一下你的模拟器吗? 另外,您是否可以看到,计算机上运行的进程并不多,因为这些进程可能会占用您希望模拟器消耗的CPU周期

  • 要创建客户端套接字,请使用以下命令

    构造函数
    套接字(字符串主机,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();
    
    }