Reprinted from Concurrent Programming network –ifeve.com
Java provides a very easy to use network API, call these APIs we can easily through the establishment of TCP/IP or UDP sockets, between the network to communicate with each other, where TCP is more common than UDP
Java TCP Network Fundamentals
Typically, the client opens a TCP/IP connection to the server side, and then the client begins to communicate with the server, and when the communication ends, the client closes the connection, as shown in the following procedure:
Socket class and serversocket class in Java
The Java socket class is used when the client wants to open a TCP/IP connection to the server. The socket class only needs to be told the IP address and TCP port of the connection, and the rest is implemented in Java.
public void Scokettest () throws Unknownhostexception, ioexception{
//create socketsocket so = new Socket ("192.168.200. //socket send data, to send data through the socket, we need to get the socket output stream (outputstream) outputstream out= so.getoutputstream (); O Ut.write ("Nihao". GetBytes ());/** * Want to send data over the network to the server side, be sure not to forget to call the Flush () method. * The underlying TCP/IP implementation of the operating system will first put the data into a larger data cache block, and the size of the cache block is the TCP/IP packet size of the * call Flush () method only writes the data to the operating system cache, does not guarantee that the data will be sent immediately */out.flush ( ); Out.close ();//socket reads the data, reads the data from the socket, we need to get the socket input stream (InputStream) InputStream in=so.getinputstream (); In.read ();/** * Reads data from the input stream of the socket and does not read the file, always calls the read () method until 1 is returned * because for the socket, the input stream of the socket will return-1 * If the connection is closed by the server But the fact that the server doesn't keep shutting down, assuming we want to send multiple requests through a connection, it's foolish to close the connection in this case. */in.close ();//close the socket, and when the socket is finished we must close the socket and disconnect the connection to the server. So.close ();}
if we want to open a listening service to listen to a client connection to some specified TCP port, you need to use the Java ServerSocket class. When the client is listening through a socket connection to the server-side ServerSocket, the server specifies a socket for the connection, and the communication between the client and the server becomes the socket-to-socket communication.
public void ServerSocket () throws ioexception{
Create the ServerSocket class to listen on the 9000 port serversocket serversocket = new ServerSocket (9000);//Listen for the requested connection, The Serversocket.accept () method is required to obtain the requested connection. The method returns a socket class that has all the attributes of the normal Java socket class socket so = Serversocket.accept ();//Close client socketso.close ();//Close the server sockets, To close the service, you need to turn off the ServerSocket connection. Serversocket.close ();}
The java.net package contains two interesting classes: a URL class and a URLConnection class. These two classes can be used to create a client-to-Web server (HTTP server) connection
public void Urltest () throws ioexception {URL url = new URL ("http://www.baidu.com"); URLConnection connection =url.openconnection (); InputStream in= connection.getinputstream (); int data = In.read (); while (Data!=-1) {System.out.println (char) data);d ATA = In.read ();} In.close ();/** * By default URLConnection sends an HTTP GET request to the Web server. * If you want to send an HTTP POST request, to call the Urlconnection.setdooutput (True) method * Once you have called setdooutput (true), You can open URLConnection OutputStream */connection.setdooutput (true); OutputStream out= Connection.getoutputstream ();// TODO}
URL class to open a local file system file
private void Urlreadfiletest () throws Exception {/** * URL is also called a Uniform Resource Locator. If your code doesn't care whether the file comes from the network or the local file system, the URL class is another way to open the file * This and the only difference between accessing a file on a Web server over HTTP is the URL: "file:/c:/data/test.txt" */url URL = new URL ("File:/c:/data/test.txt"); URLConnection urlconnection = Url.openconnection (); InputStream input = Urlconnection.getinputstream (); int data = Input.read (); while (data! = 1) {System.out.print (char) data);d ATA = Input.read ();} Input.close ();}
Java Web Tutorials