[Socket communication (Java)] Impressions of implementing Socket communication in practice for the first time

Since I had the opportunity to implement Socket communication in practice for the first time, I will make a note of my impressions and technology used. When I was studying by myself in the past, I didn't get it right, but I was able to experience it by looking at the data specifications and implementing it.

1. Socket communication overview

It seems that there are two types, server socket and client socket, but this time I used only the client socket. The impression was that there was a Java Socket API, but in the end, it was a byte stream exchange. The data specification also said something like "Data A: xxx to 4 bytes".

I referred to the following articles. @ Hyman1993 Summary of Java communication API (1) How to use Socket @Gin About TCP stream communication breaks / breaks

2. Byte order

There was a statement in the design document that "data is stored in little endian". For me, who is a beginner of Socket communication, it was a mess.

I referred to the article by @mikanbako. Byte order must be reset to duplicate ByteBuffer

2.1. There are 2 points.

--In order to process the read data, it is necessary to convert each byte array specified in the data specifications into a numerical value or a character string. --If the data is little endian, you need to specify the little endian when replicating to the ByteBuffer class. Ex. byteBuffer.order(ByteOrder.LITTLE_ENDIAN)

2.2. Excerpt from the article

LittleEndianSample.java


import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class LittleEndianSample {
    public static final void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);

        //Little endian 16-bit integer. The low byte is 0x11 and the high byte is 0x22.
        buffer.order(ByteOrder.LITTLE_ENDIAN)
            .put((byte) 0x11)
            .put((byte) 0x22)
            .rewind();

        System.out.printf("0x%x\n", buffer.getShort());
    }
}

Recommended Posts

[Socket communication (Java)] Impressions of implementing Socket communication in practice for the first time
Impressions and doubts about using java for the first time in Android Studio
Introduction to java for the first time # 2
Learning for the first time java [Introduction]
The story of learning Java in the first programming
Feel the passage of time even in Java
Programming for the first time in my life Java 1st Hello World
The story of intentionally using try catch for the first time in my life
Learn for the first time java # 3 expressions and operators
Learning memo when learning Java for the first time (personal learning memo)
Spring Boot for the first time
A summary of what Java programmers find when reading Kotlin source for the first time
Spring AOP for the first time
[Introduction] Setting up GridDB Community Edition in the CentOS container of Docker Desktop for the first time
Get the result of POST in Java
TCP communication (socket communication) in Java (ASCII, Binary)
First steps for deep learning in Java
The story of writing Java in Emacs
The story of releasing the Android app to the Play Store for the first time.
I tried using Docker for the first time
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
About the idea of anonymous classes in Java
ChatWork4j for using the ChatWork API in Java
Measure the size of a folder in Java
Sample source code for finding the least common multiple of multiple values in Java
Walls hit by Rspec for the first time
Android Studio development for the first time (for beginners)
I tried touching Docker for the first time
Use Java external library for the time being
The date time of java8 has been updated
A story about a super beginner participating in the AtCoder contest for the first time (AtCoder Beginner Contest 140)
Import files of the same hierarchy in Java
Run Dataflow, Java, streaming for the time being
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation
First touch of the Files class (or Java 8)
Enable / disable SNI in Java for each communication
[Android studio / Java] What you don't understand when you touch it for the first time
Get the URL of the HTTP redirect destination in Java
[For beginners] DI ~ The basics of DI and DI in Spring ~
[For beginners] Quickly understand the basics of Java 8 Lambda
A note for Initializing Fields in the Java tutorial
[Java] Get the file in the jar regardless of the environment
Summary of Java communication API (1) How to use Socket
Change the storage quality of JPEG images in Java
Consideration of options for implementing native apps (in writing)
Summarize the additional elements of the Optional class in Java 9
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
Was done in the base year of the Java calendar week
[For beginners] Explanation of classes, instances, and statics in Java
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
A quick explanation of the five types of static in Java
[Rails] I tried using the button_to method for the first time
Try implementing signature verification for elliptic curve cryptography in Java
Set the time zone in the JVM of your Azure application
Count the number of digits after the decimal point in Java
How to derive the last day of the month in Java
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
Processing time measurement for each BCrypt + stretch count in Java
Modeling a Digimon with DDD for the first time Part 1