TCP connection
The basis of TCP is Socket. In TCP connection, we will use ServerSocket and Socket. After the client and server establish a connection, the rest is basically the control of I/O.
Let's first look at a simple TCP communication, which is divided into client and server.
The client code is as follows:
public static void main(String[] args) throws IOException
{
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null;
BufferedReader brTemp = null;
try
{
socket = new Socket(InetAddress.getLocalHost(), 5678);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream());
brTemp = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String line = brTemp.readLine();
pw.println(line);
pw.flush();
if (line.equals("end")) break;
System.out.println(br.readLine());
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
finally
{
if (socket != null) socket.close();
if (br != null) br.close();
if (brTemp != null) brTemp.close();
if (pw != null) pw.close();
}
}
}
public static void main(String[] args) throws IOException
{
ServerSocket server = null;
Socket client = null;
BufferedReader br = null;
PrintWriter pw = null;
try
{
server = new ServerSocket(5678);
client = server.accept();
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
pw = new PrintWriter(client.getOutputStream());
while(true)
{
String line = br.readLine();
pw.println("Response:" + line);
pw.flush();
if (line.equals("end")) break;
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
finally
{
if (server != null) server.close();
if (client != null) client.close();
if (br != null) br.close();
if (pw != null) pw.close();
}
}
}
The above code basically outlines the main framework of the client and server during the TCP communication process. We can find that in the above code, the server can only process one request from the client at any time. It is serial processing. Yes, it cannot be parallelized. This is not the same as the server processing method in our impression. We can add multi-threads to the server. When a client request comes in, we create a thread to process the corresponding request.
The improved server-side code is as follows:
class ServerThread extends Thread
{
private Socket socket = null;
public ServerThread(Socket socket)
{
this.socket = socket;
}
public void run() {
BufferedReader br = null;
PrintWriter pw = null;
try
{
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream());
while(true)
{
String line = br.readLine();
pw.println("Response:" + line);
pw.flush();
if (line.equals("end")) break;
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
finally
{
if (socket != null)
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (pw != null) pw.close();
}
}
}
In the process of programming, we have the concept of "resource". For example, database connection is a typical resource. In order to improve performance, we usually do not destroy the database connection directly, but use the database connection pool to manage multiple databases. Connections are managed and reused. For Socket connections, it is also a resource. When our program requires a large number of Socket connections, it will be a very inefficient approach if each connection needs to be re-established.
Similar to the database connection pool, we can also design a TCP connection pool. The idea here is that we use an array to maintain multiple Socket connections, and another status array to describe whether each Socket connection is in use. When the program requires a Socket connection, We traverse the status array and take out the first unused Socket connection. If all connections are in use, an exception is thrown. This is a very intuitive and simple "scheduling strategy". In many open source or commercial frameworks (Apache/Tomcat), there will be similar "resource pools".
The code for the TCP connection pool is as follows:
private InetAddress address = null;
private int port;
private Socket[] arrSockets = null;
private boolean[] arrStatus = null;
private int count;
public TcpConnectionPool(InetAddress address, int port, int count)
{
this.address = address;
this.port = port;
this .count = count;
arrSockets = new Socket[count];
arrStatus = new boolean[count];
init();
}
private void init()
{
try
{
for (int i = 0; i < count; i++)
{
arrSockets[i] = new Socket(address.getHostAddress(), port);
arrStatus[i] = false;
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
public Socket getConnection()
{
if (arrSockets == null) init();
int i = 0;
for(i = 0; i < count; i++)
{
if (arrStatus[i] == false)
{
arrStatus[i] = true;
break;
}
}
if (i == count) throw new RuntimeException("have no connection available for now.");
return arrSockets[i];
}
public void releaseConnection(Socket socket)
{
if (arrSockets == null) init();
for (int i = 0; i < count; i++)
{
if (arrSockets[i] == socket)
{
arrStatus[i] = false;
break;
}
}
}
public void reBuild()
{
init();
}
public void destroy()
{
if (arrSockets == null) return;
for(int i = 0; i < count; i++)
{
try
{
arrSockets[i].close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
continue;
}
}
}
}
UDP is a connection method different from TCP. It is usually used in situations that require high real-time performance and low requirements for accuracy, such as online video. UDP will have "packet loss". In TCP, if the Server is not started, an exception will be reported when the Client sends a message, but for UDP, no exception will be generated.
The two classes used for UDP communication are DatagramSocket and DatagramPacket, the latter stores the content of the communication.
The following is a simple UDP communication example. Like TCP, it is also divided into two parts: Client and Server. The client code is as follows:
public static void main(String[] args)
{
try
{
InetAddress host = InetAddress.getLocalHost();
int port = 5678;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String line = br.readLine();
byte[] message = line.getBytes();
DatagramPacket packet = new DatagramPacket(message, message.length, host, port);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
if (line.equals("end")) break;
}
br.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
}
public static void main(String[] args)
{
try
{
int port = 5678;
DatagramSocket dsSocket = new DatagramSocket(port);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while(true)
{
dsSocket.receive(packet);
String message = new String(buffer, 0, packet.getLength());
System.out.println(packet.getAddress().getHostName() + ":" + message);
if (message.equals("end")) break;
packet.setLength(buffer.length);
}
dsSocket.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
}
Multicast uses a similar method to UDP. It uses class D IP addresses and standard UDP port numbers. Class D IP addresses refer to addresses between 224.0.0.0 and 239.255.255.255, excluding 224.0.0.0.
The class used in multicast is MulticastSocket, which has two methods to pay attention to: joinGroup and leaveGroup.
The following is an example of multicast. The client code is as follows:
public static void main(String[] args)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
InetAddress address = InetAddress.getByName("230.0.0.1");
int port = 5678;
while(true)
{
String line = br.readLine();
byte[] message = line.getBytes();
DatagramPacket packet = new DatagramPacket(message, message.length, address, port);
MulticastSocket multicastSocket = new MulticastSocket();
multicastSocket.send(packet);
if (line.equals("end")) break;
}
br.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
}
public static void main(String[] args)
{
int port = 5678;
try
{
MulticastSocket multicastSocket = new MulticastSocket(port);
InetAddress address = InetAddress.getByName("230.0.0.1");
multicastSocket.joinGroup(address);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while(true)
{
multicastSocket.receive(packet);
String message = new String(buffer, packet.getLength());
System.out.println(packet.getAddress().getHostName() + ":" + message);
if (message.equals("end")) break;
packet.setLength(buffer.length);
}
multicastSocket.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
}
NIO is a new set of IO API introduced in JDK1.4. It has new designs in buffer management, network communication, file access and character set operations. For network communication, NIO uses the concepts of buffers and channels.
The following is an example of NIO, which is very different from the coding style we mentioned above.
public static void main(String[] args)
{
String host="127.0.0.1";
int port = 5678;
SocketChannel channel = null;
try
{
InetSocketAddress address = new InetSocketAddress(host,port);
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer buffer = ByteBuffer.allocate(1024);
CharBuffer charBuffer = CharBuffer.allocate(1024);
channel = SocketChannel.open();
channel.connect(address);
String request = "GET / /r/n/r/n";
channel.write(encoder.encode(CharBuffer.wrap(request)));
while((channel.read(buffer)) != -1)
{
buffer.flip();
decoder.decode(buffer, charBuffer, false);
charBuffer.flip();
System.out.println(charBuffer);
buffer.clear();
charBuffer.clear();
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
finally
{
if (channel != null)
try {
channel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}