What is network programming

network programming

A .IP

1.  Different definition of the network nodes on IPv4:. 32 bit IPV6: 128 bit

Non-registered IP address (internal use): 192.168.0.0 ~ 192.168.255.255

2. A special IP: 127.0.0.1 local IP; 1ocalhost native domain; the DNS domain name resolver

3. java.net class InetAddress This class represents an Internet Protocol (IP) address. static InetAddress getLocalHost () Returns the local host. static InetAddress getByName (String host) to determine the IP address of a host, given the host's name

Two Port

1. Port: distinguishing software, defines the port 2 bytes 0 to 65535

Port conflict can not be under the same agreement

Port custom bigger the better , 8000, within the reserved port number 8000

Special port number : 80: http; 8080: tomcat ; 1521: Oracle port number 3306: mysql port number; INetSocketAddress class implements IP socket address (IP address + port number).

Three .URL       1. the URL of: Uniform Resource Locator | device

Internet three cornerstones : html http url protocol: http; domain: www.baidu.com; port: default 80 80 Resources:? Index.html name = zhangsan & age = 123 # a; http: //www.baidu.com: 80 / index.html? name = zhangsan & age = 123 # a

On behalf of a URL URL Uniform Resource Locator, which is pointing to "resource" pointer.

2. Several Methods :

String getFile () Gets the file name of this URL. String getHost () Gets the host name of this URL (if applicable). String getPath () Gets the path part of this URL. int getPort () Gets the port number of this URL; String getProtocol () Gets the protocol name of this URL String getQuery () Gets the query part of this URL.

String getRef () to get this anchor the URL (also called "reference").

3. reptiles principle

(1) Example 1;

 

(2) Example 2:

 

Four Protocol

1. udp protocol: Specification contract

(1) udp: write a non-connection-oriented, unsafe, high efficiency, simple protocol, overhead is small, the size is limited not more than 60k

How to achieve udp Socket Programming: the sender and the receiver are equal

Such DatagramSocket represents sockets for sending and receiving datagram packets

 DatagramSocket(int port)

DatagramPacket This class represents a datagram packet.

 UDP basic process: the transmitting end

Creating the sender indicating that the sender of the port

Prepare the data , into a byte array

Packing the data specified in the received address and port number of the terminal  DatagramPacket

send data

shut down

Example :

 

(2) UDP basic process: a receiving end

Construction of the receiving end , the receiving end designated port; prepared byte array , an interface for storing the receipt; byte array package; blocking receiver; processing transactions; Close

Examples are as follows :

 

2. tcp

(1) TCP: connection-oriented call, security, low efficiency, large overhead 3-way handshake: a dial 2. Returning: 3. The response data is transmitted, based on the IO data piercing

How the application layer and the transport layer to communicate ? By Socket socket

Oriented Socket programming, corresponds Socket outlet, is the communication interface between the two layers, different protocols, different implementations Socket

(2) tcp basic process: Client

Create a client  ip Socket specify the server and port Socket (String host, int port) ; io operation; Close

Example :

 

tcp basic process: the server

Construction of the server  ServerSocket designated port ServerSocket (int port)

Blocking monitor  Socket accept (); a client is a socket IO operation; Close

Example :

Guess you like

Origin www.cnblogs.com/ruanjianwei/p/11790292.html