Let's make a communication application in LAN Part 2 Display a window using JavaFX / Wait for socket reception

As a result of trying to improve the rhythm of life but failing to kill one day, the working time is also reduced.

By the way, this time we will make the UI part using JavaFX. Then let's make the standby part for communication. ** I don't have time! (what goes around comes around)**

Create a UI using JavaFX.

What is JavaFX?

In a nutshell, it's a "GUI drawing library for Java".

The feature is that the design is described in XML and CSS. In addition, it is very easy because you can use an application called SceneBuilder that can be designed by drag and drop for free. Somehow it can be used on the RIA platform.

For history and history, see see this wiki.

Make the main screen

Let's create the main window using the above Scene Builder.

While respecting Mr. IP Messanger, it looks like this

2018y06m13d_193810674.jpg

When this is output, an FXML format file is output. This time, we named it MainStage because it is the main screen (Stage). Let's create a directory for storage and put it in the source.

2018y06m13d_215409682.jpg

In the image above, MainStage.java is also stored, but this is a separately made one. Unlike C # Form, FXML is a file that records the structure (and event handler information), so It is necessary to create the java source to process separately.

It seems better to think that this area is divided like an MVC model.

It may be better to divide the files by control and view ...

I want to call it for the time being.

I want to call the created FXML for the time being.

I've researched various things, but I can only find a template for the JavaFX Project. Specifically, it only describes how to call the screen as it is from the _main () method.

This time I want to call it from another Main method, so I checked various things. The pages around here were helpful

Roughly speaking, Application is the main management of Stage (may be treated the same as window). Stage draws a scene. Scene is composed of various nodes stored in Parent. Is it like that?

The specific procedure is like this.

  1. Prepare a class that inherits javafx.application.Application.
  2. Feed the Application inheritance method prepared in javafx.application.Application.launch method.

Let's do it concretely.

1. Prepare a class that inherits javafx.application.Application.

This is easy.

MainStage.java



package kugui.owd.privateMessenger.stage;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainStage extends Application {
	
	@Override
	public void start(Stage stage) throws Exception{
		
		Parent root = FXMLLoader.load(getClass().getResource(
				getClass().getSimpleName() + ".fxml" )
				);
		
		Scene scene = new Scene(root);
		stage.setScene(scene);
		
		stage.show();
		
		return;
	}
	
	
}

The first thing to watch out for is overriding the start () method. It seems that the Application inheritance method passed to Application.launch () is executed according to its own life cycle.

It looks like this in simple writing

When calling
public void init()
public void start()
At the end
public void stop()

Although init () was originally implemented, start () is declared as an abstract class, so it will not build unless it is overridden. Basically, the initialization process should be done at the timing of start ().

The processing flow of the above code is as follows.

  1. Generate a Parent instance (parent) using the FXMKLoader.load () method based on the fxml file.
  2. Created based on the Parent instance that created the Scene instance (scene).
  3. Feed the created Scene instance (scene) to the Stage type parameter (stage) passed from Application.
  4. Use the stage.show () method to draw.

There are many samples that describe the processing in the same class around here, so it may not stumble so much.

2. Feed the Application inheritance method prepared in javafx.application.Application.launch method.

Since the inheritance class of Application has been prepared for the time being, set the caller next.

I added a little to ppa.java who left it in the previous article.

ppa.java


public class Ppa
{
	public static void main( String[] args) {
		System.out.println( "Good night World");
		
		Application.launch(MainStage.class, args);

		return;
	}
}

Application.launcher () seems to have the following restrictions.

In that case, you have to think a little about how to implement it.

By the way, this launch () is a synchronous method. If you write a process after this, it will be executed after the window is closed.

When I get here, I will display it for the time being.

2018y06m13d_234251791.jpg

Yup. There seems to be no problem so far.

Create a standby thread.

To be honest, I've never built a multi-threaded program. At least I have never seen it as an embedded device. Because it's okay to build in multiple processes, and there are more people who process information using IRQ than IPC. Oh, I haven't seen it in frameware.

I get the impression that you are using Morimori when it comes to games. It's a big deal, so let's get used to it here.

It's natural that things that haven't been done get better. But if you do it, there is nothing. The world is made of such things.

Prepare a class to run as a thread

How to make it work as a thread?

The usage of threads is roughly summarized as follows.

  1. Create a class that supports threading.
  2. Implement the process to call the thread.

It's simple. Let's make it.

1. Create a class that supports threading.

There are two ways to create a "threading-compatible class" in Java.

What's the difference? It seems that the operation is the same. Due to Java language specifications that do not allow multiple inheritance, use the Runnable interface when you want to inherit other classes and create threads! It seems that.

Now, create a new standby thread class RxThread by inheriting the Java.lang.Thread class. By the way, Thread class can be used without import.

