"TCP / IP JAVA network programming understood from the basics" Compiled with Eclipse. Debug with Wireshark.

Recommended for those who have never touched JAVA. It's still interesting after 20 years.

TCP / IP Java Network Programming from the Basics, Ohmsha, Tomohiro Odaka, 1999.8

基礎.png

https://www.amazon.co.jp/dp/4274064867/ It has been used as a teaching material for JAVA training since the year the first edition was published. The second edition (URL above) was released in 2002. Before reaching this book, I entered the sources of 20 books (or compiled them on the CD). The advantage of this book over other books is that you can experience how easy it is to operate the network, which is a specialty of JAVA.

In 1996, I practiced JAVA and Microsoft J ++ from Sun Microsystems in another book. Since 1999, I have practiced with JBuilder in this book. Some of us have taken this book and completed three-digit exercises, and now there are those who teach at universities and companies.

This time, we will use Eclipse's Java Developer tools.

Use of wireshark

Understand basic communication conventions such as TCP, IP, UDP, and HTTP. In addition, it is good to check the operation while acquiring data with software called Wireshark that monitors packets.

http://wireshark.org The wireshark source code may also be used in various communication devices. Details at the end of this article.

Eclipse, Java Developer tools introduced

Windows https://www.eclipse.org Download Eclipse from. After downloading Select "Eclipse Java Developer tools" and install it additionally. If you don't make this selection, you can compile, but you may get an execution error.

Macintosh Introducing Eclipse to Macintosh OS X (5 walls) https://qiita.com/kaizen_nagoya/items/b17f03db00aceee41a80

Debian/Ubuntu/Raspbian

apt-get update
apt-get install eclipse

Introduced 113 packages on Raspbian.

JAVA program, project, package, java / class created in Eclipse

Creating a JAVA project

java0.png Select "Java Project" from "New" in the "File" menu.

java01.png If it is 2.2.1 Readnet, enter "Readnet" in "Project Name". When "Finish" becomes selectable, press "Finish".

If the "Java Project" does not appear, it means that the introduction of "Java developer tools" has not been successful. I will try to introduce it again.

Create class file

java02.png

Select "class" of "New" from the "File" menu.

java03.png

Enter "readnet" and "Readnet" in "Package:" and "Name:". When "Finish" becomes selectable, press "Finish".

If you capitalize the Package name as "Readnet", the following warning will appear under "Java Class" at the top. 「This package name is discouraged. By convention, package names usually start with a lowercase letter」

Rednet.java


package readnet;

public class Readnet {

}

Make sure the file name and class name are the same. Java assumes one class definition in one file. Therefore, if the file name and class name are different, a compile error will occur.

Compile, run, debug in Eclipse

compile

java1.png Select "Run Configurations" from the "Run" menu. java22.png

Double-click Java Application. java32.png Select the "(x) = Arguments" tab, enter the required parameters in "Program arguments:", and press "Run".

For example, as a usage in the P88 "2.2 Network Functions Available in JAVA" program

//Example JAVA Readnet kick.fuis.fukui-u.ac.jp 60000

It has become. Here, enter the environment you are using. For example, if 192.168.2.23 is an IP address, you may enter the IP address directly. Also, even if the port number is 60000, you can use another available number to deal with the other party.

In C language, the program name is entered in args [0], but in JAVA, the first argument is entered in args [0].

java33.png

You will be asked if you want to save the file. Press "OK".

java34.png

Run-time errors or program results appear in the "Console".

java35.png If there is a compilation error, it will be displayed in the "Problems" tag.

Compile error

At first, a compile error appears 2-3 times due to a description error, and I remember it while fixing it.

java36.png

There is a compilation error, but if you want to proceed, select "Proceed", and if you want to go back and fix it and compile again, select "Cancel". java4.png

The "Package Explorer", the line number of the source code, and the "Outline" also show the location of the compilation error. If you select the "Problems" tag, you will see a list of compilation errors. If there are more than one error java41.png

Execution / measurement by wireshark

If the execution result is not what you expected, Start wireshark and measure packets. wire1.png When interacting with other network devices, select the interface to which they are connected. For example, "Ethernet: en0". If you want to interact with yourself, select "Loopback: lo0".

wire2.png

To avoid this, on Macintosh and Linux, use the command prompt.

su otheruser
wireshark &

Select "Loopback: lo0". Here, other user is the ID of another user. Even if you type otheruser, it is said that you do not know that there is no person with the ID otheruser.

wire3.png

You can measure packets from yourself to yourself.

Introduction of wireshark

Windows https://www.wireshark.org/download.html

Windows Installer (64-bit)
Windows Installer (32-bit)

Drop one of them and execute.

Macintosh Five Walls to Introduce Wireshark to Macintosh https://qiita.com/kaizen_nagoya/items/69eb2d357a125f5368e1 reference

Debian/Ubuntu/Raspbian

apr-get update
apt-get install wireshark

10 Barriers to Leveraging Wireshark on Raspberry Pi

https://qiita.com/kaizen_nagoya/items/b3fa0a20855d44c3768d

Summary

Features of JAVA (compared to C and C ++)

1 The Class name and file name are the same, and the extensions are class and java, respectively. Project and Package may be the same. However, in principle, Package starts with a lowercase letter. 2 Good at Internet (compared to C and C ++ languages before JAVA) There is a main function in 3 classes. A feeling of strangeness. It may be good to have the main class as the main class or the OS class of the execution environment. 4 In C language, args [0] is the program name. In JAVA, the first argument is entered in args [0].

JAVA in Eclipse

1 Eclipse is also written in JAVA and is compatible. 2 Introduced JAVA Developer tools 3 Start with New-> Java Project, New-> class. 4 For command arguments, select the "(x) = Arguments" tab and the parameters required for "Program arguments:".

Network programming debugging

1 Use Wireshark 2 Specify Loopback as the interface for packets to yourself 3 If access is prohibited, su to an accessible person and start with wireshark & 4 For connections other than yourself, select a specific connection such as Ethernet: en0.


This article is about to be written. I will add it one by one.
"TCP / IP JAVA network programming understood from the basics" Chapter 2 Network programming by JAVA phttpd Exception collection in three places https://qiita.com/drafts/d1db78d3db0e90c0ff9f It is said that I don't like it because it is http / 0.9.

Passed the Information-Technology Engineers Examination Network Specialist https://qiita.com/kaizen_nagoya/items/407857392ca5c5677ee4

Communication emulator port https://qiita.com/drafts/ce505bbea4229b83e93b

Macintosh compatible "TCP / IP analyzer creation and packet analysis understood from the basics Linux / FreeBSD compatible" Tomohiro Odaka Ohmsha https://qiita.com/kaizen_nagoya/items/517411b42fc5ceabd581

Recommended Posts

"TCP / IP JAVA network programming understood from the basics" Compiled with Eclipse. Debug with Wireshark.
Find the address class and address type from the IP address with Java
Find the address class and address type from the IP address with Java [No. 2 decoction]
[Java] Set the time from the browser with jsoup
Try to implement TCP / IP + NIO with JAVA
java programming basics
[Tutorial] Download Eclipse → Run the application with Java (Pleiades)
I tried to implement TCP / IP + BIO with JAVA