我正在尝试在android上开发一个echo服务器/客户端。我有这个用于我的服务器
private TextView serverStatus, data;
// default ip
public static String SERVERIP = "10.0.2.2";
// designate a port
public static final int SERVERPORT = 6000;
private Handler handler = new Handler();
private ServerSocket serverSocket;
String a;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.server);
serverStatus = (TextView) findViewById(R.id.server_status);
data = (TextView) findViewById(R.id.data);
SERVERIP = getLocalIpAddress();
Thread fst = new Thread(new ServerThread());
fst.start();
}
public class ServerThread implements Runnable
{
public void run()
{
try
{
if (SERVERIP != null)
{
handler.post(new Runnable()
{
@Override
public void run()
{
serverStatus.setText("Listening on IP: " + SERVERIP);
}
}
);//end handler 1
serverSocket = new ServerSocket(SERVERPORT);
while (true)
{
// listen for incoming clients
Socket client = serverSocket.accept();
handler.post(new Runnable()
{
@Override
public void run()
{
serverStatus.setText("Connected.");
}
}
); // end handler 2
try
{
BufferedReader in = new BufferedReader( new InputStreamReader(client.getInputStream() ) );
PrintStream out = new PrintStream(client.getOutputStream());
String str = in.readLine(); // Read one line to \n or \r
out.print("Echo:"+str); // Echo input to client
System.out.println( str ); // Echo input to console
str = a;
in.close();
out.close(); // Close file
System.out.println("Connection closed");
handler.post(new Runnable() {
@Override
public void run() {
// do whatever you want to the front end
// this is where you can be creative
}
});
break;
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
}
});
e.printStackTrace();
}
}
} else {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Couldn't detect internet connection.");
}
});
}
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Error");
}
});
e.printStackTrace();
}
}
}
// gets the ip address of your phone's network
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
}
}
} catch (SocketException ex) {
Log.e("ServerActivity", ex.toString());
}
return null;
}
@Override
protected void onStop() {
super.onStop();
try {
// make sure you close the socket upon exiting
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} 这对我的客户来说
public class Client extends Activity implements OnClickListener{
private EditText serverIp;
private static EditText data;
private TextView receivedata;
private Button send;
private String serverIpAddress = "";
private boolean connected = false;
Socket socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
serverIp = (EditText) findViewById(R.id.server_ip);
receivedata = (TextView) findViewById(R.id.textView1);
data = (EditText) findViewById(R.id.data);
connectPhones = (Button) findViewById(R.id.connect_phones);
send = (Button) findViewById(R.id.send);
connectPhones.setOnClickListener(this);
send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v == send)
{
if (!connected)
{
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals(""))
{
Thread cThread = new Thread(new SendThread());
cThread.start();
}
}
} }
public class SendThread implements Runnable
{
String line;
final Handler handler = new Handler();
final Runnable updateUI = new Runnable() {
public void run() {
synchronized( line ) {
receivedata.setText( line );
}
}
};
public void run()
{
try
{
Log.d("ClientActivity", "C: Sending...");
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, 5000);
connected = true;
if(connected)
{
BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
PrintStream out = new PrintStream(socket.getOutputStream());
out.println(data.getText());
line = in.readLine(); // read server
System.out.println("Echo: " + line);
handler.post(updateUI);
in.close();
out.close();
}
// Close stream
// and connection
}
catch(Exception e) { System.out.print(e+""); }
}
}
} 它的工作原理只是第一次。它连接到服务器并回显您输入的数据然后停止。我希望套接字保持打开状态,以便监听所有数据,直到我停止它为止。我做错了什么?