Java network communication is very simple, the server side through the ServerSocket to establish the monitoring, the client through the socket connected to the specified server, the communication between the two sides can communicate through the IO stream.
An IP address is used to uniquely identify a communication entity in a network. The port is used to indicate which communication program the data is given to handle.
Well-known ports from 0 to 1023, tightly bound to some specific services. To register ports from 1024 to 49151, applications should typically use this range of ports. Dynamic ports from 49152 to 65535 are dynamic ports used by applications, and applications generally do not actively use these ports.
Packagecom.ivy.net;Importjava.net.InetAddress; Public classInetaddressdemo { Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method Stubinetaddress IP= Inetaddress.getbyname ("www.google.com"); System.out.println ("Crazyit isreachable:" + ip.isreachable (2000)); System.out.println (Ip.gethostaddress ()); InetAddress Local= Inetaddress.getbyaddress (New byte[]{127,0,0,1}); System.out.println ("Local isreachable:" + local.isreachable (2000)); System.out.println (Local.gethostaddress ()); }}
When a string containing non-Western European characters is included in the URL address, the system converts these non-western strings into application/x-www-form-urlencoded mime strings. This requires Urldecoder and Urlencoder classes. Ordinary strings that contain only Western European characters do not need to be converted.
Packagecom.ivy.net;Importjava.io.UnsupportedEncodingException;ImportJava.net.URLDecoder;ImportJava.net.URLEncoder; Public classUrldecoderdemo { Public Static voidMain (string[] args)throwsunsupportedencodingexception {//TODO auto-generated Method StubString KeyWord= Urldecoder.decode ("%b7%e8%bf%f1java", "GBK"); System.out.println (KeyWord); String urlstring= Urlencoder.encode ("Crazy Java Handout", "GBK"); System.out.println (urlstring); }}
Typically, URLs can be made up of protocol names/hosts/ports and resources, in the following format
Protocol://host:port/resourcename
eg. http://www.crazyit.org/index.php
Using socket communication
Packagecom.ivy.net;ImportJava.io.PrintStream;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classServer { Public Static voidMain (string[] args)throwsexception{//TODO auto-generated Method StubServerSocket SS=NewServerSocket (30000); while(true) {Socket s=ss.accept (); PrintStream PS=NewPrintStream (S.getoutputstream ()); Ps.println ("Hello"); Ps.close (); S.close (); } }}
Packagecom.ivy.net;ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.net.Socket; Public classClient { Public Static voidMain (string[] args)throwsexception{//TODO auto-generated Method StubSocket Socket=NewSocket ("127.0.0.1", 30000); BufferedReader BR=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); String Line=Br.readline (); System.out.println ("Data from server:" +Line ); Br.close (); Socket.close (); }}
"Crazy Java Handout" (35)----Network programming