簡體   English   中英

空閑時間后,套接字掛起:服務器-客戶端Java程序

[英]socket hangs after idle time :server - client java program

您好,我編寫了一個服務器程序來接收ISO8583-93版本的請求,然后處理它們並發送響應。我將接收連續的請求。它可以正常工作,但是如果下一個請求到來時套接字空閑,則服務器無法讀取。 請在下面找到代碼片段

服務器:

 public class MBServ { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; boolean listening = true; String request_date = null; String request_time = null; try { serverSocket = new ServerSocket(7777); } catch (IOException e) { System.err.println("Could not listen on port: 7777."); System.exit(-1); } while (listening) { new MBServT(serverSocket.accept()).start(); } serverSocket.close(); } } 

線:

 public class MBServT extends Thread { private Socket socket = null; Logger log = Logger.getLogger(MBServT .class.getName()); public MBServT(Socket socket) throws FileNotFoundException, IOException { super("MBServT"); this.socket = socket; } public void run() { String inputLine = ""; String msgType = null; int response = 12; String outwardMsg = null; BufferedReader buffReaderObj = null; // String ip = "172.30.12.69"; String stanNo = "0"; boolean ISSUBFIELDPARSING = false; GenericPackager packager; try { packager = new GenericPackager("basicparse.xml"); BufferedReader is = new BufferedReader(new InputStreamReader(socket .getInputStream(), "ISO8859_1")); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( socket.getOutputStream(), "ISO8859_1")); String strBuf = null; int length = 4; char[] chrBuf = new char[length]; int cnt = 0; while (true) { socket.setKeepAlive(true); int value = 0; int ret = 0; ret = is.read(chrBuf, 0, chrBuf.length); if (-1 == ret) { log.error("nothing to read closing socket"); try{ socket.close(); } catch(Exception e){ System.out.println("Error in socket.close: " +e.toString()); } throw new Exception("Read Error: Socket closed"); } strBuf = new String(chrBuf); chrBuf = new char[Integer.parseInt(strBuf)]; is.read(chrBuf, 0, chrBuf.length); strBuf = new String(chrBuf); chrBuf = null; chrBuf = new char[4]; value = 0; /*writing response*/ Runnable respThread = new MBServRespT(writer, fieldList, "threadname"); ((Thread) respThread).start(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Error::" + e.toString()); } finally { try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

在實現set sockettimeout之后,它起作用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM