java - ObjectInput and Output streams are not being accepted by client/server -


i have 1 program 2 serverside processes. 1 server sends arraylist client. other server first takes string client , finds proper record corresponding id , sends record back.

i'm having problems second server process. see println statement below says "gets stuck here". that's program hangs.

import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.net.serversocket; import java.net.socket; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.statement; import java.util.arraylist;   public class custserver{     public static void main(string args[]) throws ioexception, classnotfoundexception{      serversocket serversocket1 = null;      serversocket serversocket2 = null;      arraylist<customer> list = null;          string drivername = "sun.jdbc.odbc.jdbcodbcdriver";         string connectionurl = "jdbc:odbc:customer";         connection con = null;         statement stmt = null;         string sqlstatement = "select id, customername, address," +                 " city, state, zip, orders customer";         resultset rs = null;         try {             class.forname(drivername).newinstance();             con = drivermanager.getconnection(connectionurl);             stmt = con.createstatement();             rs = stmt.executequery(sqlstatement);             list = new arraylist<customer>(39);             while(rs.next()){                 list.add(                     new customer(                         rs.getstring(1),                          rs.getstring(2),                          rs.getstring(3),                           rs.getstring(4), rs.getstring(5), rs.getstring(6),                          rs.getstring(7)));             }              rs.close();             stmt.close();             con.close();         }catch (exception ex) { ex.printstacktrace(); }      try {         serversocket1 = new serversocket(8889);         system.out.println("customer db server running @ port 8889");      } catch (ioexception e){         system.out.println("could not listen on port: 8889.");         system.exit(1);     }      socket clientsocket = null;     try{         clientsocket = serversocket1.accept();      }catch (ioexception e){         system.out.println("server 1: accept failed.");         system.exit(1);     }     //a server program returns list of customer ids database.     objectoutputstream out = new objectoutputstream(clientsocket.getoutputstream());     out.writeobject(list);     system.out.println(list.size() + "customer ids (list) sent.");      out.flush();     out.close();     clientsocket.close();     serversocket1.close();     //second server process:     //takes customer  d , returns customer record      try {         serversocket2 = new serversocket(8888);         system.out.println("customer db server running @ port 8888");      } catch (ioexception e){         system.out.println("could not listen on port: 8889.");         system.exit(1);     }     try{         clientsocket = serversocket2.accept();      }catch (ioexception e){         system.out.println("server 2: accept failed.");         system.exit(1);     }      system.out.println("gets stuck here."); //<<<<<here     objectinputstream in = new objectinputstream(clientsocket.getinputstream());     objectoutputstream out1 = new objectoutputstream(clientsocket.getoutputstream());     string id = (string) in.readobject();     string record = null;      for(customer c : list){         if(c.getid().equals(id))             record = c.tostring();     }      out1.writeobject(record);      out1.flush();     out1.close();     in.close();     clientsocket.close();     serversocket2.close();     } } 

clientside:

    import java.awt.borderlayout; import java.awt.dimension; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.net.socket; import java.net.unknownhostexception; import java.util.arraylist;  import javax.swing.defaultlistmodel; import javax.swing.jframe; import javax.swing.jlist; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextfield; import javax.swing.event.listselectionevent; import javax.swing.event.listselectionlistener;   public class clientgui extends jframe implements listselectionlistener{      //gui components     jlist jlist = null;     string requestid = null; //assigned selected id in jlist.      jscrollpane scpane = null;     jtextfield field = null;     jpanel pane = null;     defaultlistmodel<string> listmodel = null;     arraylist<string> idlist = null;      //client stuff:     socket sock1 = null;     socket sock2 = null;     objectinputstream in = null;     objectoutputstream out = null;      public clientgui() throws classnotfoundexception{         //get list of ids server1         try{             sock1 = new socket("fahadahmed-pc", 8889);              in = new objectinputstream(sock1.getinputstream());             idlist = new arraylist<string>(29);             arraylist<customer> custlist = null;             custlist = (arraylist<customer>) in.readobject();             for(customer c : custlist){                 idlist.add(c.getid());             }              in.close();             sock1.close();              sock2 = new socket("fahadahmed-pc", 8888);             }catch(unknownhostexception e) {             system.err.println("don't know host: fahadahmed-pc");             system.exit(1);         }catch(ioexception e){             system.err.println(e);             system.exit(1);         }          //setup gui         jlist = new jlist(idlist.toarray());         jlist.setvisiblerowcount(10);         scpane = new jscrollpane(jlist);         jlist.addlistselectionlistener(this);         pane = new jpanel(new borderlayout());         pane.setpreferredsize(new dimension(300, 300));         field = new jtextfield(29);         field.seteditable(false);         pane.add(scpane, borderlayout.page_start);         pane.add(field, borderlayout.page_end);          this.setcontentpane(pane);         this.setdefaultcloseoperation(jframe.exit_on_close);         this.pack();         this.setvisible(true);     }      public static void main(string args[]) throws classnotfoundexception{         clientgui gui = new clientgui();     }       @override     public void valuechanged(listselectionevent arg0) {         if(!arg0.getvalueisadjusting())             try {                 getrecord(jlist.getselectedvalue().tostring());             } catch (classnotfoundexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }            }      private void getrecord(string getid) throws classnotfoundexception {         try{             in = new objectinputstream(sock2.getinputstream());             out = new objectoutputstream(sock2.getoutputstream());             system.out.println(getid + "sent getrecord method");             out.writeobject(getid);              string rec = (string) in.readobject();              field.settext(rec);             out.flush();             out.close();             in.close();         }catch(unknownhostexception e) {             system.err.println("don't know host: fahadahmed-pc");             system.exit(1);         }catch(ioexception e){             system.err.println(e);             system.exit(1);         }     }   } 

you need create objectoutputstream before objectinputstream @ both ends.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -