Socket-based java communication programming

Java first appeared as a network programming language, which provided a high degree of support for the network, making communication between the client and the server a reality, and in network programming, the most used is Socket. Like everyone familiar with QQ, MSN uses Socket-related technology. Let us uncover the mystery of Socket.

Socket programming , network basics (refer to computer network)

For the computer network section, please refer to the relevant blog:

"TCP/IP Protocol Stack and OSI Reference Model Detailed Explanation"

Http://wangdy.blog.51cto.com/3845563/1588379

1. Communication between two computers requires the following three conditions:

IP address, protocol, port number

2. TCP/IP protocol:

It is currently the most widely used protocol in the world. It is a collection of multiple protocols at different levels based on TCP and IP. It also becomes a TCP/IP protocol suite or a TCP/IP protocol stack.

TCP: Transmission Control Protocol

IP: Internet Protocol Internet Protocol

3, TCP / IP five-layer model

Application layer: HTTP, FTP, SMTP, Telnet, etc.

Transport layer: TCP/IP

Network layer:

data link layer:

Physical layer: network cable, twisted pair, network card, etc.

4, IP address

In order to communicate between different computers in the network, each computer must have a unique identifier --- IP address.

32-bit binary

5, the port

Differentiate a different application of a host, the port number ranges from 0-65535, and 0-1023 bits are reserved for the system.

Such as: HTTP: 80 FTP: 21 Telnet: 23

The IP address + port number constitutes the so-called Socket, which is the termination point of the two-way communication link between programs running on the network, and is the basis of TCP and UDP.

6, Socket socket:

A uniquely identified IP address and port on the network are combined to form a uniquely identifiable identifier socket.

Socket principle mechanism:

Both ends of the communication have a Socket

Network communication is actually the communication between Sockets.

Data is transmitted through IO between two Sockets

7, network support in Java

For different levels of network communication, Java provides different APIs, which provide four types of network functions:

InetAddress: used to identify hardware resources on the network, mainly IP addresses.

URL: Uniform resource locator, which can directly read or write data on the network through the URL.

Sockets: Network communication Socket related classes implemented using the TCP protocol

Datagram: Use UDP protocol to store data in user datagrams and communicate over the network.

Second, InetAddress

The InetAddress class is used to identify hardware resources on the network and to identify Internet Protocol (IP) addresses.

There is no constructor for this class

Socket-based java communication programming

Third, the URL class

1. URL (Uniform Resource Locator) Uniform Resource Locator, which indicates the address of a resource on the Internet. Protocol Name: Resource Name

Socket-based java communication programming

2, use the URL to read web content

The input stream of the specified resource can be obtained through the openStream() method of the URL object, and the resource on the webpage can be read or accessed through the stream.

Socket-based java communication programming

Fourth, TCP programming

1. The TCP protocol is connection-oriented, reliable, and orderly, and sends data in a byte stream manner. The connection is established through a three-way handshake to form a channel for transmitting data, and a large amount of data is transmitted in the connection, and the efficiency is slightly low

2. Classes for implementing network communication based on TCP protocol in Java

Client's Socket class

Server-side ServerSocket class

Socket-based java communication programming

3, the steps of Socket communication

1 Create ServerSocket and Socket

2 Open the input/output stream connected to the Socket

3 Read/write operations on the Socket according to the protocol

4 close the input and output streams, close the Socket

4, the server side:

1 Create a ServerSocket object, bind the listening port

2 Listening for client requests via the accept() method

3 After the connection is established, the request information sent by the client is read through the input stream.

4 Send the hometown information to the client through the output stream.

5 Close related resources

Socket-based java communication programming

6, the application of multi-threaded communication between the server and the multi-client

1 Server side creates a ServerSocket, and cyclically calls accept() to wait for the client connection.

2 The client creates a socket and requests to connect to the server.

3 The server side accepts the hard-to-read segment request, creates a socket and establishes a dedicated line connection with the client.

4 The two sockets that establish the connection talk on a separate thread

5 The server continues to wait for a new connection

Socket-based java communication programming

Five, UDP programming

UDP protocol (User Datagram Protocol) is connectionless, unreliable, unordered, fast

When data transmission is performed, the data to be transmitted is first defined as a datagram (Datagram), the size is limited to 64k, the Socket (host address and port number) to which the data is to be reached is indicated in the datagram, and then the datagram is sent out.

DatagramPacket class: indicates data packet

DatagramSocket class: Class for end-to-end communication

1, server-side implementation steps

1 Create a DatagramSocket and specify the port number.

2 Create a DatagramPacket

3 Accept the data information sent by the client

4 read data

Socket-based java communication programming

2, the client implementation steps

1 Define sending information

2 Create a DatagramPacket containing the information to be sent

3 Create a DatagramSocket

4 send data

Socket-based java communication programming

Six, pay attention to the problem:

1, multi-threaded priority issues:

According to the actual experience, the priority is lowered appropriately, and the side may have low efficiency.

2. Whether to close the output stream and input stream:

For the same socket, if the output stream is closed, the socket associated with the output stream will also be closed, so you can usually close the socket without closing the stream.

3, using TCP communication to transfer objects, serialization part of IO

4, socket programming transfer file, IO stream part

9V Ac Dc Adapter

9V Ac Dc Adapter,Adaptor 9 Volt,Dc 9V Adapter,9V Ac Adapter

ShenZhen Yinghuiyuan Electronics Co.,Ltd , https://www.yhypoweradapter.com

This entry was posted in on