0

Hi i have written a server program to receive ISO8583-93 version requests process them and send response.I will be receiving continuous requests.It works fine ,but if socket is idle when the next request comes, server is not reading. Please find below code snippet

SERVER :

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();
 }
}

THREAD:

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();
   }

  }

 }
jan
  • 55
  • 7
  • I guess you mean *idle*, not *ideal* – geert3 Dec 09 '14 at 10:53
  • How many threads have you running in parallel at the moment your server hangs? What do you mean with "server is idle when next request comes"? When is the server not idle? – geert3 Dec 09 '14 at 11:00
  • when server doesnot receive request for long time (idle) when new request is initiated it hangs .But when i initiate a request from my local machine it works(both local machine and server are in LAN) – jan Dec 10 '14 at 05:15
  • Most plausible would be that the `accept` is not triggered on new connections. I've seen this happen when our firewall was dropping idle connections. Can you check your network infrastructure? – geert3 Dec 10 '14 at 09:43
  • It worked after we implemented set sockettimeout. – jan Jul 02 '16 at 09:41
  • 'IUf socket is idle when the next request comes, server is not reading' is meaningless. Try agan. – user207421 Sep 17 '16 at 11:00
  • @EJP, it happened when i do not receive request for long time read was blocked further.But after setting setsockettimeout the issue was resolved.No question will be useless until we face such issues. – jan Sep 17 '16 at 11:45
  • 'Do not receive request for long time' and 'read was blocked' are the same thing. Reads block forever in blocking mode unless you have set a read timeout ir data arrives. The word I used was not 'useless' but 'meaningless'. – user207421 Nov 28 '16 at 02:51

1 Answers1

-3

It worked after we implemented set sockettimeout.

jan
  • 55
  • 7