Summary of Java communication API (1) How to use Socket

Introduction

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.

How to use ServerSocket (server side)

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();
      }
   }
}

How to use Socket (client side)

--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();
      }
   }
}

Finally

Thank you for reading to the end. This time I will summarize how to use HttpURLconnection.

Recommended Posts

Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
[Java] [Maven3] Summary of how to use Maven3
[java] Summary of how to handle char
[java] Summary of how to handle character strings
[Java] Summary of how to abbreviate lambda expressions
How to use Java API with lambda expression
How to use Chain API
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
[Java] How to use compareTo method of Date class
Summary of how to implement default arguments in Java
How to use Java HttpClient (Post)
[Java] How to use join method
How to use setDefaultCloseOperation () of JFrame
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
How to use JSON data in WebSocket communication (Java, JavaScript)
[Must-see for apprentice java engineer] How to use Stream API
How to call and use API in Java (Spring Boot)
Summary of how to use the proxy set in IE when connecting with Java
How to make a Java calendar Summary
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Java] How to use the HashMap class
Summary of how to write annotation arguments
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] How to use the Calendar class
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
[Java] How to use substring to cut out a part of a character string
[Java] Note how to use RecyclerView and implementation of animated swipe processing.
[For Rails beginners] Summary of how to use RSpec (get an overview)
Summary of how to select elements in Selenium