INETADDRESS,UDP, TCP

Source: Internet
Author: User

inetaddress,udp, TCP

Package Hanshi.net;import java.net.*;p ublic class Count {public static void main (string[] args) throws Exception {InetAddr  ESS ia = Inetaddress.getlocalhost (); Gets the local print (Ia.gethostaddress ());p rint (Ia.gethostname ()), inetaddress iad = inetaddress.getbyname ("www.baidu.com" ); Obtain information by name, either IP name or host name print (Iad.gethostaddress ());p rint (Iad.gethostname ());} static public void print (Object obj) {System.out.println (obj);}} UDP send-side package Hanshi.net;import java.io.bufferedreader;import java.io.inputstreamreader;import java.net.*;p ublic Class Count {public static void main (string[] args) throws Exception {datagramsocket ds = new Datagramsocket (110); BufferedReader BUFD = new BufferedReader (new InputStreamReader (system.in)); String str = null;while ((str = bufd.readline ())!=null) {if ("over". Equals (str)) {break;} byte[] bt = str.getbytes ();D atagrampacket dp = new Datagrampacket (Bt,bt.length,inetaddress.getbyname ("192.168.109.119 "),;d S.send (DP);} Ds.close ();} static public void print (Object obj) {SystEm.out.println (obj);}} UDP receive End package Hanshi.net;import Java.net.datagrampacket;import Java.net.datagramsocket;public class Count2 {public static void Main (string[] args) throws Exception {datagramsocket ds = new Datagramsocket (1000);//receiver must define a port while (true)   {byte[] buf = new byte[1024];D atagrampacket dg = new Datagrampacket (buf, Buf.length);d s.receive (DG);  Here is the blocking method, and the data will remain in the Wait state print (Dg.getaddress (). Gethostaddress () + "* * *" +dg.getaddress (). GetHostName ());   Use the packet to get the address to operate print (new String (Dg.getdata (), 0,dg.getlength ()));   Gets the data in the data stream, the length of the given data print (Dg.getport ()); Get the port on the send side, if the sender does not specify a send port, here will get to a system randomly allocated port}//Accept the port can be not closed, like the server}static public void print (Object obj ) {System.out.println (obj);}} Tcppackage Hanshi.net;import Java.io.outputstream;import java.net.*;//If the establishment of the Scoke is not passed parameters, then after the establishment is not connected, But you can connect via connect ... This one is built with a scoket flow, which is the network flow ... Once the flow is established, you can get the input stream and the output stream ... There is no streaming object on the server side, and the server is using accept to get the flow object of the client. The server can not shut itself down, but it can close the stream object that was fetched from the client ... /* The end of the service end after the client is also due to, after the meaning of S.close () is at the end of adding-one to do the end of the flag, so on the server read it is-1, think that the end of the return null, so jump out of the loop, end the client ... Simplified code: PrintWriter can accept both character streams and byte streams ... *///theoretically in the Scokert will throw an exception, if the connection is not directly processed ah ... public class Count {public static void main (string[] args) throws Exception {inetaddress ia = inetaddress.getlocalhost (); S Ocket SK = new Socket (ia.gethostaddress (), OutputStream OS = Sk.getoutputstream (); Os.write ("chenruibing").  GetBytes ());//os.close (); This one can not be closed, when the socket is closed, the flow attached to the above will automatically close Sk.close ();} static public void print (Object obj) {System.out.println (obj);}} Package Hanshi.net;import Java.io.inputstream;import Java.net.serversocket;import Java.net.socket;public class  Count2 {public static void main (string[] args) throws Exception {ServerSocket ss = new ServerSocket (1000);   Listen to the port socket s = ss.accept (); This is also the blocking method print (S.getinetaddress (). Gethostaddress () + "Denglu"); InputStream is = S.getinputstream (); byte[] bt = new Byte[1024];int len = Is.read (BT);p rint (New String (Bt,0,len)); S.close (); Ss.close ();} static public void print (OBJECT obj) {System.out.println (obj);}} 

TCP

sockets are the endpoints of communication between two machines ...
There is no streaming object on the server side, and the server is using accept to get the flow object of the client.
The server can not shut itself down, but it can close the stream object that was fetched from the client ...
after the compilation is complete, start the client after the server is started ...

Class Anli{public static void Main (string[] args) throws Exception{while (true) {socket s = new socket ("192.168.109.119", 12 345); OutputStream OS = S.getoutputstream ();//os.write ("Ni Hao". getBytes ()); BufferedReader br = new BufferedReader (new InputStreamReader (system.in));  String line = Br.readline (), Os.write (Line.getbytes ()), InputStream in = S.getinputstream (); byte[] bt = new Byte[1024];int Len = In.read (BT); System.out.println (New String (Bt,0,len));//br.close ();} S.close ();}} Class Rec {public static void main (string[] args) throws Exception{serversocket ss = new ServerSocket (12345), while (true) {S Ocket s = ss.accept (); String IP = s.getinetaddress (). gethostaddress ();//system.out.println (ip+ ".... connect"); InputStream in = S.getinputstream (); byte[] bt = new Byte[1024];int len = In.read (BT); System.out.println (New String (Bt,0,len)); OutputStream os = S.getoutputstream (); BufferedReader br = new BufferedReader (new InputStreamReader (system.in)); String line = Br.readline (); Os.write (line.getbytes()); S.close ();//br.close ();}} 
TCP about

/* Simplified code: PrintWriter can accept either character stream or byte stream ... */class anli{public static void Main (string[] args) throws Exception{while (true) {socket s = new socket ("192.168.109.119", 10086); BufferedReader br = new BufferedReader (new InputStreamReader (system.in)); BufferedReader bfr = new BufferedReader (New InputStreamReader (S.getinputstream ()));//bufferedwriter BFW = new BufferedWriter (New OutputStreamWriter (S.getoutputstream ()));                  PrintWriter pw = new PrintWriter (S.getoutputstream (), true); Benefits of using the PrintWriter string line = Null;while ((line = Br.readline ())!=null) {if (' over '. Equals (line) ') {break;} Bfw.write (line),//bfw.newline (),//bfw.flush ();p w.println (line); A function with the preceding multiple codes string str = Bfr.readline (); System.out.println ("Server:::" +str);}   Br.close ();//input stream to close ... These two can not be turned off, otherwise it has been an error s.close ();}} Class Rec{public static void Main (string[] args) throws Exception{serversocket ss = new ServerSocket (10086); while (true) {S Ocket s = ss.accept (); BufferedReader br = new BufferedReader (new InputStreamReader (s. getInputStream ()));//bufferedwriter bw = new BufferedWriter (New OutputStreamWriter (S.getoutputstream ()));  PrintWriter pw = new PrintWriter (S.getoutputstream (), true); Be sure to add true, otherwise the result is that data cannot be passed out ... String line = null;while (line = Br.readline ())!=null) {pw.println (Line.touppercase ());//bw.write (Line.touppercase () );//bw.newline ();//bw.flush ();} S.close (); Ss.close ();}}}



INETADDRESS,UDP, TCP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.