The Java self-study road-day20

Source: Internet
Author: User

JAVA20

Network Programming Network Communication Protocol

L computers in a computer network can transmit data to each other.

L Transfer Data There is a certain rule is the network protocol

l Provision of data transfer format rate steps

L must follow rules to transfer data

L There are many kinds, the most extensive is TCP/IP and UDP

L Learn TCP/IP mainly

Network model of TCP/IP protocol

L Application Layer

protocol for the application

L Transport Layer

Enable network programs to communicate UDP and TCP

L Network Layer

TCP/IP Core layer

L Link Layer

Optical fiber, network cable Drive protocol

IP address and port number

L IP Address

    1. Unique identification of the computer
    2. To be able to specify the transfer data and send the data
    3. Widespread use of IPv4
    4. Represented in a computer with a four-byte binary number
    5. For ease of identification, each byte is represented by a decimal number. Separating 0-255
    6. More and more IP address requirements may appear saturated
    7. There's a IPv6.
    8. expressed in 16 bytes
    9. Is IPv4 's 8*10 28 times to 2 of 128 times a square
    10. Solve the problem of insufficient resources
    11. Native loopback Address

127.0.0.1 on behalf of the native address

L Port number

    1. IP address can be connected to a computer
    2. To access a program in your computer
    3. Need port number
    4. Assigned by the operating system
    5. Binary number 0-65535 with two bytes made up of 16 bits
    6. 0-1024 not available because it is already occupied by well-known programs or network services
    7. For example, browser port number 80
InetAddress class

L represents an IP address in the Internet

L Java.net.InetAddress

L can not instantiate an object

L static method gets

    1. Static inetaddress Getlocalhost ()
    2. The return value is a InetAddress object
    3. After the output is the IP address and host name

L Other static methods

    1. String gethostaddress () returns the host address
    2. String GetHostName () returns the host name

L Get non-native IP objects

    1. Static InetAddress Getbyname (String hostnmae)
    2. Delivery host name Get IP address object

UDP protocol

L is one of two important advanced protocols in the Transport layer

L Non-connected communication protocol

L No logical connection at both ends of data transfer

L The sender does not confirm whether the receiving end is present or not, and does not feedback whether it receives data

L Consumption of resources small communication efficiency is often used for audio and video transmission of ordinary data

L such as video conference space message

L loss of one or two packets will not have a significant impact on receiving results

L For example, computer video synchronization is just a bit of a card and it won't make much difference.

L Data size within 64KB

TCP protocol

L connection-oriented is the communication protocol

L provides reliable, error-free data transfer between two computers

L must clear the client and server

L Need "three handshake" per connection

L Client sends connection request service side feedback allows connection client feedback connection successful

• Slow connection but high security

L is widely used

L download file must use TCP

L Two Computers

L

UDP communication packets and sending objects

L Two Computers

L One send one receive

L The sender needs to know the receiving method IP and port number

L data needs to be packaged to send

Datagrampacket

L packing data equivalent to container of wharf

L encapsulate the address and port of the data receiving end

L Receive data-bound port number and sender-side consistency

Datagramsocker

L Send/Receive packaged data equivalent to the role of the terminal

Implementation steps

L Send End Create Datagrampacket object wrapper data and set the address and port of the receiving end

    1. Constructor method passed in parameter

L send a packet to the sending side to create the Datagramsocker object

    1. Call the Send method to pass in the Datagrampacket object for delivery

L send-side close resource Send Object Datagramsocker

l Example

L receive End Object binding port number for creating Datagrampacket

L Receive-side create byte array plus receive data sent

L The receiver creates an incoming array of Datagramsocker objects and arrays of lengths

L Call the Receive method to receive the data incoming parameter Datagrampacket object

L Unpacking

The IP address sent

Number of bytes Received

Sender's port number

L Close Resources

l Example

No end after running because receive has a thread waiting to detect the sending of UDP if the data is not sent it will always be detected

Split Package

But the length of the array is too large to cause waste

So call Datagrampacket's GetLength method to return the received data length

Unpacking IP Address

Call Datagrampacket's getaddress to get the IP object to the sending end

The IP address and hostname can be obtained by this object

Unpacking send port port number

Method of calling Datagrampacket Getport

TCP Communication

L have clear client and server

L Service End Class ServerSocket

L Client class Scoket

L Client actively connects to server

L Server pre-boot wait for client connection

L Client Pull Server

L Server agrees to connect

L The client and the server form a pathway. One of the objects is set up.

L This object is Io stream object byte stream

• Data exchange is possible

L Client Output service-side input service-side output client input

Scoket class

L This class implements a socket socket for a client that is the endpoint of two machine communication

L Java.net.Socket

L Construction Method

    1. Socket (Stringhost,int Port)
    2. Delivery server IP and port number
    3. The construction method fails to throw an exception if it runs with a connection to the server
    4. OutputStream Getouputstream () returns the output stream of the socket

Output the data to the server

    1. InputStream getInputStream () returns the input stream of a socket

Input data, read data from the server

    1. Example

Serverscoket

L This class implements a server-side socket

L Construction Method

Serverscoket (int port) passing the port number required by the client

The server needs to know who is connected to the server and therefore needs to get the server's socket object

L Get method

Accept () returns the client's socket object

l Example

Exchange data between client and server

L Client

L Service Side

Stream objects in TCP

Shutdownoutput

L read a byte array in the network stream is not read-1

L if the loop reads, the loop will die.

L This is the way to use it.

L DISABLE the output stream for this socket

L If this method is called, an exception is thrown

L Read thrown exception will return-1

L stopped the loop.

Java self-learning path-day20

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.