Java Network Programming (i)---basic knowledge and conceptual understanding

Source: Internet
Author: User

Java provides users with a very comprehensive network function:

1. Get various resources (URLs) on the network

2. Establishing connections and communications with the server (ServerSocket and sockets)

3. No connection passing local data (Datagramsocket)

All of these related features are defined in the java.net package, under the detailed description below:

First: URL: Through the expression of the URL network resources to determine the location of the data in the network, using the relevant methods provided in the URL object, directly read into the network data, the local data transfer to the other side of the network.

The following example uses a URL object to obtain a local program's configuration file and print it.

Second. Socket: Two program communication connection on the network. More commonly used. is achieved through the use of flow patterns, an interface has 2 streams, an input stream, an output stream; When a process sends data to another process, it only needs to write the data to the output stream of the corresponding interface, while the other process reads the data in the interface's input stream.

A. ServerSocket: To write a TCP Network Service program, first use the Java.net.ServerSocket class to create a server socket

A. Construction method:

ServerSocket (int port): Create a server socket bound to a specific port

ServerSocket (int port, int backlog): Creates a server socket and binds it to the specified local port number, using the specified backlog (the number of waiting clients to keep the connection request when the server is busy).

ServerSocket (int port, int backlog, inetaddress bindaddr): Creates a server with the specified port, the listening backlog, and the local IP address to bind to.

B. Socket: Client to establish a connection to the server, a socket object must be created first

A. Common construction methods

Socket (String host, int port): Creates a stream socket and connects it to the specified port number on the specified host.

Socket (inetaddress address, int port): Creates a stream socket and connects it to the specified port number for the specified IP address.

The server-side program calls the Accept () method in the ServerSocket class to wait for a connection request from the client, and once accept () receives a client connection request, the method returns a socket object that establishes a leased line connection to the client. Do not use the program to create this socket object. The connected two sockets are data exchange in IO Stream, Java provides getinputstream () in the socket class to return the socket input stream object, Getoutputstream () returns the socket's output stream object.

Third: Datagramsocket, is the most low-level, non-connected, datagram-based way of working communication. As a result, it has the advantage of small resource consumption and fast processing speed, so audio, video and normal data are usually transmitted with more UDP, because they will not have much effect on receiving results even if they occasionally lose one or two packets. For example, the chat with ICQ and OICQ is the use of the UDP protocol.

A. Datagramsocket class: Creating a socket instance that receives and sends UDP

Datagramsocket (): Creates an instance. Typically used for client-side programming, it does not have a specific listening port, just using a temporary one.

Datagramsocket (int port): Creates an instance and secures a message that listens on port ports.

Datagramsocket (int port, inetaddress localaddr): This is a very useful builder, when a machine has more than one IP address, the instance created by it only receives messages from LOCALADDR.

Note: When creating an instance of the Datagramsocket class, if the port is already in use, a SocketException exception is thrown and the program terminates illegally, and the exception should be aware of the catch. Common methods are as follows:

Receive (Datagrampacket D): Receives data message to D. The Receive method produces a "blocking".

Send (Datagrampacket D): Send paper D to destination.

Setsotimeout (int timeout): Sets the time-out period in milliseconds.

Close (): Datagramsocket off. When an application exits, it usually releases the resource voluntarily, shutting down the socket, but may cause the resource not to be reclaimed due to an unexpected exit. Therefore, you should actively use this method to close the socket when the program completes, or to close the socket after catching the exception thrown. "Blocking" is a professional noun that produces an internal loop that causes the program to pause in this place until a condition is triggered.

B. Datagrampacket: For processing messages, the byte array, the destination address, the destination port, and other data are packaged into a message or the message is disassembled into a byte array.

Datagrampacket (byte[] buf, int length, inetaddress addr, int port): From the BUF array, take long data to create the packet object, the destination is the addr address, port ports.

Datagrampacket (byte[] buf, int offset, int length, inetaddress address, int port): From the BUF array, take the length data from the offset start to create the packet object , the destination is the addr address, port.

Datagrampacket (byte[] buf, int offset, int length): Load data in the packet from offset, length long, into the BUF array.

Datagrampacket (byte[] buf, int length): The length of data in the packet is loaded into the BUF array.

Common methods:

GetData (): It obtains a byte array encoding of the message from the instance.

Java Network Programming (i)---basic knowledge and conceptual understanding

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.