大佬教程收集整理的这篇文章主要介绍了使用Service或IntentService进行Android Client / Server Socket通信,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这就是我的服务器的样子:
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,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(); } } }
如何从服务器中取出服务?客户应该也是服务吗?
不要忘记在服务的onDestroy()中正确清理Threads.
另请注意,如果您希望应用程序继续接收来自其他客户端的消息,则需要确保将服务强制进入@L_874_16@(请参阅http://developer.android.com/reference/android/app/Service.html#startForeground(int,android.app.Notification).但是,这不是万无一失的,您的服务仍然可以被杀死.
这就是为什么人们倾向于使用托管在某个专用盒子上的服务器而不是每个单独的设备……
以上是大佬教程为你收集整理的使用Service或IntentService进行Android Client / Server Socket通信全部内容,希望文章能够帮你解决使用Service或IntentService进行Android Client / Server Socket通信所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。