Java Network Programming Summary __ algorithm

Source: Internet
Author: User
Tags fully qualified domain name
Summary of Java Network programming

Basic concepts of Network programming


1.OSI and TCP/IP system model


2.IP and Port

Solves the problem of positioning that the article mentions at the beginning.

IP can uniquely identify a single computer in the Internet, is the unique identification of each computer (identity card); Network programming is communication with a remote computer, so you have to be able to navigate to a remote computer; IP helps solve the problem; there may be many processes in a computer, specific to which process to communicate, This depends on the port to identify;

IP and Port can uniquely locate the process that requires communication. Here the IP represents the address, which differs from the IP protocol. In the OSI system or TCP/IP system, the IP protocol is located in the Internet layer to encapsulate the IP address into the packet.


3.TCP and UDP protocols

TCP is the abbreviation of Tranfer Control Protocol, is a connection-oriented protocol to ensure reliable transmission. Through the TCP protocol transmission, obtains is a sequential error-free data stream. A connection must be established between the pair of two sockets of the sender and receiver to communicate on the basis of the TCP protocol, and when one socket (usually the server socket) waits for the connection to be made, another socket can require a connection. Once these two sockets are connected, they can transmit data in both directions, and both can send or receive operations.


UDP is the user Datagram protocol abbreviation, is a connectionless protocol, each datagram is a separate information, including the full source address or destination address, it on the network on any possible path to the destination, so can reach the destination, The time to arrive at the destination and the correctness of the content are not guaranteed.


Comparison:

Udp:

Full address information is given in each datagram, so there is no need to establish a connection between the sender and the receiver.

UDP transmits data with a size limit, and each transmitted datagram must be limited to 64KB.

UDP is an unreliable protocol, and datagrams sent by the sender do not necessarily reach the receiver in the same order.

Tcp:

Connection-oriented protocols, which must be connected before the data is transmitted between sockets, so the connection time is required in TCP.

TCP transmits data size limits, and once the connection is established, both sockets can transmit large data in a uniform format.

TCP is a reliable protocol that ensures that the receiver is fully and correctly getting all the data sent by the sender.


Data hardwood:


Application:

TCP has great vitality in network communications, such as remote Connection (Telnet) and file Transfer (FTP) that require indefinite length of data to be transmitted reliably. But the reliable transmission is to pay the price, the test of the correctness of the data content must occupy the processing time of the computer and the bandwidth of the network, so the TCP transmission efficiency is not as high as UDP.


UDP is simple and requires less monitoring, so it is usually used for client/server applications in decentralized systems with high reliability on the LAN. For example, video conferencing system, does not require audio and video data is absolutely correct, as long as the consistency can be guaranteed, in this case, the obvious use of UDP is more reasonable.


4.Socket

A socket is a network-driven layer that provides an application programming interface and a mechanism. We can liken the Socket to a port. The application simply puts the goods on the dock, even if the goods are shipped. A port is also created for the receiving application, which only needs to wait for the goods to arrive at the dock and take the goods away.


The Socket is created in the application, which establishes a relationship with the driver through a binding mechanism, telling itself the corresponding IP and Port. Each data frame that is transmitted over a network must contain the sender's IP address and port number. After creating the socket, the application writes to the socket data, the socket to the driver to send data to the network, the computer from the network to receive a socket bound to the IP and Port-related data, the driver to the socket, the application can Read the received data from this Socket. This is how the network application is sent and received through the Socket.


Socket data Sending process:


Socket data Receive process:


5. Common Application Layer Protocol

Application layer protocol is to solve a kind of application problem, and the problem is often solved by the communication and collaboration between multiple application processes located in different hosts. The specific content of the application layer is to specify the protocol that the application process follows when communicating.


Java Network Programming Common classes


1.InteAddress class

The inetaddress in Java is a package that represents an IP address. IP addresses can be represented by byte arrays and strings, and inetaddress the IP address as an object, making it easier to manipulate and get its properties. InetAddress has no construction method, and can obtain its object through two static methods.


