首先,我知道这是一个非常普通的问题,我已经浏览了数十个出版物和帖子。他们的解决方案基本相同。但经过几个小时的尝试仍然不适合我。
问题在于发送/接收短信 - 我没有收到服务器的任何回复。它不是HTTP服务器所以我使用Socket。我想可能有几个原因:
现在代码。
初始化/打开套接字连接:
private class OpenSocketTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
// Creating InetAddress object from ipNumber passed via constructor from IpGetter class.
InetAddress serverAddress = InetAddress.getByName(HOST); //also tried instead: socket = new Socket(HOST, PORT);
// Create a new Socket instance and connect to host
socket = new Socket(serverAddress, PORT);
Log.i(TAG, "Socket created: " + HOST + ":" + PORT);
// Create PrintWriter object for sending messages to server.
// out = new PrintWriter(socket.getOutputStream(), true); //also tried this
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
//Create BufferedReader object for receiving messages from server.
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.d(TAG, "In/Out created");
} catch (UnknownHostException e) {
Log.e(TAG, "Don't know about host: " + HOST + ":" +PORT);
connected = false;
Log.e(TAG, e.getMessage());
} catch (IOException e) {
Log.e(TAG, "Couldn't get I/O for the connection to: " + HOST + ":" +PORT);
connected = false;
Log.e(TAG, e.getMessage());
}
connected = true;
return null;
}
@Override
protected void onPostExecute(Void result) {
if (connected) {
Log.d(TAG, "Connected to TCP server...");
}
super.onPostExecute(result);
}
}
发送和接收消息:
public class ServerSendMessageTask extends AsyncTask<Void, Void, Void> {
private String message;
public ServerSendMessageTask(String message){
this.message = message;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
if (connected) {
Log.d(TAG, "Sending command to server: " + message);
try {
out.println(message);
out.flush();
/**
* Alternative for SEND message
* Both didn't work.
* */
// //Send the message to the server
// OutputStream os = socket.getOutputStream();
// OutputStreamWriter osw = new OutputStreamWriter(os);
// BufferedWriter bw = new BufferedWriter(osw);
// bw.write(message);
// bw.flush();
Log.d(TAG, "Message was sent: " + message);
//Listen for the incoming messages while connected = true
while (connected) {
String incomingMessage = in.readLine();
if (incomingMessage != null) {
//never reach here :(
Log.d(TAG, "Received from server string: " + incomingMessage);
}
}
/**
* This is the alternative way to RECEIVE message
* Both didn't work for me
* */
// //Get the return message from the server
// InputStream is = socket.getInputStream();
// InputStreamReader isr = new InputStreamReader(is);
// BufferedReader br = new BufferedReader(isr);
// String response = br.readLine();
// Log.d(TAG, "Received from server string: " + response);
} catch (UnknownHostException e) {
Log.e(TAG, "Don't know about host: " + HOST + ":" +PORT);
Log.e(TAG, e.getMessage());
} catch (IOException e) {
Log.e(TAG, "Couldn't get I/O for the connection to: " + HOST + ":" +PORT);
Log.e(TAG, e.getMessage());
}
}
return null;
}
@Override
protected void onPostExecute(Void res) {
super.onPostExecute(res);
}
}
我在活动onStart()
中以new OpenSocketTask().execute();
打开套接字,并在按钮点击时发送消息,代码为new ServerSendMessageTask(message).execute();
。因此,线程不会“重叠”。
另一件事可能是:
我正在尝试将Ping程序包(请参阅第6页here)发送到服务器。只是看它的工作。它看起来像#P#\r\n
。如果一切正常,我应该收到这个字符串:#AP#\r\n
。
我对Java中的格式并不十分清楚,因此我尝试发送不同的邮件组合:#P#\r\n
,#P#\\r\\n
,#P#
。没有差异。
你能否指出我的错误?