In Android Network Communication, sockets are usually used for data communication between the number of devices and Http is used for network data requests.
1. Socket)
Anyone with Java development experience or. NET development experience should have a more or less understanding of Socket. Common TCP or UDP protocols are actually implemented based on Socket.
A Socket is used to describe a process or application in a device on the network. A Socket consists of an IP address and a port number. IP addresses are used to locate devices and port numbers are used to locate applications or processes, such as common HTTP protocols running on port 80. The common format of Socket is 192.168.1.1: 1234.
How does an application communicate with other devices on the network through a Socket? Generally, the Socket communication includes two parts: the listening Server and the actively requesting Client. The Server listens to the port in the Socket until there is a request. When the Client sends a connection request to the port, the Server responds and returns a Socket object, in the future, you can use this Socket for data exchange between the Server and the Client.
2. Use Socket for data exchange in Android
ServerSocket
Server), you need to use the ServerSocket object, this object will automatically listen to the port number passed in its constructor, and after receiving the connection request, use ServerSocket. the accept () method returns a connected Socket object. This method does not need to use the Start method as in. NET. It will automatically listen.
Socket
Whether you create a Client or perform other data exchange operations, you must use the Socket class. During initialization, the Socket class needs to access the IP address and port number of the Server and return a Socket object connected to the Server. If the connection fails, an exception is returned. Same as ServerSocket, it also automatically makes connection requests.
After the above two steps, the Server and Client can be connected, but only the connection is useless. Data Exchange is our purpose, in this case, the OutputStream class and InputStream class in the IO stream are used.
OutputStream-- Writable stream
When the application needs to write data to the stream, you can use the data stream returned by the Socket. getOutputStream () method.
InputStream-- Readable stream
To retrieve data from a stream, you can use the data stream returned by Socket. getInputStream () to perform operations.
Let's take a look at the complete code.
- View Code
- Package LiB. Demo;
- Import java. io. BufferedReader;
- Import java. io. BufferedWriter;
- Import java. io. IOException;
- Import java. io. InputStreamReader;
- Import java. io. OutputStreamWriter;
- Import java.net. ServerSocket;
- Import java.net. Socket;
- Public class SocketHelper {
- Private static ServerSocket serverSocket = null;
- Private static Socket client = null;
- Private final static int port = 9048;
- Private static BufferedReader br = null;
- Private static BufferedWriter bw = null;
- /**
- * Create a SocketServer object to create a server
- * @ Throws IOException
- */
- Public static void CreateServer () throws IOException
- {
- ServerSocket = new ServerSocket (port, 10 );
- System. out. println ("start listening ...");
- }
- /**
- * Create a Socket object to connect to the SocketServer object
- * @ Param dstName the IP address of the Server Object
- * @ Return
- * @ Throws IOException
- */
- Public static Socket CreateClient (String dstName) throws IOException
- {
- Socket socket = new Socket (dstName, port );
- // Socket sockets = new Socket ("192.168.8.12", port );
- Return socket;
- }
- /**
- * Returns a Socket object that has been connected to the server.
- * @ Throws IOException
- */
- Public static void GetClinetSocket () throws IOException
- {
- Client = serverSocket. accept ();
- System. out. println ("get a connected client ");
- }
- /**
- * Send data to the Stream Obtained by the socket object
- * @ Param socket
- * @ Param msg
- * @ Throws IOException
- */
- Public static void SendMsg (Socket socket, String msg) throws IOException
- {
- Bw = new BufferedWriter (new OutputStreamWriter (socket. getOutputStream ()));
- Bw. write (msg );
- Bw. flush ();
- Bw. close ();
- }
- /**
- * Obtain data in the socket object stream
- * @ Param socket
- * @ Param msg
- * @ Return
- * @ Throws IOException
- */
- Public static String ReceiveMsg (Socket socket, String msg) throws IOException
- {
- Br = new BufferedReader (new InputStreamReader (socket. getInputStream ()));
- String receiveMsg = "Receive msg:" + br. readLine ();
- Br. close ();
- Return receiveMsg;
- }
- /**
- * Release the socket object
- * @ Throws IOException
- */
- Public static void Close () throws IOException
- {
- If (client! = Null)
- {
- Client. close ();
- }
- If (serverSocket! = Null)
- {
- ServerSocket. close ();
- }
- }
- }
Next page will introduce youHTTP CommunicationContent