To obtain the corresponding InetAddress instance based on the host name
inetaddress IP = inetaddress.getbyname ("www.baidu.com");
Judge whether it is up to
SYSTEM.OUT.PRINTLN ("Baidu is up to:" + ip.isreachable (2000));
Gets the IP string for the InetAddress instance
System.out.println (Ip.gethostaddress ());
Gets the corresponding InetAddress instance based on the original IP address (byte array form)
InetAddress local = inetaddress.getbyaddress (new byte[]{127,0,0,1});
SYSTEM.OUT.PRINTLN ("The machine is up to:" + local.isreachable (5000));
Gets the fully qualified domain name for the inetaddress instance
System.out.println (Local.getcanonicalhostname ());


2.URL and URLConnection class

The URL in the network (uniform Resource Locator) is the abbreviation for the Uniform Resource Locator. That represents the address of a resource on the Internet. Through the URL we can access various network resources on the Internet, such as the most common www,ftp sites.


URLs can be thought of as "pointers" to Internet resources, where information about Internet resources is available through URLs, including information about the InputStream object obtaining the URL, and a connection to the remote object referenced by the URL urlconnection. The URLConnection object can send a resource that requests and reads URLs to the URL represented. Typically, creating a connection to a URL requires the following steps:

Creates a URL object and obtains the URLConnection object by calling the OpenConnection method;

Set URLConnection parameters and normal request properties;

Send a request to a remote resource;

The remote resource becomes available, and the program can access the header field of the remote resource and read the information returned by the remote resource through the input stream.


Here we need to focus on the third step: if you just send a get-way request, use the Connect method to establish a connection to a remote resource, and if you need to send a post-mode request, you need to get the output stream of the URLConnection object to send the request.


It is important to note here that since the parameter pass of the Get method is to explicitly append the parameter to the address, the parameter in the construction of the URL object should be the full URL address of the parameter, and after the URLConnection object is obtained, You can send the request directly by calling the Connect method. The Post method only requires the page URL when passing parameters, and the parameters are passed through the output stream. You also need to set the header field. Here are two ways to code:


1. A request to send a GET method to the specified URL
String urlname = URL + "?" + param;
URL realurl = new URL (urlname);
Opening and linking to URLs
URLConnection conn = Realurl.openconnection ();
To set common request properties
Conn.setrequestproperty ("Accept", "*/*");
Conn.setrequestproperty ("Connection", "keep-alive");
Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
Establish the actual connection
Conn.connect ();
2. A request to send a POST method to a specified URL
URL realurl = new URL (URL);
Opening and linking to URLs
URLConnection conn = Realurl.openconnection ();
To set common request properties
Conn.setrequestproperty ("Accept", "*/*");
Conn.setrequestproperty ("Connection", "keep-alive");
Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
Send POST request must be set as follows two lines
Conn.setdooutput (TRUE);
Conn.setdoinput (TRUE);
Gets the output stream corresponding to the URLConnection object
out = new PrintWriter (Conn.getoutputstream ());
Send Request parameters
Out.print (param);


3.URLDecoder and Urlencoder

These two classes can be used to convert strings of application/x-www-form-urlencoded MIME types to ordinary strings and convert ordinary strings to such special types of strings. The static method of the Urldecoder class, Decode () is used for decoding, and the static method of the Urlencoder class encode () is used for encoding. The specific use of the following methods:


Converts a application/x-www-form-urlencoded string into a normal string
String KeyWord = Urldecoder.decode ("%e6%9d%8e%e5%88%9a+j2ee", "UTF-8");
System.out.println (KeyWord);
Converts an ordinary string to a application/x-www-form-urlencoded string
String urlstr = Urlencoder.encode ("Ror Agile Development Best Guide", "GBK");
System.out.println (URLSTR);


4.Socket and ServerSocket class

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.