【问题标题】:performance issue with TCP client and server program using android and PC使用 android 和 PC 的 TCP 客户端和服务器程序的性能问题
【发布时间】:2012-06-10 08:33:49
【问题描述】:

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

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

import java.io.*;
import java.net.*;

class TCPServer
{
   public static void main(String argv[]) throws Exception
      {
         String clientSentence;
         String capitalizedSentence=null;
         ServerSocket welcomeSocket = new ServerSocket(9000);

         while(true)
         {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient =new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            System.out.println("Received: " + clientSentence);
            if(clientSentence.equals("IS COMPUTER ON?"))
            {
                capitalizedSentence = "YES SYSTEM IS ON.";
            }
            outToClient.writeBytes(capitalizedSentence);
         }
      }
}

TCP 客户端(在安卓模拟器中运行):

package a.b.c;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class WifitestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try
        {
            String sentence="IS COMPUTER ON?";
            String modifiedSentence=sentence;
            Socket clientSocket = new Socket("10.0.2.2", 9000);
            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            outToServer.writeBytes(sentence);
            modifiedSentence = inFromServer.readLine();
            TextView a=(TextView)findViewById(R.id.textView1);
            a.setText(modifiedSentence);
            a.showContextMenu();
            clientSocket.close();
        }
        catch(Exception e)
        {
            TextView a=(TextView)findViewById(R.id.textView1);
            a.setText(e.toString());
            a.showContextMenu();
        }

    }
}

【问题讨论】:

    标签: java android tcp android-emulator socket.io


    【解决方案1】:

    你能检查一下你的模拟器的network delay option 吗? 您还可以看到机器上没有多少进程正在运行,因为这些进程可能会窃取您希望模拟器消耗的 CPU 周期

    【讨论】:

      【解决方案2】:

      小贴士

      1. 要创建客户端套接字,请使用以下内容

        构造函数Socket(String host, int port) 可以无限期阻塞,直到与主机建立初始连接。

      您可以通过首先构造一个未连接的套接字然后使用超时连接它来克服这个问题:

      Socket s = new Socket();
      s.connect(new InetSocketAddress(host, port), timeout);
      
      1. 在服务器端仅使用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();
        
        }
        

        }

      【讨论】:

        猜你喜欢
        • 2012-09-09
        • 1970-01-01
        • 2015-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-21
        • 2012-05-13
        • 1970-01-01
        相关资源
        最近更新 更多