我已经实现了在我的PC上运行的Java TCP服务器,它将接受从运行客户端的Android设备发送的传入消息,也已实现 . 服务器只是从Android设备接收消息并发回答案 . 一切正常,直到我尝试从Android设备发送 second 消息:然后,应用程序崩溃!
但我不知道为什么 . 我使用按钮发送以前写的消息 .
这是服务器线程代码(使用 run() 从main调用该线程):
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 {
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();
}
}
}
}
我不知道发生了什么,但我怀疑套接字连接可能有问题 .