Asynchronous communication between processes through Socket (2)

xiaoxiao2021-03-06  63

Step 2: Write a Socket Asynchronous Communication SignalsocketthRead

Package com.hode.thread;

/ ** * @Author Tailbo child * * Need to use the SOCKET client in SignalsocketThread to get an asynchronous communication object must inherit this interface * / public interface dealWith {public void dealwith ();}

Package com.hode.thread;

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ConnectException; import Java.net.NetAddress; import java.net.serversocket; import java.net.socket;

/ ** * @Author Tailborn * * / public class signals {

public static int PORT = 8881; private static String CanIWorkNow = "CanIWorkNow"; private static String YouCanWork = "YouCanWork"; private static String MyWorkIsCompleted = "MyWorkIsComplete"; private static String IGetYouMessage = "IGetYouMessage"; private static String Shutdown = "Shutdown "" Private commitread commonread = NULL;

/ ** * * / public SignalSocketThread () {super ( "SignalSocketThread");} public SignalSocketThread (int port) {super ( "SignalSocketThread"); PORT = port;} public void threadStart () {start ();} public void run () {ServerSocket ();} public void ServerSocket () {ServerSocket s = null; Socket socket = null; try {InetAddress addr = InetAddress.getByName ( "localhost"); s = new ServerSocket (PORT, 1, addr ); System.out.println ("/ nStarted:" s); while (true) {// blocks uncle a connection occurs: system.out.println ("/ n Open port" port); socket = s. Accept (); system.out.println ("Connection ACCEPTED: Socket); BufferedReader IN = New BufferedReader (socket.getinputStream ())); // Output is Auto matically flushed // by PrintWriter: PrintWriter out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (socket.getOutputStream ())), true); String strIn = null; while ((strIn = in.readLine ()) = null!) {System.out.println ("Communication Process:"); if (Strin.Equals (Shutdown)) {System.out.println ("[Receive]:" Shutdown; Return;

} Else if (strin.equals (caniworknow)) {system.out.println ("[received]:" caniworknow; waitthread (); system.out.println ("[send]:" Youcanwork); OUT .println (YouCanwork);} else if (Strin.Equals (myworkiscompleted)) {system.out.println ("[received]:" myworkiscompleted); workthread (); system.out.println ("[Send]: " IGetyouMessage); out.println;}} Socket.close ();}} catch (ooException e) {} finally {Try {// ALWAYS Close The Two sockets ... if (socket! = Null) {System.out.println ("closing ..."); socket.close ();} if (s! = NULL) {s.close (); system.out.println ("server closed");}}}}}}}} public boolean clientsocket (String Question, string answer) {Boolean Bool = False; Socket socket = null; try {inetaddress addr = inetaddress.getbyname ("localhost"); system.out.println ("/ NADDR =" addr; socket = new socket (addr, port); system.out.println "socket =" socket); bufferedreader in =

new BufferedReader (new InputStreamReader (socket .getInputStream ())); // Output is automatically flushed // by PrintWriter: PrintWriter out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (socket.getOutputStream ())), true); System. Out.println ("/ N communication process:"); out.println (questions); system.out.println ("[Send]:" quothes); string strin = in.readline (); if (strin! = NULL) {system.out.println ("[received]:" Strin); if (strin.equals (answer) {bool = true;}}} catch (connexception e) {system.out.println (" Abnormal: " Port " Service does not exist ");} catch (ioException e) {} finally {try {} (socket! = Null) {// Always Close The Two SO CKETS ... System.Out.println ("Closing ..."); socket.close (); System.Out.println ("Close CLOSED");}}}}}}}}}} Return Bool } public boolean Shutdown () {return ClientSocket (Shutdown, "");} public boolean CanIWork () {return ClientSocket (CanIWorkNow, YouCanWork);} public boolean MyWorkIsCompleted () {return ClientSocket (MyWorkIsCompleted, IGetYouMessage);} public void serverBind (Strthread strthread) {this.commthread =

转载请注明原文地址:https://www.9cbs.com/read-119033.html

New Post(0)