Detail of client and server instances in java simulating TCP transport
- 2020-06-15 08:54:40
- OfStack
1. Create the client for TCP transport
1. Socket service of TCP client is established. Socket object is used.
2. If the connection is established successfully, the data transmission channel has been established. The channel is Socket stream, which is established at the bottom level.
3. Use the output stream to write out the data;
4. Turn off the Socket service.
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
// 1 , create a client Socket service
Socket socket = new Socket("192.168.1.100", 10002);
// 2 , get Socket Input stream in stream
OutputStream out = socket.getOutputStream();
// 3 , USES an output stream to write out the specified data
out.write("TCP is coming !".getBytes());
// 4 , close Socket service
socket.close();
}
}
2. Create the TCP transfer server
1. Establish Socket service on TCP server, through ServerSocket object;
2. The server must provide an external port, otherwise the client cannot connect;
3. Get the connected client object;
4. Obtain Socket stream through client object and read data sent by client;
5, close the resources, close the client, close the server.
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
// 1 , create client objects
ServerSocket ss = new ServerSocket(10002);
// 2 , gets the connected client object
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
// 3 And through Socket Object gets the input stream and reads the data sent by the client
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf, 0, len);
System.out.println(ip + ":" + text);
// 4 , close resources
s.close();
ss.close();
}
}
3. Optimize the client and server of TCP transmission
In this part, we optimize the contents of the first two parts to realize the interaction between client and server under TCP transmission mode.
/**
* To optimize the TCP Transfer client
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ClientUpdate {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("192.168.1.100", 10002);
OutputStream out = socket.getOutputStream();
out.write("tcp!".getBytes());
// Read the data returned by the server Socket Read the flow
InputStream in = socket.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf, 0, len);
System.out.println(text);
socket.close();
}
}
/**
* To optimize the TCP The service side of the transfer
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerUpdate {
public static void main(String[] args) throws IOException {
// 1 , create server-side objects
ServerSocket ss = new ServerSocket(10002);
// 2 , gets the connected client object
Socket s = ss.accept(); //accept The method is blocking
String ip = s.getInetAddress().getHostAddress();
// 3 And through Socket Object gets the input stream and reads the data sent by the client
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf, 0, len);
System.out.println(ip + ":" + text);
// Using the client Socket The output stream of the object returns data to the client
OutputStream out = s.getOutputStream();
out.write(" received ".getBytes());
s.close();
ss.close();
}
}
4. Create an English uppercase conversion server
Using the related nature of TCP (Transmission Control Protocol, transmission control protocol), we created an English uppercase conversion server based on THE transmission of TCP. The server receives the data and displays it in the console, and returns the data to the client in uppercase. The conversion ends when the client enters "over".
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class TransClient {
public static void main(String[] args) throws IOException {
/**
* Idea: Create a client
* 1 , create, Socket Client object
* 2 , access to keyboard input data
* 3 , send the input information to Socket The output stream
* 4 , read data from the server and return upper-case data
*/
// 1 , create, Socket Client object
Socket s = new Socket("192.168.1.100", 10004);
// 2 , access to keyboard input
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
// 3 , Socket The output stream
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
// 4 , Socket Enter the stream, read the data from the server, and return the uppercase data
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while ((line = bufr.readLine()) != null) {
if ("over".equals(line))
break;
out.println(line);
// Reads what the server returns 1 Row uppercase data
String upperStr = bufIn.readLine();
System.out.println(upperStr);
}
s.close();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TransServer {
public static void main(String[] args) throws IOException {
/**
* Idea: Create a server
* 1 , create, SeverSocket Client object
* 2 , get Socket flow
* 3 And through Socket . Read the data sent by the client that needs to be converted
* 4 , displayed on the console
* 5 , converts the data to uppercase and returns it to the client
*/
// 1 , create, SeverSocket object
ServerSocket ss = new ServerSocket(10004);
// 2 , get Socket object
Socket s = ss.accept();
// To obtain IP address
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + "......connected");
// 3 , get Socket Read the stream and decorate
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
// 4 , get Socket Output stream and trim
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
String line = null;
while ((line = bufIn.readLine()) != null) {
System.out.println(line);
out.println(line.toUpperCase());
}
s.close();
ss.close();
}
}
Thank you for reading, I hope to help you, thank you for your support to this site!