RxThread.java


package kugui.owd.privateMessenger;


public class RxThread extends Thread{
	
	public void run() {
		
		int countPray = 0;
		
		try {
			while(true) {
				
				System.out.println(String.format("%2d : please let me see the good dream...", countPray));
				Thread.sleep(1000);
				
				countPray++;
			}
		}catch(InterruptedException ie ) {
			
			/* no operation */
			
		}catch(Exception ex) {
			
			ex.printStackTrace();
			
		}
		
		return;
	}
	
}

With loop processing to check the operation.

It seems that Thread also has something like a life cycle ... and the void run () method is called when the thread is executed. So, implement the run () method and describe the process. I searched for a description that would call other methods, but I couldn't find it, so it seems necessary to write to call from run () from initialization to termination processing.

2. Implement the process to call the thread.

Add the following processing to ppa.java's main () method

  1. Create an instance of the threaded class you created.
  2. Generate by feeding the threaded class that generated the Thread class.
  3. Call the thread.start () method to start the thread.
  4. Send an interrupt queue to the thread (after clearing the send window).

ppa.java


	public static void main( String[] args) {
		
		RxThread rxThread = new RxThread();
		Thread thread = new Thread(rxThread);
		
		System.out.println( "Good night World");
		
		thread.start();
		Application.launch(MainStage.class, args);
		thread.interrupt();
		
		System.out.println( "r u still awake?");
		
		return;
                }

When I check the operation, it looks like this.

2018y06m14d_012812748.jpg

_ ↓ window It looks like this when closed _

2018y06m14d_012833983.jpg

I'll add a little bit below about what I got stuck about threading so far.

What happens if the main thread exits while the thread is running an infinite loop?

To be honest, I thought, "If the parent thread disappears, the child thread will also disappear." Yes, it feels a little strong when it is written as "parents die but children live".

When I try to move it, it looks like this.

2018y06m14d_013214000.jpg

If you think about it carefully, it's like "parents are dead and children are zombies", so it's okay to be pitch black. Therefore, it is necessary to make sure that the child process is terminated before the parent process is terminated.

That's why I looked it up. How to terminate a thread. Then I found that a method called Thread.stop () was implemented.

When I write this, Eclipse is sick this time.

Even the strikethrough is put out like thread. ~~ stop ~~ (). why? If you think this method, deprecated method. Details: Oracle --Documentation Class Thread (Japanese) --See stop () section

To cancel the wait, use the Thread.interrupt () method and let the thread end the thread itself. By the way, suspend () that suspends Thread operation and resume () that resumes it are also deprecated. The reason is "because it is easy to cause deadlock!" Basically, it seems that interference with other threads is limited to interrupt queues using thread.interrupt ().

Well, if you make the control system disjointed, you will be able to strangle yourself later.

Do I have to create a Thread instance even though I inherit Thread?

I tried this and it works fine. If it's a pattern class that implements the Runnable interface, you'll have to do it here. (start () method or)

Make a standby process

Since this time is Socket communication, communication using webSocket.

Hmmm, how do you explain Socket communication in an easy-to-understand manner? Since there is a data storage area that is also used as a port used for sending and receiving data, An image that specifies the port to be used during standby and monitors it.

When I looked it up, it seems that java socket communication is very easy to make.

Practice immediately.

Delete the process of RxThread.run () that was kneaded above once and change it to standby process

RxThrea.java


package kugui.owd.privateMessenger;

import java.io.IOException;
import java.net.*;

public class RxThread extends Thread{
	
	public void run() {
		
		int port = 80; //For test.
		ServerSocket sSocket;
		
		try {
			
			sSocket = new ServerSocket(port);
			sSocket.accept();
			
			System.out.println( String.format("port %d : socket accept", port));
			
		}catch(IOException ioE) {
			
			ioE.printStackTrace();
			
		}
		
		if( sSocket != null ){
			sSocket.Close();
		}

		System.out.println( "finished");
		
		return;
	}
}

Start immediately after assembling. 2018y06m14d_025627764.jpg

Access localhost with a browser to send data to port 80.

What is localhost? The name you use to access yourself. Specifically, the domain name to access the IP address of 127.0.0.1.
Why browser? The number of ports is finite (65536), and the frequently used port is known as a well-known port. Since "port 80 is a port accessed by HTTP", you can send an information acquisition request to port 80 simply by specifying localhost with a browser. If you want to access other ports with a browser, specify: << port number >> after the address / domain name. It's usually repelled.

The result is this. 2018y06m14d_030827575.jpg


I was able to stand by without any problems, so that's it for today. Today's operation is ... I got confused by restarting windows on the way, but assuming about 2 hours ... 06:45:53.

Recommended Posts