Home > Back-end >  About the communication problems between the child process
About the communication problems between the child process

Time:09-16

Recently in contact with the multi-process programming, content are two child processes to be able to communicate with each other, and then in the main process of the console can actually read the child to communicate with each other information, but the two child process can run independently is the exchange of data, with the main process open after two child not line,
1 (this is the child process contains two threads), a thread content is to calculate a file hash value and then sent to another child process, another thread is receiving another child process from the hash value,
 
Package ms;//the child 1 resourse. Jar (from)

The import org. Json. Simple. JSONObject;
Import the Java. IO. *;
The import java.net.Socket;
import java.util.Scanner;
The import java.net.InetAddress;
The import java.net.UnknownHostException;
Import the Java. Math. BigInteger;
Import the Java. Security. MessageDigest;

Public class Client1 {
Private static Socket Socket;

Public static Boolean connection_state=false;
Public static String md5Hash;

Public static void main (String [] args) {

while (! Connection_state) {
The connect ().

Try {
Thread.sleep(3000);
} the catch (Exception e) {
e.printStackTrace();
}
}
}

Private static void the connect () {
Try {

The socket=new socket (" 127.0.0.1 ", 5008);

Connection_state=true;

The ObjectOutputStream ohs=new ObjectOutputStream (socket. GetOutputStream ());//heartbeat
ObjectInputStream ois=new ObjectInputStream (socket. GetInputStream ());


New Thread (new Client1_listen (socket, ois)). The start ();

New Thread (new Client1_heart (socket, ohs). The start ();
} the catch (Exception e) {
e.printStackTrace();
Connection_state=false;
}
}

Public static String md5HashCode32 (InputStream fis) {
Try {
MessageDigest md=MessageDigest.getInstance("MD5");
Byte [] buffer=new byte [1024].
Int length=1;
While ((length=fis. Read (buffer, 0, 1024)).=1) {
Md. Update (buffer, 0, length);
}
fis.close();
Byte [] md5Bytes=md. Digest ();
StringBuffer hexValue=https://bbs.csdn.net/topics/new StringBuffer ();
for (int i=0; i Int val=((int) md5Bytes [I]) & amp; 0 XFF.
If (val & lt; 16) {
HexValue. Append (" 0 ");
}
HexValue. Append (Integer. ToHexString (val));
}
Return hexValue. ToString ();
} the catch (Exception e) {
e.printStackTrace();
return "";
}
}

Public static String md5HashCode32 (String filePath) throws FileNotFoundException {
FileInputStream fis=new FileInputStream (filePath);
Return md5HashCode32 (fis);
}

Public static String hash (String file) {
Try {
//String filePath=file;
String md5Hash=md5HashCode32 (file);
Return md5Hash;
} the catch (FileNotFoundException e) {
e.printStackTrace();
}

Return md5Hash;
}


}


The class Client1_listen implements Runnable {
Private Socket Socket;
Private ObjectInputStream ois.
Public String a;
Public static Boolean connection_state=true;
83247 a4c321c70e257ef6b295bfde436 String localhash=new String (" ");
Client1_listen (Socket Socket, ObjectInputStream ois) {
This. The socket=socket;
This. Ois=ois;
}

@ Override
Public void the run () {
Try {

While (connection_state) {
System. The out. Println (ois. ReadObject ());
A=String. The valueOf (ois. ReadObject ());
String hash=a.s ubstring (9, 41);
If (hash equals (localhash)) {
System. The out. Println (" internal validation by ");
}
Else {System. Out. Println (" internal verification through, System disconnected ");
//socket. ShutdownOutput ();
//socket. ShutdownInput ();
//Client. Shutdownclient ();
//heart_listen shutdownserve ();

Connection_state=false; }

}
} the catch (Exception e) {
e.printStackTrace();
}
}
}


The class Client1_heart implements Runnable {
Private Socket socket1;
Private ObjectOutputStream ohs.
Public String hashvol;
Client1_heart (Socket Socket, ObjectOutputStream oos) {
Enclosing socket1=socket;
This. Ohs=oos;
}

@ Override
Public void the run () {
Try {
System. Out.println (" heartbeat packets thread has started... ");
While (true) {

Thread.sleep (5000);
//System. Out. Println (" 111 ");
String hashvol=Client1. Hash (" E: \ \ Java \ \ ruanjian \ \ binary \ \ com sun. Java jdk8. Win32. X86_64_1. 8.0 v112 \ \ jre \ \ lib \ \ resources jar ");
JSONObject object=new JSONObject ();
Object. The put (" Hash ", hashvol);
Ohs. WriteObject (object);
Ohs. Flush ();
}
} the catch (Exception e) {
e.printStackTrace();
}
}
}


This is the child of 2, content as well as the child process 1
 
Package ms;//the child 2 rt. Jar (main)

The import org. Json. Simple. JSONObject;
Import the Java. IO. ObjectInputStream;
Import the Java. IO. ObjectOutputStream;
The import java.net.ServerSocket;
The import java.net.Socket;
import java.util.Scanner;
import java.io.IOException;
The import java.net.InetAddress;
The import java.net.UnknownHostException;

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related