This article uses Java 10.0.1 installed on WIN10.
OSI's Layer 4 (transport) protocols, TCP and UDP, are often used to communicate between systems. The benefits of TCP and UDP are: TCP --High reliability UDP --Fast speed In addition, TCP and UDP can communicate between systems, but other than that, some I / O processing must be performed on the data to be communicated, so an I / O processing method is also required. Blocking and (BIO) or non-blocking (NIO) are often used here. This time, I will implement communication between systems with TCP / IP + BIO.
A library called ** socket ** is used for TCP communication. We provide ** Socket ** and ** ServerSocket ** from Java language, so this time we will implement TCP / IP + BIO using these. (Official API is here: Socket, ServerSocket /javase/jp/6/api/java/net/ServerSocket.html)) ** Server-side files (actually local) **
*** Create a socket and listen for client-side access *** TcpServer file contents:
import java.io.*;
import java.net.*;
public class TcpServer{
    public static void main (String[] argv) throws Exception{
        try (
            //8001 Create port number
            ServerSocket server = new ServerSocket(8001);
            FileOutputStream fos = new FileOutputStream("server_recv.txt");
            FileInputStream fis = new FileInputStream("server_send.txt");
        ){
            System.out.println("wait access...");
            //Wait for client-side access
            Socket socket = server.accept();
            System.out.println("client access.");
            int ch;
            //Server the stream passed from the client side_recv.Write in txt
            InputStream input = socket.getInputStream();
            while ((ch = input.read()) != 0) {
                fos.write(ch);
            }
            // server_send.Pass the content stream written in txt to the client side
            OutputStream output = socket.getOutputStream();
            while ((ch = fis.read()) != -1) {
                output.write(ch);
            }
            //Access end
            socket.close();
            System.out.println("accsess closed.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
server_send.txt File contents:
server_send
server_recv.txt is an empty file
** Client-side files **
*** Create a socket and access the specified server and port *** TcpClient file contents:
import java.io.*;
import java.net.*;
public class TcpClient{
    public static void main (String[] argv) throws Exception{
        try (
            //8001 Access port number
            Socket socket = new Socket("localhost", 8001);
            FileOutputStream fos = new FileOutputStream("client_recv.txt");
            FileInputStream fis = new FileInputStream("client_send.txt");
        ){
            int ch;
            // client_send.Pass the content stream written in txt to the server side
            OutputStream output = socket.getOutputStream();
            while ((ch = fis.read()) != -1) {
                output.write(ch);
            }
            //Pass "Transfer completed!" To the server side
            output.write(0);
            //Client the stream passed from the server side_recv.Write in txt
            InputStream input = socket.getInputStream();
            while ((ch = input.read()) != -1) {
                fos.write(ch);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
client_send.txt File contents:
client_send
client_recv.txt is an empty file
Compile TcpClient.java and TcpServer.java respectively in javac and run in java
--Run TcpServer.class
Starting the server and waiting for a connection on the client side:
PS C:\Users\ma\Documents\work\javaweb\1> java TcpServer
wait access...
--Run TcpClient.class
Start the client and access the server side:
PS C:\Users\ma\Documents\work\javaweb\1> java TcpClient
The server terminates after processing the connection from the client side:
PS C:\Users\ma\Documents\work\javaweb\1> java TcpServer
wait access...
client access.
accsess closed.
PS C:\Users\ma\Documents\work\javaweb\1>
server_recv.txt File contents:
client_send
I was able to verify that the content passed from the client side (client_send.txt) is overwritten.
client_recv.txt also overwrote the contents (server_send.txt) passed from the server side:
server_send
This time, I made a TCP / IP + BIO transfer path using Java Socket and ServerSocket libraries. We also verified that data can be sent bidirectionally between the server and the client.
Recommended Posts