Java TCP/IP socket programming (1)

Source: Internet
Author: User
1.1 computer networks, protocols, and packets

A computer network is composed of a group of machines connected through communication channels. We turn these machines into hosts and routers.

1. A host is the computer that runs applications. These applications include Web browsers, instant messaging agents, and file sharing programs.

2. A Router transmits or forwards information from one communication channel to another.

3. Communication channels refer to the means to transmit byte sequences from one host to another.

4. Information refers to the byte sequence created and interpreted by the computer.

A protocol is an agreement between processes that communicate with each other. It defines the exchange methods and meanings of packets. A set of protocols specify the packet structure and how to parse the packets.

The Protocol is also called the protocol family. The TCP/IP protocol family includes the IP protocol (Internet Protocol), transmission control protocol (Transport Control Protocol), and UDP protocol (User Data Protocol ).

It is a good way to layer protocols.

Application: application; socket: socket; host: host; Channel: communication channel; Ethernet: Ethernet; router: router; Network Layer: network layer; Transport Layer: transport layer.

In the TCP/IP protocol family, the underlying layer consists of basic communication channels, such as Ethernet or modem dial-up connections. These channels are used by the network layer, and the network layer completes the packet transmission to their destination address (that is, the router function ). The only protocol at the network layer in the TCP/IP protocol family is the IP protocol, which makes a series of communication channels and routers between two hosts look like a single channel between the host and the host.

The IP protocol provides a datagram service: each group of packets is independently processed and distributed by the network. To implement this function, each IP packet must contain a field that stores its destination address. The IP protocol is only a "best-effort" Protocol: it tries to distribute packets in each group. However, packet loss occasionally occurs during network transmission, disrupt the packet sequence or send packets repeatedly.

The transport layer is called the transport layer above the IP protocol layer ). It provides two Optional Protocols: TCP and UDP. These two protocols are based on the services provided by the IP layer, but according to the different requirements of the Application Protocol (Application Protocols, they use different methods for transmission. Both TCP and UDP have a common function, namely addressing. In retrospect, the IP protocol only distributes grouped packets to different hosts. Obviously, it also requires more fine-grained addressing to send packets to the specified applications in the host, because the network can be used by multiple applications on the same host. The port numbers address used by TCP and UDP is used to distinguish different applications on the same host. TCP and UDP are also known as end-to-end transport protocols because they transmit data from one application to another, the IP protocol only transmits data from one host to another.

The TCP protocol can detect and restore possible packet loss, duplication, and other errors from the host provided by the IP layer to the channel of the host. TCP provides a reliable byte-stream channel, so that the application does not need to deal with the above problems. TCP is a connection-oriented protocol. before using it for communication, you must first establish a TCP connection between two applications, this involves handshake messages. On the other hand, UDP does not try to fix Errors generated by the IP layer. It simply expands the datagram service of the IP protocol "Best Effort" so that it can work between applications, instead of working between hosts. Therefore, UDP-enabled applications must be prepared for packet loss and disordered order.

1.2 address

1. There are two pieces of information in TCP/IP to specify a program: Port and Internet address.

2. Internet addresses are composed of binary numbers, which are IPv4 (32-bit) and IPv6 (128-bit.

3. The port number range is 1--65535.

4. Loop address: a virtual device that directly returns the packets sent to the sender. The IPv4 retrieval address is 127.0.0.1, And the IPv6 retrieval address is 0.0.0.0.0.0.1.

5. Another special-purpose reserved address in an IPv4 address includes the "private-purpose" addresses. These include all IPv4 addresses starting with 10 or 192.168, and the first number is 172, and the second number is between 16 and 31. (There is no such address in IPv6) this type of address was initially used
Designed for use, it is not part of the public internet. These types of addresses are usually used in residential or small-sized offices. These locations are connected to the Internet through NAT (network address translation.

6. Related types of addresses include local links, or "automatically configured" addresses. IPv4,
This type of address starts with 169.254. in IPv6, the first 16 addresses starting with fe8 are local links.
Address. This type of address can only be used for communication between hosts connected to the same network. The Router does not forward the information of this type of address.

7. multicast addresses. A common IP address (sometimes called "unicast") is only unique
A single destination address is associated, while a multicast address may be associated with any number of destination addresses. The multicast address in IPv4 is in the point-to-point format, and the first number is between 224 and 239. In IPv6, multicast addresses start with ff.

1.3 about name

First, it should be clear that the use of host names instead of addresses is for convenience, which is independent from the basic services provided by TCP/IP. You can also write and use TCP/IP applications without using their names. When you use a name to locate a communication terminal, the system will do some additional work to resolve the name into an address. There are two reasons to prove that this additional step is worthwhile: first, people are more likely to remember the name than the point-based form (or the hexadecimal digit string in IPv6); second, the name provides an indirect layer so that IP address changes are invisible to users.

The name resolution service can obtain information from a variety of information sources. The two main sources of information are Domain Name System (DNS) and local configuration database. DNS is a distributed database that maps domain names to real Internet addresses and other information. DNS Protocol [allows hosts connected to the Internet to obtain information from the DNS database through TCP or UDP protocol. A local configuration database is usually a mechanism related to a specific operating system to map local names to Internet addresses.

1.4 client and server 1.5 socket

A socket is an abstraction layer through which an application sends and receives data, just as an application opens a file handle and reads and writes data to a stable storage. A socket allows applications to be added to the network and communicate with other applications in the same network. The information written by applications on one computer to the socket can be read by another application on the other computer, and vice versa.

Different types of sockets are associated with different underlying protocol families and different protocol stacks in the same protocol family. Currently, the main socket types in the TCP/IP protocol family are streaming Sockets (Sockets sockets) and datagram Sockets (datemedisockets ). Streaming socket uses TCP as its peer protocol (using the IP protocol at the underlying layer) and provides a reliable byte stream service. A TCP/IP stream socket represents one end of the TCP connection. The datagram socket uses the UDP protocol (the underlying layer also uses the IP protocol) to provide a "best-effort" (best-effort) datagram service, through which the application can send up to 65500 bytes of personal information. A TCP/IP socket is uniquely identified by an Internet address, an end-to-end protocol (TCP or UDP), and a port number.

Applications: applications; TCP sockets: TCP socket; TCP ports: TCP port; socket references: Socket reference; UDP sockets: UDP socket; sockets bound to ports: the socket is bound to the port; UDP ports: UDP port.

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.