Javase Basic Review strategy "ten"

Source: Internet
Author: User

According to the plan of this article for everyone to summarize the Java network programming, what is called Network programming? Network programming! = website Programming, for this point we must pay attention to, many children have once thought so. Since we are talking about network programming, let's start with the basics of networking, what is a computer network? The distribution of computers in different regions and specialized external equipment with communication lines into a large-scale, powerful network system, so that many computers can easily transfer information to each other, share hard disk, software, data information and other resources.

What is the network communication protocol: in the computer network to realize the communication must have some convention namely the communication protocol, to the rate, the transmission code, the code structure, the Transmission control step, the error control and so on standard.

Network communication interface: In order to be able to communicate between the two nodes, communication tools (i.e. interfaces) must be established between them so that information can be exchanged between each other. The interface consists of two types: the hardware device-the realization of information transmission between nodes. Software Device-a contractual agreement that provides for communication between the parties.

1, Network hierarchical Reference Model:

  

2. TCP protocol and UDP protocol:

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

UDP provides a way for an application to send an encapsulated raw IP datagram, and it does not need to establish a connection when it is sent. is an unreliable connection protocol.

3. TCP Programming:

A, server side:

 Public classTestseriver {/**Server-side *@paramargs*/     Public Static voidMain (string[] args) {Try {            //One of our machines has 65,526 ports, when we set the port, try to choose the port after 124, because the port before 124 has been set by a specific program, for example: 80: Browser address portServerSocket SS =NewServerSocket (888);//Here's 888: The port number that we set for the convenience of the client connectionSocket s = ss.accept ();//to receive the client's connection, note that this method is blocking, that is, if there is no client connection, the program will stay in this locationSYSTEM.OUT.PRINTLN ("A client connect!"); } Catch(IOException e) {e.printstacktrace (); }    }}

A, client:

 Public classTestClient {/**Client *@paramargs*/     Public Static voidMain (string[] args) {Try{Socket s=NewSocket ("127.0.0.1", 888);//Here's a point, "127.0.0.1": Make our host number, 888: is the port number we need to connect}Catch(unknownhostexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }}

B, Server side:

 Public classTestSeriver1 {/**Server-side *@paramargs*/     Public Static voidMain (string[] args) {Try{ServerSocket ss=NewServerSocket (999);  while(true) {Socket s=ss.accept (); System.out.println ("A Client connect!"); InputStream is=S.getinputstream (); DataInputStream Dis=NewDataInputStream (IS); System.out.println (Dis.readutf ());//here The readUTF () method is also blocking, in order to see this effect I added a pause in the client input place, has been labeled            }        } Catch(IOException e) {e.printstacktrace (); }    }}

B, Client:

 Public classTestClient1 {/**Client *@paramargs*/     Public Static voidMain (string[] args) {Try{Socket s=NewSocket ("127.0.0.1", 999); OutputStream OS=S.getoutputstream (); DataOutputStream dos=Newdataoutputstream (OS); Thread.Sleep (3000);//set a pause for three seconds, about this I'm on the thread's blog that explainsDos.writeutf ("Hello server!"); } Catch(unknownhostexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

4, UDP Programming Example:

A, server side:

 Public classTestudpserver {/**Server-side code *@paramargs * for UDP and TCP port numbers, first we can set arbitrary, but must be guaranteed within 65536 * UDP and TCP port number is not associated, that is, UDP and TCP each have 65,536 ports*/     Public Static voidMain (string[] args)throwsexception{byte[] buf =New byte[1024];//set the character array that holds the dataDatagrampacket DP =NewDatagrampacket (buf, buf.length); Datagramsocket DS=NewDatagramsocket (5888);//set the port number         while(true) {ds.receive (DP);//receive request data from clientSystem.out.println (NewString (buf, 0, Dp.getlength ())); //new String (buf, 0, Dp.getlength ()): A string constructor that converts a byte array into a string class of data        }    }}

A, client:

 Public classtestudpclient {/**Client Code Instance *@paramargs * socketaddress: Address on the network, including not only IP, but also the port number and so on * INETADDRESS:IP address of the super * INETSOCKETADDRESS:IP address + port number */     Public Static voidMain (string[] args)throwsexception{byte[] buf =New byte[1024]; BUF= (NewString ("Hello")). GetBytes ();//converting a string type of data to a character arrayDatagrampacket DP =NewDatagrampacket (buf, Buf.length,NewInetsocketaddress ("127.0.0.1", 5888)); Datagramsocket DS=NewDatagramsocket (9999);//set the port number for the client to send dataDs.send (DP);//sending data to the serverDs.close (); }}

B, Server side:

 Public classTestUDPServer1 { Public Static voidMain (string[] args)throwsexception{byte[] buf =New byte[1024];//set the character array that holds the dataDatagrampacket DP =NewDatagrampacket (buf, buf.length); Datagramsocket DS=NewDatagramsocket (5888);//set the port number         while(true) {ds.receive (DP);//receive request data from clientBytearrayinputstream Bais =NewBytearrayinputstream (BUF); DataInputStream Dis=NewDataInputStream (Bais);        System.out.println (Dis.readlong ()); }    }}

B, Client:

 Public classTestUDPClient1 { Public Static voidMain (string[] args)throwsexception{Long L= 10000L; Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); DataOutputStream dos=NewDataOutputStream (BAOs);//DataOutputStream: You can write a long type of data directlyDos.writelong (L); byte[] buf =Baos.tobytearray (); Datagrampacket DP=NewDatagrampacket (buf, Buf.length,NewInetsocketaddress ("127.0.0.1", 5888)); Datagramsocket DS=NewDatagramsocket (9999);//set the port number for the client to send dataDs.send (DP);//sending data to the serverDs.close (); }}

Well, here's a summary of the network programming in Java.

To today's summary of the basic knowledge of javase for everyone to analyze the completion, there must be some problems, but also hope that we have a lot of guidance, and then the next day, I also share JSP, JAVA SSH framework and Android basic knowledge Summary, I have always believed that only the foundation to fight, To go farther, let's refuel together. If you have any good ideas, but also look at the guidance.

Javase Basic Review strategy "ten"

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.