Socket Communication for Android development-implement the server-side C/S architecture on the mobile phone, and implement androidsocket

Source: Internet
Author: User

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;import;import;import;import;import java.util.ArrayList;import;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 =!=-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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.