The client program of Java Socket communications (1) sends and receives data

  • 2020-05-07 19:35:46
  • OfStack

Web applications are divided into client and server parts, and the Socket class is the Java class that handles client communication. This class allows you to connect to a server with an IP or domain name, and to send and receive data from each other.

For the Socket communication brief, the server writes to the output stream of Socket, and the client can read the corresponding content through the input stream of Socket. Socket and Socket are bidirectional, so the client can also write into the corresponding Socket output stream, and the server's Socket input stream can read the corresponding content.

example 1: shorthand for the client (1).


Socket client = null;
try{
client = new Socket(Ip,Port);
String msg=" Send the data content! ";
// get socket Reading and writing flow , Sends data to the server program  
client.getOutputStream().write(msg.getBytes());
byte[] datas = new byte[2048];
// Receive data from a server-side program 
client.getInputStream().read(datas);
System.out.println(new String(datas));
}catch(Exception e){
e.printStackTrace();
}finally {
if (client != null) {
try {
client.close();
} catch (IOException e) {
System.out.println("systemerr:" +e);
}
}
}

example 2: client shorthand (2).


try{
client = new Socket();
SocketAddress socketAddress = new InetSocketAddress(Ip,Port);
client.connect(socketAddress, 3000);
String msg=" Accessed server returns content! ";
// get socket Reading and writing flow , Sends data to the server program  
client.getOutputStream().write(msg.getBytes());
byte[] datas = new byte[2048];
// Receive data from a server-side program 
client.getInputStream().read(datas);
System.out.println(new String(datas));
}catch(Exception e){
e.printStackTrace();
}finally {
if (client != null) {
try {
client.close();
} catch (IOException e) {
System.out.println("systemerr:" +e);
}
}
}

example 3: client complete writing.


try { 
//1. Build client socket Connect, specifying the server location and port  
Socket socket =new Socket(Ip,Port); 
//2. get socket Reading and writing flow  
OutputStream os=socket.getOutputStream(); 
PrintWriter pw=new PrintWriter(os); 
// The input stream  
InputStream is=socket.getInputStream(); 
BufferedReader br=new BufferedReader(new InputStreamReader(is)); 
//3. Utilize flow according to 1 Definite operation, right socket Read and write  
String sendInfo=" Data information sent to the server! ";
pw.write(sendInfo); 
pw.flush(); 
socket.shutdownOutput(); 
// Receive the response from the server  
String replyInfo=null; 
while(!((replyInfo=br.readLine())==null)){ 
System.out.println(" Data information of the receiving server: "+replyInfo); 
} 
//4. Close the resource  
br.close(); 
is.close(); 
pw.close(); 
os.close(); 
socket.close(); 
} catch (UnknownHostException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
}

About Java Socket communication (1) of the client program to send and receive data related knowledge, this site to give you the introduction here, more information please visit this site website for more information!


Related articles: