Socket Programming Basics:
Primary server-side programming and client programming.
Server-side programming steps:
1: Create server-side sockets and bind to a port (0-1023 is System reserved, preferably about 1024)
2: Socket set listening mode waiting for connection request
3: Communication After accepting the connection request
4: Return, wait to win a connection request
Client Programming Steps:
1: Create a client socket (specify server-side IP address and port number)
2: Connect (automatically connect when Android creates socket)
3: Communication with the server side
4: Close Socket
Android Socket Communication Principle Note:
1: The middle pipe connection is achieved through Inputstream/outputstream flow.
2: Once the pipeline is established to communicate
3: Closing the pipe means closing the socket
4: When duplicate pipes are created on the same socket, they are abnormal
5: The order is important in the communication process: The server side first gets the input stream and then outputs the input stream information to its individual clients
After the client establishes the connection, it writes the output stream before obtaining the input stream. Otherwise, there are eofexception anomalies.
Don't forget to add Internet permissions, don't forget to add Internet permissions, and don't forget to add Internet permissions
The writing of server-side code:
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import java.util.ArrayList;
public class Server {static ServerSocket aserversocket = NULL;//server Socet. DataInputStream adatainput = null;
Server input Stream that to//Receive MSG from client. DataOutputStream adataoutput = null;
Server output Stream that to static ArrayList list = new ArrayList ();
public static void Main (string[] args) {try {aserversocket = new ServerSocket (50003);//Listen 8888 port.
System.out.println ("Already listen 50003 port.");
catch (Exception e) {e.printstacktrace ();
int num = 0;
while (Num <) {Socket asessionsoket = null;
try {asessionsoket = aserversocket.accept ();
Mythread thread = new Server (). New Mythread (Asessionsoket);
Thread.Start ();
num = List.size (); catch (IOException E1) {//TODO auto-generated Catch block E1.printstacktrace ();
Class Mythread extends Thread {Socket asessionsoket = null;
Public mythread (socket socket) {asessionsoket = socket;
public void Run () {try {adatainput = new DataInputStream (Asessionsoket.getinputstream ());
Adataoutput = new DataOutputStream (Asessionsoket.getoutputstream ());
List.add (Adataoutput);
while (true) {String msg = ADATAINPUT.READUTF ();//Read MSG. if (!msg.equals ("Connect ...")) {System.out.println ("IP:" + asessionsoket.getinetaddress ());//IP.
SYSTEM.OUT.PRINTLN ("Receive msg:" + msg);
for (int i = 0; i < list.size (); i++) {DataOutputStream output = (dataoutputstream) list.get (i);
Output.writeutf (msg + "----" + list.size ());
if (Msg.equals ("End")) the break;
} adataoutput.writeutf ("");
The catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
Finally {try {adatainput.close (); if (adataoutput!= null) adataOutput.close ();
List.remove (Adataoutput);
Asessionsoket.close (); catch (Exception E2) {