Socket Communication for Android development-implement the server-side C/S architecture on the mobile phone, and implement androidsocket
Every smartphone is a micro-computer. In school, the favorite thing is to find dozens of eight students to work together in a LAN to open several bureaus of dota. Suddenly, how can I use a mobile phone C as a game to play? I don't need to connect to wifi or open data. I have to choose one of my mobile phones as a host. Other mobile phones can connect to its hotspots, so I can interact with the game, happy ~~
In the actual implementation process, it is found that there are very few servers using mobile phones, and there are few ready-made solutions on the Internet. After some exploration, the socket communication function of the mobile phone itself as a server (or a client) is simply implemented. If you write it out, please give us some instructions and simply sort it out to facilitate future reuse.
The biggest problem we encounter may be the blocking of the socket on the server, because we need to keep the socket on the server in the listening status, in this way, the client's connection requests and messages sent by the client can be continuously accepted, and then broadcast to all client machines. Therefore, we cannot place serversocket in MainActivity. Because it will block app running, we can create a new service, define serversocket operations in service, and define a button in MainActivity, start the service by using the button to open the server. My service code is as follows:
import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;import android.app.Service;import android.content.Intent;import android.os.Bundle;import android.os.IBinder;import android.os.Message;public class ServerSocketService extends Service{private static ArrayList<Socket> socketList = new ArrayList<Socket>();private ServerSocket serverSocket = null;@Overridepublic IBinder onBind(Intent intent){return null;}@Overridepublic void onCreate(){super.onCreate();}@Overridepublic void onDestroy(){super.onDestroy();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId){try{new Thread(new ServerThread()).start();}catch(Exception e){e.printStackTrace();}return START_STICKY;}public class ServerThread implements Runnable{private Socket socket = null;public ServerThread(){}public void run(){try{serverSocket = new ServerSocket(4567);while(true){socket = serverSocket.accept();socketList.add(socket);new Thread(new HandleInputMessageThread(socket)).start();}}catch(IOException e){e.printStackTrace();}}}public class HandleInputMessageThread implements Runnable{private Socket socket = null;public HandleInputMessageThread(Socket sock){this.socket = sock;}public void run(){while(true){InputStream inputStream = null;try{inputStream = socket.getInputStream();byte data[] = new byte[1024*4];int i = 0;while((i = inputStream.read(data))!=-1){String buffer = null;buffer = new String(data,"gbk");buffer = buffer.trim() + "\n";OutputStream outputStream = null;for(Socket sock : socketList){outputStream = sock.getOutputStream();outputStream.write(buffer.getBytes("gbk"));outputStream.flush();}break;}}catch(IOException e){e.printStackTrace();}}}}}
The implementation of the client is relatively simple. The client code can connect to the server and send message data. It will not be posted here.
Hope to help people in need ~ Long journey, fly slowly