Java Programming Network Programming TCP Programming
Complete the development of TCP programs using Socket s
Use this class to easily establish a reliable, two-way, continuous, point-to-point communication connection
In Socket development, the server side uses ServerSocket to wait for the client to connect. For Java network programs, each client uses a Socket object to represent
ServerSocket Class and Socket Class
The ServerSocket class is primarily used for server-side program development to receive connection requests from clients
ServerSocket class
Method | Effect |
---|---|
public ServerSocket(int port) throws IOException | Create a ServerSocket instance and specify a listening port |
public Socket accept() throws IOException | Waiting for client connection, blocking until connection |
public InetAddress getInetAddress() | Return the IP address of the server |
public boolean isClosed() | Returns the closed state of the ServerSocket |
public void close() throws IOException | Close ServerSocket |
Use the accept() method to wait for client connections each time the server runs
The server side will be blocked after this method is executed
The program cannot continue down until the client is connected
The return value type of this method is Socket
Each Socket represents a client object
Socket class
Method | Effect |
---|---|
public Socket(String host,int port) throws UnknownHostException,IOException | Construct the Socket object, specifying the host name and port to connect to the server |
public InputStream getInputStream() throws IOException | Returns the input stream for this socket |
public OutputStream getOutputStream() throws IOException | Returns the output stream of this socket |
public void close() throws IOException | Close this Socket |
public boolean isClosed() | Determine if this socket is closed |
On the client side, the program can get the output information of the server through the getInputStream() method of the Socket class, and on the server side, it can get the output information of the client through the getOutputStream() method.
Write TCP program
Write server program
import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class Hello { public static void main(String[] args) throws IOException { ServerSocket server = null; Socket client = null; PrintStream out = null; server = new ServerSocket(8889); System.out.println("Server running, waiting for client connection"); client = server.accept(); String str = "HELLO"; out = new PrintStream(client.getOutputStream()); out.println(str); out.close(); client.close(); server.close(); } }
[External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-M4azSWf1-1645464825525) (C:/Users/30452/AppData/Roaming/Typora/typora-user-images/image-20220220220213550615.png)]
Write client program
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Socket; public class Hello2 { public static void main(String[] args) throws Exception{ Socket client = null; client = new Socket("localhost",8889); BufferedReader buf = null; buf = new BufferedReader(new InputStreamReader(client.getInputStream())); String str = buf.readLine(); System.out.println("Server Output:"+str); client.close(); buf.close(); } }