Java socket programming example based on Tcp protocol

  • 2020-04-01 03:38:05
  • OfStack

This article illustrates the Java socket programming method based on Tcp protocol. Specific analysis is as follows:

The following is a one-to-one communication programming implementation, and we will continue to learn about the implementation of one server listening to multiple clients.

The main steps used here are as follows:

Step 1: create a new socket object with a specific port, such as 4800
Step 2: construct the BufferedReader object with the system input device, which is used to receive the characters entered by the system keyboard
Step 3: construct a PrintWriter with an output stream from a socket object
Step 4: construct the corresponding BufferedReader object by getting the input stream from the socket object, which is used to receive the information sent from the server side

I understand to close the socket: open first then close, socket last close.

The following is the code implementation of the client:

package com.fan.socket;
import java.io.*;
import java.net.Socket; public class SocketClient {
public static void main(String[] args) throws IOException{ try{
Socket socket=new Socket("127.0.0.1",4800);
System.out.println("client start ...");
//A customer request
is issued to port 4800 on the machine BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//The BufferedReader object
is constructed by the system standard input device PrintWriter write=new PrintWriter(socket.getOutputStream());
//The output stream is obtained from the Socket object and the PrintWriter object
is constructed BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//The input stream is obtained by the Socket object and the corresponding BufferedReader object
is constructed String readline;
readline=br.readLine(); //Read in a string
from system standard input while(!readline.equals("end")){
//If the string read from the standard input is "end", the loop
is stopped write.println(readline);
//Output the string read from the system standard input to Server2
write.flush();
//Flush the output stream so that the Server receives the string
immediately System.out.println("Client:"+readline);
//Print the read string
on the system standard output System.out.println("Server:"+in.readLine());
//Read a string from the Server and print it to
on standard output readline=br.readLine(); //Read in a string
from system standard input } //Continue the loop
write.close(); //Close Socket output stream
in.close(); //Close Socket input stream
socket.close(); //Close Socket
}catch(Exception e) {
System.out.println("can not listen to:"+e);//Error, print error message
}
} }

Server-side code implementation:

package com.fan.socket;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket; public class SocketService {
public static void main(String[] args) throws IOException{
SocketService socketService = new SocketService();
socketService.oneServer();
} public void oneServer(){
try{
ServerSocket server=null;
try{
server=new ServerSocket(4800);
System.out.println("server start is ok...");
//Create a ServerSocket on port 4800 to listen for customer requests
}catch(Exception e) {
System.out.println("can not listen to:"+e);
//Error, print error message
}
Socket socket=null;
try{
socket=server.accept();
//Use accept() to block waiting for a customer request. There is a customer
//The request arrives, produces a Socket object, and continues with
}catch(Exception e) {
System.out.println("Error."+e);
//Error, print error message
}
String line;
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//The input stream is obtained by the Socket object and the corresponding BufferedReader object
is constructed PrintWriter writer=new PrintWriter(socket.getOutputStream());
//The output stream is obtained from the Socket object and the PrintWriter object
is constructed BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//The BufferedReader object
is constructed by the system standard input device System.out.println("Client:"+in.readLine());
//Print the string
read from the client on standard output line=br.readLine();
//Reads in a string
from standard input while(!line.equals("end")){
//If the string is "bye", the loop
is stopped writer.println(line);
//Output the string
to the client writer.flush();
//Flush the output stream so that the Client receives the string
immediately System.out.println("Server:"+line);
//Print the read string
on the system standard output System.out.println("Client:"+in.readLine());
//Read a string from the Client and print it to
on standard output line=br.readLine();
//Read in a string
from system standard input } //Continue the loop
writer.close(); //Close Socket output stream
in.close(); //Close Socket input stream
socket.close(); //Close the Socket < br / > server.close(); //Close the ServerSocket < br / > }catch(Exception e) {//Error, print error message
System.out.println("Error."+e);
}
}
}

I hope this article has been helpful to your Java programming.


Related articles: