Java-based network programming and java Network Programming

Source: Internet
Author: User
Tags connection reset

Java-based network programming and java Network Programming

IP address

The IP address is a network-layer communication address expressed by 32-bit or 128-bit unsigned numbers. It is a low-level protocol. Both TCP and UDP are built on it. In Java, InetAddress is used to represent the IP address. It has two subclasses: Inet4Address and Inet6Address, which represent IPV4 and IPV6 respectively.

The getByName () method obtains the InetAddress object through the host name. If there are multiple hosts, use getAllByName () to return the InetAddress object array.

Socket

Socket is a mechanism that provides network services. Both Ends of communication have sockets, and both ends communicate through sockets. InetSocketAddress encapsulates IP + port number.

 

TCP/UDP protocol features

UDP protocol

The UDP protocol encapsulates the data into a datagram, which is within 64 KB and does not need to be connected. It is fast and unreliable. Java uses the DatagramSocket class to describe the socket used to send and receive data packets, and uses DatagramPacket to describe UDP data packets and describe the non-connection package delivery service.

Data Transmission

  • Create a UDPsocket service and specify the sending port: DatagramSocket ds = new DatagramSocket (localport );
  • Encapsulate byte data in data packets: DatagramPacket dp = new DatagramPacket (buf, buf. length, ia, receiveport)
  • Send through the socket sending function: ds. send (dp)
  • Close resources: ds. close ()

Receive data

TCP protocol

TCP needs to establish a connection, transmit a large amount of data (do not need to be encapsulated as a datagram) in the connection, and complete the connection through three handshakes, which is a reliable protocol, but the efficiency is low. Java uses Socket to describe the socket service on the client, and SocketSever to describe the socket service on the server.

Client

Server

When initializing a ServerSocket object, you can set the backlog parameter to specify the maximum number of clients that can be connected to the server.

URL

A URL is a uniform resource identifier, which can be used to construct a URL object based on the specified format.

  • GetHost ()
  • GetFile () Get the file name
  • GetPath ()
  • GetPort () is used to obtain the port number. If no port is specified in the URL,-1 is returned;
  • GetProtocal () Get the protocol name
  • GetQuery () GET request section
  • OpenConnection () returns the connection object of a URL.
  • OpenStream () opens a URL Connection and returns an InputStream used to read from the connection.

 

URLConnection class

This class is an abstract class and has two subclasses: HttpURLConnection and JarURLConnection. This class encapsulates socket objects at the transport layer and can directly read socket stream objects.

  • GetInputStream () returns the read Stream Object of the socket, which is used to read data returned from the connection.
  • GetContent () obtains the URL Connection content.

Java Network Programming Basics

Java basics must be met.
Network Programming does not have much to do with it. It requires a higher level of thread and concurrency.
For some simple applications, if you have a good foundation, read the books for two weeks and contact us. JAVA provides a set of APIS for TCP and UDP protocols, and I/O is required.
If the research is deep, it will be complicated. The implementation of various protocols and underlying system operations are very complicated. I also don't quite understand them. Come on step by step.
JAVA programming this book has knowledge about network programming, as well as exercises and examples.

Java basic network programming

Take a look at the document to solve the problem.
InputStream
Java.net. Socket. getInputStream ()
Throws IOException

Returns an input stream for this socket. If this socket has an associated channel then the resulting input stream
Delegates all of its operations to the channel. If the channel is in
Non-blocking mode then the input stream's read operations will throw
Java. nio. channels. IllegalBlockingModeException. Under abnormal conditions the underlying connection may be broken by
Remote host or the network software (for example a connection reset in the case
Of TCP connections). When a broken connection is detected by the network
Software the following applies to the returned input stream :-

The network software may discard bytes that are buffered by the socket. Bytes
That aren't discarded by the network software can be read using read.

If there are no bytes buffered on the socket, or all buffered bytes have been
Consumed by read,
Then all subsequent callto read
Will throw an IOException.

If there are no bytes buffered on the socket, and the socket has not been
Closed using close,
Then available
Will return 0.

Closing the returned InputStream
Will close the associated socket.

In the last row, disabling the returned input stream will close the associated socket.

Related Article

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.