Javase basic review strategy 10 and javase Introduction 10

Source: Internet
Author: User

Javase basic review strategy 10 and javase Introduction 10

According to the plan, this article summarizes JAVA network programming. What is network programming? Network Programming! = Website programming. Everyone must pay attention to this point. Many children once think so. Now that we are talking about network programming, Let's first look at the basic knowledge of the network. What is a computer network? Interconnect computers distributed in different regions with dedicated external devices using communication lines into a large and functional network system, so that many computers can easily transmit information to each other, share hardware, software, data information, and other resources.

What is network communication protocol: there must be some conventions for communication in computer networks, that is, communication protocols, and standards for speed, transmission code, code structure, transmission control steps, error control, and so on.

Network Communication Interface: to allow communication between two nodes, a communication tool (that is, an interface) must be established between them to enable information exchange between them. There are two types of interfaces: hardware device-information transmission between nodes. Software device-Agreement on communications between the parties.

1. Reference Model for network layering:

  

2. TCP and UDP protocols:

TCP is designed to provide reliable, end-to-end byte stream communication protocol in an unreliable network environment. It is a connection-oriented protocol.

UDP provides an application with a method to send encapsulated original IP datagram, and no connection is required during sending. Is an unreliable connection protocol.

3. TCP programming:

A. Server:

Public class TestSeriver {/** server side * @ param args */public static void main (String [] args) {try {// There are 65526 ports on our machine, when we set the port, try to select the port after 124, because the port before 124 has been set by a specific program, such as: 80: browser address port ServerSocket ss = new ServerSocket (888); // here 888: the port number Socket s = ss set to facilitate client connection. accept (); // used to receive client connections. Note that this method is blocking. That is to say, if there is no client connection, the program will stay in this position. out. println ("A client connect! ");} Catch (IOException e) {e. printStackTrace ();}}}

A. Client:

Public class TestClient {/** client * @ param args */public static void main (String [] args) {try {Socket s = new Socket ("127.0.0.1", 888 ); // here, "127.0.0.1": Make our host number, 888: the port number we need to connect} catch (UnknownHostException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}

B. server side:

Public class TestSeriver1 {/** server side * @ param args */public static void main (String [] args) {try {ServerSocket ss = new ServerSocket (999 ); while (true) {Socket s = ss. accept (); System. out. println ("A client connect! "); InputStream is = s. getInputStream (); DataInputStream dis = new DataInputStream (is); System. out. println (dis. readUTF (); // The readUTF () method here is also blocking. To see this effect, I added a pause at the place entered by the client, already noted} catch (IOException e) {e. printStackTrace ();}}}

B. Client:

Public class TestClient1 {/** client * @ param args */public static void main (String [] args) {try {Socket s = new Socket ("127.0.0.1", 999 ); outputStream OS = s. getOutputStream (); DataOutputStream dos = new DataOutputStream (OS); Thread. sleep (3000); // set the pause for three seconds. I have a description of dos in the thread blog. writeUTF ("Hello Server! ");} Catch (UnknownHostException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}

4. UDP programming example:

A. Server:

Public class TestUDPServer {/** server code * @ param args * for UDP and TCP port numbers, we can first set them as needed, but ensure that the range is less than 65536 * The UDP and TCP port numbers are not associated, that is, UDP and TCP have 65536 ports */public static void main (String [] args) throws Exception {byte [] buf = new byte [1024]; // you can specify the character array used to store data. The parameter rampacket dp = new parameter rampacket (buf, buf. length); DatagramSocket ds = new DatagramSocket (5888); // set the port number while (true) {ds. receive (dp); // receives the request data from the client System. out. println (new String (buf, 0, dp. getLength (); // new String (buf, 0, dp. getLength (): String constructor, which converts a byte array to a String type data }}}

A. Client:

Public class TestUDPClient {/** client code instance * @ param args * SocketAddress: address on the network, including not only IP addresses, but also port numbers. * InetAddress: Super IP Address * InetSocketAddress: IP address + port */public static void main (String [] args) throws Exception {byte [] buf = new byte [1024]; buf = (new String ("Hello ")). getBytes (); // converts String-type data to the character array DatagramPacket dp = new DatagramPacket (buf, buf. length, new InetSocketAddress ("127.0.0.1", 5888); DatagramSocket ds = new DatagramSocket (9999); // set the port number ds for sending data from the client. send (dp); // send data ds to the server. close ();}}

B. server side:

Public class TestUDPServer1 {public static void main (String [] args) throws Exception {byte [] buf = new byte [1024]; // set the character array of the data to be stored. DatagramPacket dp = new DatagramPacket (buf, buf. length); DatagramSocket ds = new DatagramSocket (5888); // set the port number while (true) {ds. receive (dp); // receives the request data sent from the client ByteArrayInputStream bais = new ByteArrayInputStream (buf); DataInputStream dis = new DataInputStream (bais); System. out. println (dis. readLong ());}}}

B. Client:

Public class TestUDPClient1 {public static void main (String [] args) throws Exception {Long L = 10000L; ByteArrayOutputStream baos = new ByteArrayOutputStream (); DataOutputStream dos = new DataOutputStream (baos ); // DataOutputStream: You can directly write a Long data dos. writeLong (L); byte [] buf = baos. toByteArray (); DatagramPacket dp = new DatagramPacket (buf, buf. length, new InetSocketAddress ("127.0.0.1", 5888); DatagramSocket ds = new DatagramSocket (9999); // set the port number ds for sending data from the client. send (dp); // send data ds to the server. close ();}}

Now, we will summarize network programming in JAVA.

By now, the summary of the basic knowledge of JAVASE has been analyzed for everyone. There must be some problems. I hope you can give me more instructions. In the next few days, I also share my summary on the basic knowledge of JSP, java ssh framework, and Android. I have always believed that only by laying a solid foundation can we go further. Let's cheer up. If you have any good ideas, I hope to give you some advice.

 

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.