我正在使用套接字编程创建一个Android服务器客户端聊天应用。现在,在我的应用程序中,客户端消息将仅显示在服务器中。来自服务器的消息未显示。我只附加了java代码,因为我认为你会了解xml布局组件。
server.java
package com.example.androidsocketserver;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Server extends ActionBarActivity {
private ServerSocket serverSocket;
private Button btn;
private boolean willsendMsg = false;
Handler handler;
Thread serverThread = null;
public static final int serverPort = 6000;
public static String SERVERIP = "10.0.2.15";
TextView serverStatus;
TextView text;
EditText msgg;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server);
serverStatus = (TextView)findViewById(R.id.status);
text = (TextView)findViewById(R.id.chatt);
msgg = (EditText)findViewById(R.id.mesg);
btn = (Button)findViewById(R.id.send);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
willsendMsg = true;
}
});
handler = new Handler();
this.serverThread = new Thread(new ServerThread());
this.serverThread.start();
}
public class ServerThread implements Runnable{
Socket socket = null;
@Override
public void run() {
// TODO Auto-generated method stub
try {
serverSocket = new ServerSocket(serverPort);
while(true){
socket = serverSocket.accept();
handler.post(new Runnable(){
public void run(){
serverStatus.setText("New Connection accepted,and the address is" + socket.getInetAddress() + ", and the port is " + socket.getLocalPort());
}
});
CommunicationThread comm = new CommunicationThread(socket);
new Thread(comm).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Couldn't detect internet connection.");
}
});
e.printStackTrace();
}
}
}
public class CommunicationThread implements Runnable{
private Socket client;
BufferedReader input;
String chat = msgg.getText().toString();
public CommunicationThread(Socket clientt){
this.client = clientt;
try {
this.input = new BufferedReader(new InputStreamReader(client.getInputStream()));
if(willsendMsg){
willsendMsg=false;
OutputStream outputSteam = client.getOutputStream();
PrintWriter pw = new PrintWriter(outputSteam, true);
pw.println(chat);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(!Thread.currentThread().isInterrupted()){
try {
final String read = input.readLine();
handler.post(new Runnable(){
public void run(){
text.setText(text.getText().toString()+"Client :"+read+"\n");
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
protected void onStop() {
super.onStop();
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client.java
package com.example.androidclientsocket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Clientsocket extends ActionBarActivity {
private Socket socket;
private static final int serverIPPort = 5000;
private static String server_IP = "10.0.2.2";
Handler handlers;
TextView inb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clientsocket);
inb = (TextView)findViewById(R.id.inbox);
handlers = new Handler();
new Thread(new ClientThread()).start();
}
public class ClientThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
try {
InetAddress serverAdd = InetAddress.getByName(server_IP);
socket = new Socket(serverAdd,serverIPPort);
BufferedReader inputt = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final String box = inputt.readLine();
handlers.post(new Runnable(){
public void run(){
inb.setText(inb.getText().toString()+"Server :"+box+"\n");
}
});
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void Posting(View v){
EditText et = (EditText)findViewById(R.id.msg);
String message = et.getText().toString();
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println(message);
et.setText("");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
protected void onStop() {
super.onStop();
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
logcat没有显示按钮点击时的任何异常或错误。我已在清单文件中添加了Internet和访问网络权限。请帮助我找到导致服务器消息不发送到客户端并在那里显示的问题。