Home > Back-end >  In the TCP server side what's the use of this code if (reader!=null) {reader. The close ();//of
In the TCP server side what's the use of this code if (reader!=null) {reader. The close ();//of

Time:10-25

import java.io.*;
import java.net.*;

Public class MyTcp {//create the class MyTcp
Private BufferedReader reader;//create BufferedReader object
Private ServerSocket server;//create ServerSocket object
Private Socket Socket;//create a Socket object Socket

Void getserver () {
Try {
Server=new ServerSocket (8997);//instantiate the Socket object
System. The out. Println (" server socket has been created successfully ");//output information
While (true) {//if the socket is connected state
System. The out. Println (" waiting for the client connection ");//output information
The socket=server. The accept ();//instantiate the Socket object
Reader=new BufferedReader (new InputStreamReader (socket
GetInputStream ()));//instantiate BufferedReader object
GetClientMessage ();//call getClientMessage () method
}
} the catch (Exception e) {
e.printStackTrace();//output exception information
}
}

Private void getClientMessage () {
Try {
While (true) {//if the socket is connected state
If (reader) ready ()) {
//client information
System. The out. Println (" the client: "+ reader. ReadLine ());
}
}
} the catch (Exception e) {
e.printStackTrace();//output exception information
}
try {
If (reader!=null) {
Reader. The close ();//off flow
}
If (socket!=null) {
Socket. The close ();//close the socket
}
} the catch (IOException e) {
e.printStackTrace();
}

}

Public static void main (String [] args) {//main method
MyTcp TCP=new MyTcp ();//create the class object
TCP. Getserver ();//call the method
}
}









If (reader!=null) {
Reader. The close ();//off flow
}
If (socket!=null) {
Socket. The close ();//close the socket
}
} the catch (IOException e) {
e.printStackTrace();


He is what error message to the circumstances under which an error of
The student party request bosses support
  • Related