Based on the use of sockets
Server side:
Start a server-side socket first
ServerSocket svr = new ServerSocket (8989);
Start listening for requests
Socket s = svr.accept ();
Get input and output
DataInputStream dis = new DataInputStream (S.getinputstream ());
DataOutputStream dos = new DataOutputStream (S.getoutputstream ());
The interaction of the socket is done through the stream, which means that the byte stream is transmitted, so any file can be transmitted above. Whoever opens it, remember to close it.
The Datainputstream/dataoutputstream are packaged because we want them to read and write the basic data type Readint (), Writeint (), readUTF (), writeUTF (), and so on.
Client:
Initiate a socket connection
Socket s = new socket ("192.168.1.200", 8989);
Get input and output
DataInputStream dis = new DataInputStream (S.getinputstream ());
DataOutputStream dos = new DataOutputStream (S.getoutputstream ());
Then we can communicate with each other. Whoever opens it, remember to close it.
based on HTTP protocol
Typically, you send a request to an application server. The httpurlconnection is needed at this time.
Get HttpURLConnection First
Urlconn = new URL ("http://www.google.com"). OpenConnection ();
Set flag
Urlconn.setdooutput (TRUE); Urlconn.setdoinput (TRUE);
You need to set Dooutput to true in case of post
Urlconn.setrequestmethod ("POST");
Urlconn.setusecache (false);//setting whether to use caching
Set Content-type
Urlconn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
Get the output stream to make it easier to send information to the server.
DataOutputStream dos = new DataOutputStream (Urlconn.getoutputstream ());
Look in the stream inside write request parameters
Dos.writebytes ("Name=" +urlencoder.encode ("Chenmouren", "gb2312");
Dos.flush ();d os.close ()//close immediately after sending.
Get input stream, fetch data
Bufferreader reader = new BufferedReader (New InputStreamReader (Urlconn.getinputstream ()));
Reader.readline ()//use!=null to determine whether the end
Reader.close ();
Finish reading remember to close connection urlconn.disconnect ();