Network Programming (the java.net)
a network element
1, IP address: InetAddress
192.168.1.255 (192.168.1 subnet broadcast address)
2, port number
0--65535
0-1024
3, the transport protocol
UDP
data source and Objective and encapsulated into packets, without establishing a connection
packet size constraints in the 64K
due connectionless and unreliable protocol
without establishing a connection, fast
TCP
connection is established, data transmission channel is formed
be a large amount of data transmission in the connection
Linking is accomplished by three-way handshake is a reliable protocol
must establish a connection, less efficient
Two, UDP protocol instance
Communication network: sending end
. 1 public class UDPSendDemo { 2 . 3 / ** . 4 * @param args . 5 * @throws IOException . 6 * / . 7 public static void main (String [] args) throws IOException { . 8 / * . 9 * Create transmitting end idea UDP transmission: 10 * 1, establishing udp socket server 11 * 2, the transmitted data packets encapsulated into 12 * 3, through udp socket server to send the packet out 13 * 4, serving to close the socket 14 * / 15 // 1 , 16 DatagramSocket ds = new DatagramSocket(); 17 // 2、 18 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 19 String line=null; 20 while ((line=br.readLine())!=null) { 21 byte[] buff = line.getBytes(); 22 DatagramPacket dp = new DatagramPacket(buff, buff.length, InetAddress.getByName("172.28.188.168"), 10000); 23 ds.send(dp); 24 } 25 26 ds.close(); 27 28 } 29 30 }
Second, the communications network: receiving terminal
1 public class UDPReceDemo { 2 . 3 / ** . 4 * @param args . 5 * @throws IOException . 6 * / . 7 public static void main (String [] args) throws IOException { . 8 / * . 9 * UDP receiving end to establish ideas 10 * 1 establish udp socket service 11 * 2, create a package for storing data received. Convenient analytical method of data packet object data 12 stored in * 3, using the method of the socket and services receive the received data packet. 13 * 4, parsing data packets over the packet process 14 * 5, close the resource 15 * / 16 DatagramSocket DS = new new DatagramSocket (10000 ); . 17 the while ( to true ) { 18 is byte [] = buf new new byte [1024 ]; . 19 of DatagramPacket DP = new new of DatagramPacket (buf, to buf.length); 20 is // using a receiving method storing the data packet 21 is ds.receive (DP); // blocking 22 is 23 is // 4, the data packet object method, wherein the analysis data, such as address, port, content data 24 25 String IP = DP .getAddress () getHostAddress ();. 26 is int Port = dp.getPort(); 27 String str = new String(dp.getData(), 0, dp.getLength()); 28 System.out.println(ip + "::" + port + ":::" + str); 29 // ds.close(); 30 } 31 } 32 33 }
result:
Example two, multi-threaded
Thinking: multithreaded: transmitting data with a data receiving tasks and tasks to prepare two classes, respectively for transmission and reception
Send class
1 public class Send implements Runnable { 2 private DatagramSocket ds; 3 4 Send(DatagramSocket ds) { 5 this.ds = ds; 6 } 7 8 @Override 9 public void run() { 10 try { 11 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 String line = null; 13 while ((line = br.readLine()) != null) { 14 byte[] buff = line.getBytes(); 15 DatagramPacket dp = new DatagramPacket(buff, buff.length, InetAddress.getByName("172.28.188.168"), 16 10000); 17 ds.send(dp); 18 19 if("886".equals(line)) 20 break; 21 } 22 ds.close(); 23 } catch (IOException e) { 24 25 e.printStackTrace(); 26 } 27 } 28 29 }
Reception class
1 public class Rece implements Runnable { 2 private DatagramSocket ds; 3 4 Rece(DatagramSocket ds) { 5 this.ds = ds; 6 } 7 8 @Override 9 public void run() { 10 try { 11 while (true) { 12 byte[] buf = new byte[1024]; 13 DatagramPacket dp = newOf DatagramPacket (buf, to buf.length); 14 // using a receiving method to store data packets 15 16 ds.receive (DP); // blocking . 17 18 is // 4, the data packet object method, parses data, such as address, port, data content . 19 20 is String IP = dp.getAddress () getHostAddress ();. 21 is // int = dp.getPort port (); 22 is String STR = new new String (dp.getData (), 0 , dp.getLength ()); 23 System.out.println (ip + "::" + str); 24- IF ( "886" .equals (str)) 25 System.out.println (ip + "exit"); 26 } 27 } catch (IOException e) { 28 29 e.printStackTrace(); 30 } 31 } 32 }
The main function
1 public class ChatDemo { 2 3 /** 4 * @param args 5 * @throws IOException 6 */ 7 public static void main(String[] args) throws IOException { 8 DatagramSocket rece=new DatagramSocket(10000); 9 DatagramSocket send=new DatagramSocket(); 10 new Thread(new Rece(rece)).start(); 11 new Thread(new Send(send)).start(); 12 } 13 14 }
result: