我已经实现了在我的 PC 上运行的 Java TCP 服务器,它将接受从运行客户端的 Android 设备发送的传入消息,也实现了。服务器只是接收来自 Android 设备的消息并发送回一个答案。一切正常,直到我尝试从 Android 设备发送第二条消息:然后,应用程序崩溃!
但我不知道为什么。我使用一个按钮来发送之前编写的消息。
这是服务器线程代码(线程只是从 main using 调用run()
):
public class ServerThread extends Thread {
ServerSocket serverSocket;
public ServerThread() {
}
public void run() {
String incomingMsg;
try {
System.out.println("Starting socket thread...");
serverSocket = new ServerSocket(21111);
System.out
.println("ServerSocket created, waiting for incomming connections...");
Socket socket = serverSocket.accept();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
while (true) {
// System.out.println("Connection accepted, reading...");
while ((incomingMsg = in.readLine()) != null && socket.isConnected()) {
System.out.println("Message recieved: " + incomingMsg
+ ". Answering...");
// send a message
String outgoingMsg = "Message \"" + incomingMsg
+ "\" recieved on server."
+ System.getProperty("line.separator");
out.write(outgoingMsg);
out.flush();
System.out.println("Message sent: " + outgoingMsg);
}
if (socket.isConnected()) System.out.println("Socket still connected");
else System.out.println("Socket not connected");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
这是客户端代码。我正在使用 AsyncTask:
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private Button sendButton;
private EditText messageText;
private String messageToSend;
private ClientSender clientSender;
private Context context;
private Socket socket;
private static String SERVER_IP = "192.168.1.129";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageText = (EditText) findViewById(R.id.editTextMessage);
sendButton = (Button) findViewById(R.id.sendButton);
context = this.getApplicationContext();
clientSender = new ClientSender(context);
socket = null;
sendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
messageToSend = messageText.getText().toString()
+ System.getProperty("line.separator");
clientSender.execute(messageToSend);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class ClientSender extends AsyncTask<String, Void, Socket> {
private Socket socket;
private String answer;
private Context context;
private BufferedWriter out;
private BufferedReader in;
public ClientSender(Context context) {
this.context = context;
socket = null;
out = null;
in = null;
}
@Override
protected Socket doInBackground(String... params) {
try {
if (socket == null) {
socket = new Socket(SERVER_IP, 21111);
out = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
}
out.write(params[0]);
out.flush();
answer = in.readLine() + System.getProperty("line.separator");
return socket;
} catch (IOException e) {
e.printStackTrace();
}
return socket;
}
protected void onPostExecute(Socket socket) {
if (socket != null) {
Toast.makeText(context, answer, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Can't connect to server!",
Toast.LENGTH_LONG).show();
}
}
}
}
我不知道发生了什么,但我怀疑套接字连接可能有问题。