Transaction communication and listening control command based on "single-port-long connection"

zhaozj2021-02-16  81

Import java.net. *; import java.io. *; import java.net. *; import java.io. *; import com.nantian.iclient.log. *; import com.nantian.iclient.plugininterface. *; Import com.nantian.iclient.atm.sdb. *; import com.nantian.util.utility

public class Communication {private static Communication instance; private static Socket sock; private static String message; private static LAAdapter lap; private static recvThread recvthread; private static boolean available = true;

Private static final string comm = "iClient.communication"; private static final string fs = new string (new byte [] {(byte) 0x1c}); // domain separator; private static final string hostcmd_trscode = "6093";

// returns the unique solid columns static synchronized public Communication getInstance (String address, String port, LAAdapter lap) throws IOException {Logger.info (COMM, "Get a Communication Instance ..."); if (instance == null) { Instance = New Communication (Address, Port, LAP);} Return Instance;

Private Communication (String Address, String Port, Laadapter LAP) throws ioException {logger.info (Comm, "ClientINIT Server IP: [" Address "] Port: [" port "]"); this.lap = lap This.Sock = new socket (address); this.sock.setsotimeout (500); recvthread = new recvthread (this); recvthread.start (); logger.info (Comm, "Clientinit Finished ");

/ ** * Send a message and wait (timeout setting: 60000 milliseconds) Returns the back machine return. * @Param msgTxt String * @throws Exception * @return String * / public synchronized String sendText (String msgTxt) throws Exception {Logger.info (COMM, "Client Ready to Sending."); While (! Available) {Logger.info (Comm, "Client Ready To Sending ...");} Available = false; Logger.info (Comm, "Client senting ..."); this.Message = ""; printwriter to_server_stream = new printstreamwriter (New OutputStreamwriter new BufferedOutputStream (sock.getOutputStream ()))); to_server_stream.print (msgTxt); to_server_stream.flush (); available = true; logger.info (COMM, "Client Send: [" msgTxt "], Waiting for receiving ... "); Wait (60000); // Waiting for Recvthread to receive returns Logger.info (Comm," Client Received: [" THIS.MESSAGE "]. "); Returnims.Message;

/ ** * Send a message, not waiting for the front machine to return. * @Param msgTxt String * @throws Exception * @return String * / public synchronized String sendTextS (String msgTxt) throws Exception {Logger.info (COMM, "Client Ready to Sending."); While (! Available) {Logger.info (Comm, "Client Ready To Sending ...");} Available = false; Logger.info (Comm, "Client senting ..."); this.Message = ""; printwriter to_server_stream = new printstreamwriter (New OutputStreamwriter new BufferedOutputStream (sock.getOutputStream ()))); to_server_stream.print (msgTxt); to_server_stream.flush (); available = true; logger.info (COMM, "Client Send:". [ " msgTxt ]"); // Wait (60000); //logger.info(COMM, "Client Received: [" this.Message "]."); return "; //this.Message;} / ** * Receive message * / Public synchronized void recvtext () {while (! Available) {} available = false;

char recvbuffer [] = new char [10000]; int recvlength = 0; try {//System.out.println("Client Receiving ... "); BufferedReader from_server_stream = new BufferedReader (new InputStreamReader (sock.getInputStream ()) ); Recvlength = from_server_stream.read (recvbuffer);} catch (exception e) {//system.out.println (} string recntring = new string (recnuffer); available = true; // distinguish transaction return and control Command if (Recvlength> 0) {system.out.println ("Client Received: [" RecvString "] [" Recvlength "]"); string [] fields = utility.splittext12 (Recvstring, "[" FS "]"); IF (Fields [10] .Equals (this.hostcmd_TRSCode)) {system.out.println ("Client Received A Host Command."); New Cmdexecutor (this, Fields) .start ();} Else {System.Out.println ("Client Received A Result-of-Transcation); this.Message = Recvstring; Notify ();}}} public static laadapter getLap () { Return lap;

}

================== recvthread.java ===================

Package com.nantian.ICLIENT.ATM.SDB;

Public Class Recvthread Extends Thread {Private Communication COMM

Public Recvthread (Communication Comm) {this.comm = comm;}

Public void Run () {while (true) {comm.recvtext ();}}}

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

New Post(0)