This time, I will briefly summarize how to use the Java communication API. First, let's introduce how to use Socket. Socket is one of the original java communication APIs. Generally, both the server side and the client can communicate based on the tcp (udp?) Connection.
Let's make a server according to the following source.
--Create a ServerSocket for communication
--Connect with the client
--Take a stream and read the data information from the client
--Take a stream and write data information to the client
Server.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
 
public class Server {
   public static void main(String[] args) {
      try {
         ServerSocket serverSocket = new ServerSocket(6666);
         System.out.println("server start....");
         Socket s = serverSocket.accept();
         //Log output
         System.out.println("client:"+s.getInetAddress().getLocalHost()+"access successed");
         
         BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
         //Read data information from client
         String mess = br.readLine();
        //Log output
         System.out.println("client:"+mess);
         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
     
     //Write data information to the client
         bw.write(mess+"\n");
         bw.flush();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
--Create an instance of Socket and set IP and Port
--Take an instance of the stream
--Send to server
--Read the reply from the server
Client.java 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
 
public class Client {
   public static void main(String[] args) {
      try {
         Socket s = new Socket("127.0.0.1",6666);
         
         //Take an instance of a stream
         InputStream is = s.getInputStream();
         OutputStream os = s.getOutputStream();
         
         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
         //Send data information to the server
         bw.write("client -> server:hello server! \n");
         bw.flush();
         
         //Read the reply from the server
         BufferedReader br = new BufferedReader(new InputStreamReader(is));
         String mess = br.readLine();
         //Log output
         System.out.println("server:"+mess);
      } catch (UnknownHostException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Thank you for reading to the end. This time I will summarize how to use HttpURLconnection.
Recommended Posts