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

时间:2012-06-10 08:33:49

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

我编写了一个java TCP服务器程序。我将在我的电脑上运行该程序。我已经编写了一个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客户端(运行在ANDROID EMULATOR中):

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();
        }

    }
}

2 个答案:

答案 0 :(得分:0)

您可以查看模拟器的network delay option吗? 您也可以看到机器上运行的进程不多,因为这些进程可能会窃取您希望仿真器使用的CPU周期

答案 1 :(得分:0)

几点提示

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

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

  2. 您可以通过首先构建一个未连接的套接字,然后将其与超时连接来解决此问题:

    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();
      
      }
      

      }