我写了一个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模拟器中运行):
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();
}
}
}发布于 2012-06-10 16:51:59
你能检查你的模拟器的network delay option吗?另外,您是否可以看到计算机上运行的进程并不多,因为这些进程可能会窃取您希望模拟器消耗的CPU周期
发布于 2012-06-10 17:55:25
小贴士
用于创建客户端套接字的
在建立到主机的初始连接之前,构造函数Socket(String host, int port)可以无限期地阻塞。
您可以通过首先构造一个未连接的套接字,然后使用超时将其连接起来来克服此问题:
Socket s = new Socket();
s.connect(new InetSocketAddress(host, port), timeout);服务器端的
PrintWriter,因为它将作为来自套接字的低级字节和字符数据之间的桥梁。例如:服务器的代码
公共类ServerTest { ServerSocket s;public void go() { try {s=新ServerSocket(44457);while (true) {套接字传入= s.accept();线程t=新线程(新MyCon(传入));t.start();}} catch (IOException e) { e.printStackTrace();}}类MyCon实现Runnable {套接字传入;公共MyCon(套接字传入){ this.incoming =传入;} @Override public void run() { try { PrintWriter pw =新PrintWriter(incoming.getOutputStream(),true);InputStreamReader isr =新InputStreamReader( incoming.getInputStream());BufferedReader br =新类型(Isr);String inp = null;boolean isDone = true;System.out.println(“类型:再见”);System.out.println();while (isDone && ((inp = br.readLine()) != null)) { System.out.println(inp);if (inp.trim().equals("BYE")) { System.out .println(“感谢CONNECTING...Bye现在”);isDone = false;s.close();} catch (IOException e) { // TODO自动生成的catch块try { s.close();} catch (IOException e1) { // TODO自动生成的catch块e1.printStackTrace();} e.printStackTrace();}公共静态空main(String[] args) {新ServerTest().go();}
}
https://stackoverflow.com/questions/10967343
复制相似问题