Socket, also known as sockets, is one of the basic technologies for computer network communication. Today, most web-based software, such as browsers, instant messaging tools and even P2P downloads, are implemented based on Socket. This article will introduce Socket programming based on TCP/IP and how to write a client/server program.
Pre-dinner dessert
Unix's input and output (IO) system follows operating templates such as Open-Read-Write-Close. Before a user process performs IO operations, it needs to call Open to specify and obtain permissions for the file or device to be operated to read or write. Once the IO operation object is opened, the user process can perform one or more read or write operations on the object. Read operation is used to read data from IO operation objects and pass the data to the user process. Write operation is used to pass (write) data in user processes to IO operation objects. After all Read and Write operations are completed, the user process needs to call Close to notify the system that it completes its use of the IO object.
When Unix started to support InterProcess Communication (IPC), the IPC interface was designed to be similar to the file IO operation interface. In Unix, a process will have a set of IO descriptors that can be read and written. The IO descriptor can be a file, device, or a communication channel (socket socket). A file descriptor consists of three parts: creating (opening socket), reading and writing data (accepting and sending to socket) and destroying (closing socket).
In Unix systems, the BSD-like version of the IPC interface is implemented as a layer above the TCP and UDP protocols. The destination of the message is represented by the socket address. A socket address is a communication identifier composed of a network address and a port number.
Inter-process communication operations require a pair of sockets. Inter-process communication is accomplished by data transmission between one socket in one process and another socket in another process. When a message is executed and sent, the message is queued in the socket on the sending end until the lower-level network protocol sends the messages out. When the message reaches the socket on the receiving end, it will also be in a queue until the process on the receiving end receives the message.
TCP and UDP communication
Regarding socket programming, we have two communication protocols to choose from. One is datagram communication, the other is stream communication.
Datagram communication
Datagram communication protocol is what we often call UDP (User Data Protocol). UDP is a connectionless protocol, which means that every time we send datagrams, we need to send the socket descriptor of the native machine and the socket descriptor of the receiver at the same time. Therefore, we need to send additional data every time we communicate.
Stream communication
The stream communication protocol, also known as TCP (Transfer Control Protocol) is also called TCP (Transfer Control Protocol). Unlike UDP, TCP is a connection-based protocol. Before using stream communication, we must establish a connection between the communication pair of sockets. One of the sockets serves as the server to listen for connection requests. The other one makes connection requests as a client. Once the two sockets have established a connection, they can transfer data in one-way or two-way.
After reading this, we have some questions about whether we use UDP or TCP for socket programming. The choice of socket programming based on which protocol is based on your specific application scenario depends on your specific client-server program. Below we briefly analyze the difference between TCP and UDP protocols, which may help you better choose which one to use.
In UDP, each time a datagram is sent, the socket descriptor of the native machine and the socket descriptor of the receiver need to be included. Since TCP is a connection-based protocol, a connection needs to be established between communication socket pairs before communication, so there will be a time-consuming connection programming in TCP protocol.
In UDP, datagram data has a 64KB limit in size. There is no such restriction in TCP. Once the socket pair of TCP communication establishes a connection, the communication between them is similar to an IO stream, and all data will be read in the order it is accepted.
UDP is an unreliable protocol, and the sent datagrams are not necessarily accepted by the receiving socket in the order in which they are sent. Then TCP is a reliable protocol. The order of packets received by the receiving end is consistent with the order of packets on the sending end.
In short, TCP is suitable for network services such as remote login (rlogin, telnet) and file transfer (FTP). Because the size of these data that needs to be transmitted is uncertain. UDP is simpler and lighter than TCP. UDP is used to implement some services that are more real-time or have no importance of packet loss. The packet loss rate of UDP in LAN is relatively low.
socket programming in Java
In the following section, I will explain how to use socket to write client and server programs through some examples.
Note: In the following example, I will use socket programming based on the TCP/IP protocol, because this protocol is much more widely used than UDP/IP. And all socket-related classes are located under the java.net package, so we need to introduce this package when we are doing socket programming.
Client writing
Turn on Socket
If you are on the client side, you need to write the following code to open a socket.
String host = "127.0.0.1";int port = 8919;Socket client = new Socket(host, port);
In the above code, host is the machine that the client needs to connect to, and port is the port used by the server to listen to the request. When selecting a port, one thing you need to pay attention to is that ports such as 0~1023 have been reserved by the system. These ports are used by some commonly used services, such as mail, FTP and HTTP. When you are writing server-side code and selecting a port, please select a port larger than 1023.
Write data
Next is to write the request data. We get the OutputStream object from the client's socket object and then write the data. Very similar to file IO processing code.
public class ClientSocket { public static void main(String args[]) { String host = "127.0.0.1"; int port = 8919; try { Socket client = new Socket(host, port); Writer writer = new OutputStreamWriter(client.getOutputStream()); writer.write("Hello From Client"); writer.flush(); writer.close(); client.close(); } catch (IOException e) { e.printStackTrace(); } } }Close IO object
Similar to file IO, after reading and writing data, we need to close the IO object to ensure the correct release of resources.
Server-side writing
Open the server-side socket
int port = 8919;ServerSocket server = new ServerSocket(port);Socket socket = server.accept();
The above code creates a server-side socket, and then calls the accept method to listen and get the client's request socket. The accept method is a blocking method that waits for blocking until a connection is made between the server and the client.
Read data
Get the InputStream object through the socket object obtained above, and then install the file IO to read the data. Here we print out the content.
public class ServerClient { public static void main(String[] args) { int port = 8919; try { ServerSocket server = new ServerSocket(port); Socket socket = server.accept(); Reader reader = new InputStreamReader(socket.getInputStream()); char chars[] = new char[1024]; int len; StringBuilder builder = new StringBuilder(); while ((len=reader.read(chars)) != -1) { builder.append(new String(chars, 0, len)); } System.out.println("Receive from client message=: " + builder); reader.close(); socket.close(); server.close(); } catch (Exception e) { e.printStackTrace(); } }}Close IO object
It cannot be forgotten. Finally, I need to close the IO object correctly to ensure the correct release of resources.
Note an example
Here we add an example, using socket to implement an echo server, that is, the server will pass the data sent by the client back to the client. The code is very simple.
import java.io.*;import java.net.*;public class EchoServer { public static void main(String args[]) { // declaration section: // declare a server socket and a client socket for the server // declare an input and an output stream ServerSocket echoServer = null; String line; DataInputStream is; PrintStream os; Socket clientSocket = null; // Try to open a server socket on port 9999 // Note that we can't choose a port less than 1023 if we are not // privileged users (root) try { echoServer = new ServerSocket(9999); } catch (IOException e) { System.out.println(e); } // Create a socket object from the ServerSocket to listen and accept // connections. // Open input and output streams try { clientSocket = echoServer.accept(); is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); // As long as we receive data, echo that data back to the client. while (true) { line = is.readLine(); os.println(line); } } catch (IOException e) { System.out.println(e); } }}Compile and run the above code and make the following request, you can see the content of the data carried by the client request.
15:00 $ curl http://127.0.0.1:9999/?111GET /?111 HTTP/1.1User-Agent: curl/7.37.1Host: 127.0.0.1:9999Accept: */*
Summarize
It is quite interesting to perform client-server programming, and socket programming in Java is easier and faster than other languages (such as C).
The java.net package contains many powerful and flexible classes for developers to program networks. When programming networks, it is recommended to use the API below this package. At the same time, the Sun.* package also contains many network programming-related classes, but it is not recommended to use the API below this package, because this package may change. In addition, this package cannot be guaranteed to be included on all platforms.
The above is a compilation of Java Socket information. We will continue to add relevant knowledge in the future. Thank you for your support for this website!