ServerSocket with socket class TCP socket protocol:The main feature of TCP is the ability to establish long-time connections, but also to ensure data security delivery, but the speed is relatively slow. Use TCP to connect the time there will be three handshake, and then establish the server and the client connection, because the TCP link habit is to ask the other machine exists, does not exist will not establish a connection, exists to establish a connection.
:
ServerSocket class:In Java, if you want to use the TCP protocol to write the service side, you need to use the Serversocke class, which is the TCP protocol.
The operation of the TCP protocol classes are under the java.net package, ServerSocket class is the server side, server socket, socket the word is the meaning of the socket.
Socket class:The same is the TCP protocol and the ServerSocket class is the socket class, this class is as a communication terminal, the existence of the client. Used to go to the server side, if the socket is from the ServerSocket to get the object, that is the server connection to the socket, if it is created by the socket object, then you are the client.
In theory, there can be an infinite socket to connect the ServerSocket end, on the ServerSocket end each has a socket to connect, will create a relative socket object. In other words, there are socket objects on the ServerSocket end, each socket is like a jack on the socket, connecting the socket object on the communication side.
:
ServerSocket and Socket classes:The ServerSocket class can be written on the server side, and the socket class can be used to write clients. When creating a ServerSocket object, you need to declare a port number so that the communication port can be connected via the port number. When you create a socket object, you need to declare an IP address and the port number of the ServerSocket object in order to make a connection request to the server. After the connection request is successfully connected, the socket port creates two streams, one is the InputStream stream and the other is the OutputStream stream. At the same time, the ServerSocket server will get a socket object, and the socket object will also create the InputStream stream and the OutputStream stream.
:
The above is just a cursory talk about how to establish the connection, actually serversocket the server side of the socket object is through the Accept () method, this method listens to the port number of the declaration. Once a communication port is connected, the socket object is created, and the client and server connections are successfully connected through the InputStream stream and the OutputStream stream to interact with the data.
Complete:
Having understood the connection between the two classes, we began to try to write a server-side bar. We use the browser as the socket, because the browser is also the TCP protocol, so we can connect to the server side we write. (Hint: If you determine the code is correct, but the browser is not connected to the written server, you can change IE browser to try)
code example:
Operation Result:
The browser sends a browser message:
We can also use the byte stream, read some pretty sister photos and output to the browser through the network stream, estimated that a lot of the cock silk is a mental shock (manual funny).
code example:
Operation Result:
Now that we know how to write the server side, and know that we can use the browser as a client to connect, then use the socket class to write a client, simple to interact with the server side of the data. The server-side authoring does not differ from the first example above, and it does not explain too much.
The client is required to declare an IP address and port number, this port number is the server side of the port number, this should be noted. And then the same through the flow of data, read or output data, need to see whether the server is first read or output, server-side first read the client will need to write the output data code, otherwise. There is no rule about who reads or outputs first, and is determined by the code written at the other end.
Server-Side code example:
Sample client code:
Operation Result:
When you have two results and want two console interfaces, you can click here and select Newconsoleview to create a new console:
The relationship between the code is indicative:
Multiple network cards:Multi-network card is a computer has several network cards, the general ordinary PC has two network cards, a physical network card, a virtual network card. Laptops generally have three network cards a wireless card, a physical network card, a virtual network card.
Generally on the server has a lot of network cards, so there are a lot of network interface, because some servers will need "three-in-one network", "three-in-one network" is to connect Unicom, telecommunications, mobile cable are connected to this server, then this server can use the network of three operators simultaneously.
And because the server room is usually a lot of servers in the cluster, so also need a part of the network interface to connect the internal cluster network, part of the network interface is connected through the firewall and other security measures to filter the external network.
Multi-NIC Server Picture: