到目前为止,我已经成功在一台安卓设备上启动了一个服务器(使用wifi热点/共享网络),并让另一台安卓设备作为客户端连接到该服务器,并发送消息。服务器再回复这些消息。我意识到需要一种方法来使服务器保持对客户端的监听,即使聊天应用程序没有运行。客户端应该能够发送消息,而服务器应该接收到这些消息。我应该使用Service还是IntentService来实现这一点?我无法从AsyncTask和Service中继承...如何实现这一点?一些示例代码会很棒。
如何将服务器转化为服务?客户端也应该成为服务吗?
以下是我的服务器外观:
public class Server extends AsyncTask<Integer, Void, Socket> {
private ServerSocket serverSocket;
private TextView textView;
private String incomingMsg;
private String outgoingMsg;
public Server(TextView textView) {
this.textView = textView;
}
public void closeServer() {
try {
serverSocket.close();
} catch (IOException e) {
Log.d("Server", "Closung the server caused a problem");
e.printStackTrace();
}
}
@Override
protected Socket doInBackground(Integer... params) {
try {
serverSocket = new ServerSocket(params[0]);
//accept connections
Socket socket = serverSocket.accept();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
incomingMsg = in.readLine() + System.getProperty("line.separator");
//send a message
outgoingMsg = "You are connected to the Server" + System.getProperty("line.separator");
out.write(outgoingMsg);
out.flush();
return socket;
} catch (InterruptedIOException e) {
//if timeout occurs
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// finally {
// if (serverSocket != null) {
// try {
// serverSocket.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
return null;
}
protected void onPostExecute(Socket socket) {
if(socket != null) {
try {
Log.i("Server", "Server received: " + incomingMsg);
textView.setText("Server received: " + incomingMsg + "\n");
textView.append("Server sent: " + outgoingMsg + "\n");
Log.i("Server", "Server sent: " + outgoingMsg);
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Log.d("Server", "Can't communicate with the client!");
}
}
}
这是我的客户端:
public class Client extends AsyncTask<Integer, Void, Socket> {
private WifiManager wifi;
private Context context;
private String outMsg;
private String inMsg;
public Client(Context context, WifiManager wifiManager) {
this.context = context;
this.wifi = wifiManager;
}
@Override
protected Socket doInBackground(Integer... params) {
try {
String gateway = intToIp(wifi.getDhcpInfo().gateway);
Socket socket = new Socket(gateway, params[0]);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String ipAdress = intToIp(wifi.getConnectionInfo().getIpAddress());
outMsg = ", Client " + ipAdress +" is connecting!" + System.getProperty("line.separator");
out.write(outMsg);
out.flush();
//accept server response
inMsg = in.readLine() + System.getProperty("line.separator");
return socket;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String intToIp(int addr) {
return ((addr & 0xFF) + "." +
((addr >>>= 8) & 0xFF) + "." +
((addr >>>= 8) & 0xFF) + "." +
((addr >>>= 8) & 0xFF));
}
protected void onPostExecute(Socket socket) {
if(socket != null) {
Log.i("Client", "Client sent: " + outMsg);
Toast.makeText(context, "\nClient sent: " + outMsg + "\n", Toast.LENGTH_LONG).show();
Log.i("Client", "Client received: " + inMsg);
Toast.makeText(context, "Client received: " + inMsg + "\n", Toast.LENGTH_LONG).show();
} else {
Log.d("Client", "Can't connect to server!");
Toast.makeText(context, "Can't connect to server!", Toast.LENGTH_LONG).show();
}
}
}
如何将服务器转化为服务?客户端也应该成为服务吗?