gpt4 book ai didi

使用 Service 或 IntentService 的 Android Client/Server Socket 通信

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:03:51 25 4
gpt4 key购买 nike

到目前为止,我能够在一个安卓设备(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();
}
}
}

如何从服务器创建服务?客户端也应该是服务吗?

最佳答案

使用服务但忘记 AsyncTask。让你的服务启动你的通用线程(https://developer.android.com/reference/java/lang/Thread.html)并设置你的套接字+监听器。该服务甚至还可以处理消息的发送(通过以下链接中涵盖的众多选项之一)。

不要忘记在服务的 onDestroy() 中正确清理线程。

另请注意,如果您希望该应用继续接收来自其他客户端的消息,您需要确保该服务被强制进入前台(参见 http://developer.android.com/reference/android/app/Service.html#startForeground(int ,android.app.Notification)。然而,这并非万无一失,您的服务仍可能被终止。

这就是为什么人们倾向于使用托管在某个专用盒子上的服务器,而不是每个单独的设备...

关于使用 Service 或 IntentService 的 Android Client/Server Socket 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15662783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com