Import java.net.*;/** * inetaddress usage * Get local hostname, IP * Get inetaddress object via hostname, IP*/ Public classdemo{ Public Static voidMain (string[] args) throws Exception {inetaddress LocalHost=Inetaddress.getlocalhost (); System. out. println (Localhost.tostring ()); System. out. println (Localhost.gethostaddress ()); System. out. println (Localhost.gethostname ()); InetAddress ThinkPad= Inetaddress.getbyname ("reborn-pc"); System. out. println (Thinkpad.tostring ()); }}
Import java.net.*;/** * Simple UDP send receive*/classUdpsend { Public Static voidMain (string[] args) throws Exception {//1, create the UDP service. Through the Datagramsocket object. Datagramsocket (int port) can bind the ground portDatagramsocket ds =NewDatagramsocket (); //2. Identify the data and encapsulate it into a packet. Datagrampacket (byte[] buf, int length, inetaddress//address, int port) byte[] buf ="udp ge men lai le". GetBytes (); Datagrampacket DP=NewDatagrampacket (buf, Buf.length, Inetaddress.getbyname ("127.0.0.1"),10000); //3, through the socket service, the existing data packets sent out. Through the Send method. Ds.send (DP); //4, close the resource. Ds.close (); }}
classudprece { Public Static voidMain (string[] args) throws Exception {//1, create the UDP socket and set up the endpoint. Datagramsocket ds =NewDatagramsocket (10000); while(true) { //2. Define the packet. Used to store data. byte[] buf =New byte[1024x768]; Datagrampacket DP=NewDatagrampacket (buf, buf.length); //3, the received data is stored in the packet via the Receive method of the service. Ds.receive (DP);//blocking method. //4, the data is obtained through the method of the packet. Get the send-side portString IP =dp.getaddress (). gethostaddress (); String Data=NewString (Dp.getdata (),0, Dp.getlength ()); intPort =Dp.getport (); System. out. println (IP +"::"+ Data +"::"+port); } //5, close the resource//ds.close (); }}
/*UDP multithreading Implementation to write a chat program. There is part of the data collected, and the part that sends the data. These two parts need to be executed at the same time. Then you need to use multithreaded technology. A thread control to receive, a line programmed to send. Because the receive and send action is inconsistent, define two run methods. And the two methods are encapsulated into different classes. */import java.io.*; import java.net.*;classSend implements runnable{PrivateDatagramsocket ds; PublicSend (datagramsocket ds) { This. ds =ds; } Public voidrun () {Try{BufferedReader Bufr=NewBufferedReader (NewInputStreamReader (System.inch)); String Line=NULL; while((Line=bufr.readline ())! =NULL) { byte[] buf =line.getbytes (); Datagrampacket DP=NewDatagrampacket (Buf,buf.length,inetaddress.getbyname ("127.0.0.1"),10002); Ds.send (DP); if("886". Equals (line)) Break; } } Catch(Exception e) {Throw NewRuntimeException ("Send side failed"); } }}classRece implements runnable{PrivateDatagramsocket ds; Publicrece (datagramsocket ds) { This. ds =ds; } Public voidrun () {Try { while(true) { byte[] buf =New byte[1024x768]; Datagrampacket DP=NewDatagrampacket (buf,buf.length); Ds.receive (DP); String IP=dp.getaddress (). gethostaddress (); String Data=NewString (Dp.getdata (),0, Dp.getlength ()); if("886". Equals (data)) {System. out. println (ip+".... Leave the chat room"); Break; } System. out. println (ip+":"+data); } } Catch(Exception e) {Throw NewRuntimeException ("Receive side failed"); } }}classchatdemo{ Public Static voidMain (string[] args) throws Exception {Datagramsocket Sendsocket=NewDatagramsocket (); Datagramsocket Recesocket=NewDatagramsocket (10002); NewThread (NewSend (Sendsocket)). Start (); NewThread (Newrece (Recesocket)). Start (); }}
UDP for the Java network