java - Server client application in javafx hanging at uncertain point in time -


following code of controller of javafx file of client , main.java of server. program hang @ different time intervals , not sure why or start.

if 1 of in way, highly appreciated.
******controller.java***:

public class controller {     private static socket socket;     @fxml     public textarea text_area1;     bufferedreader br;     bufferedwriter bw;     stage stage1;     @fxml     textfield friends_id;     @fxml     textfield user_text;     @fxml     textfield login_user_id;     @fxml     textfield register_user_id;     @fxml     passwordfield register_user_pass;     @fxml     passwordfield confirm_pass;     @fxml     passwordfield login_user_password;      public void logtheuser(actionevent e) {         if (socket == null)             makeconnectiontoserver();         system.out.println("log in pressed");         senddatatoserver("login");         senddatatoserver(login_user_id.gettext());         senddatatoserver(login_user_password.gettext());         if (readfromserver().equals("login success")) {             openconnectwindowforlogin(e);         } else {             alert alert = new alert(alert.alerttype.error);             alert.settitle("unidentified user");             alert.setheadertext("please check , reenter username , password");             alert.showandwait();         }     }      @fxml     public void registertheuser(actionevent event) {          stage root = (stage) ((node) event.getsource()).getscene().getwindow();         system.out.println("i working");         try {             fxmlloader fxmlloader = new fxmlloader(getclass().getresource("finalsignup.fxml"));             parent root1 = fxmlloader.load();             stage stage = new stage();             stage.setscene(new scene(root1, 500, 300));             stage.show();             system.out.println("i ran");          } catch (ioexception e) {             e.printstacktrace();         }         root.hide();      }      @fxml     public void getupdatefromserver() {         string s1 = "hi";         stringbuilder main_string = new stringbuilder("*****initial chat window box*****\n");         senddatatoserver("chat history");         while (!s1.equals("!!end of data pack!!")) {             s1 = readfromserver();             main_string.append("\n");             main_string.append(s1);         }         system.out.println("data textarea processed ");         int len = main_string.length();         main_string.delete(len - 24, len);         text_area1.settext(main_string.tostring());         text_area1.selectpositioncaret(text_area1.getlength());         text_area1.deselect();      }      public void returntomain(actionevent event) {         stage root = (stage) ((node) event.getsource()).getscene().getwindow();         try {             fxmlloader fxmlloader = new fxmlloader(getclass().getresource("sample.fxml"));             parent root1 = (parent) fxmlloader.load();             stage stage = new stage();             stage.setscene(new scene(root1, 500, 300));             stage.show();         } catch (ioexception e) {             e.printstacktrace();         }         root.hide();      }      @fxml     public void openchatwindow(actionevent event) {         try {             senddatatoserver(friends_id.gettext());             string str = readfromserver();             system.out.println(str);              if (str.equals("success")) {                 stage root = (stage) ((node) event.getsource()).getscene().getwindow();                 try {                     fxmlloader fxmlloader = new fxmlloader(getclass().getresource("chatwindow.fxml"));                     parent root1 = (parent) fxmlloader.load();                     stage stage = new stage();                     stage.setscene(new scene(root1, 600, 400));                     stage.show();                     stage.setoncloserequest(evt -> {                         alert alert = new alert(alert.alerttype.confirmation);                         alert.settitle("exit window!");                         alert.setheadertext("are sure want close application?");                         alert.showandwait().filter(r -> r != buttontype.ok).ifpresent(r->evt.consume());                     });                  } catch (exception e) {                     system.out.println(e.getmessage());                 } {                     root.hide();                 }             } else {                 alert alert = new alert(alert.alerttype.error);                 alert.settitle("unidentified user");                 alert.setheadertext("please check , reenter correct username friend");                 alert.showandwait();             }          } catch (exception e) {             system.out.println("error while entering friends id");         }      }       public void makeconnectiontoserver() {         try {             string host = "localhost";             int port = 5000;             inetaddress address = inetaddress.getbyname(host);             socket = new socket(address, port);             inputstream = socket.getinputstream();             inputstreamreader isr = new inputstreamreader(is);             outputstream os = socket.getoutputstream();             outputstreamwriter osw = new outputstreamwriter(os);             bw = new bufferedwriter(osw);             br = new bufferedreader(isr);          } catch (ioexception e) {             out.println("server error!");         }     }      public void senddatatoserver(string s) {         if (socket.isconnected()) {             try {                 outputstream os = socket.getoutputstream();                 outputstreamwriter osw = new outputstreamwriter(os);                 bw = new bufferedwriter(osw);                 if (bw!=null) {                     bw.write(s + "\n");                     system.out.println("sendtoserver inner loop");                     bw.flush();                 } else {                     system.out.println("write disconnected");                 }             } catch (exception e) {                 system.out.println(e.getmessage());             }         } else             system.out.println("not connected");     }      public string readfromserver() {         try {             inputstream = socket.getinputstream();             inputstreamreader isr = new inputstreamreader(is);             br = new bufferedreader(isr);             string str1 = br.readline();             return str1;         } catch (exception e) {             system.out.println("error while retrieving data server" + e.getmessage());         }         string str = "errors ";         return str;     }       }      public void openconnectwindow(actionevent event) {         if (socket == null)             makeconnectiontoserver();         if ((register_user_pass.gettext().equals(confirm_pass.gettext())) != true) {             alert alert = new alert(alert.alerttype.error);             alert.settitle("password mismatch");             alert.setheadertext("the confirmed password , prev. entered password weren't same");             alert.showandwait();             return;         }         senddatatoserver("register user");         senddatatoserver(register_user_id.gettext());         if (readfromserver().equals("already present user")) {             alert alert = new alert(alert.alerttype.error);             alert.settitle("invalid details");             alert.setheadertext("the user name taken");             alert.showandwait();             return;         }         senddatatoserver(register_user_pass.gettext());         stage root = (stage) ((node) event.getsource()).getscene().getwindow();         try {              fxmlloader fxmlloader = new fxmlloader(getclass().getresource("connectto.fxml"));             parent root1 = fxmlloader.load();             stage1 = new stage();             stage1.setscene(new scene(root1, 350, 200));             stage1.show();         } catch (ioexception e) {             e.printstacktrace();         }           root.hide();         alert alert = new alert(alert.alerttype.information);         alert.settitle("congratulations");         alert.setheadertext("you're verified user now!");         alert.showandwait();      }      public void openconnectwindowforlogin(actionevent event) {         stage root = (stage) ((node) event.getsource()).getscene().getwindow();         try {              fxmlloader fxmlloader = new fxmlloader(getclass().getresource("connectto.fxml"));             parent root1 = fxmlloader.load();             stage1 = new stage();             stage1.setscene(new scene(root1, 350, 200));             stage1.show();         } catch (ioexception e) {             e.printstacktrace();         }          root.hide();         alert alert = new alert(alert.alerttype.information);         alert.settitle("welcome");         alert.setheadertext("you can chat friends");         alert.showandwait();      } } 

the main.java of server follows:

public class main extends thread {     private static socket csocket;     private static statement statement;     private static statement statement1;     private static connection conn;     private static connection conn1;     public bufferedwriter bw;     public bufferedreader br;     private socket socket;      main(socket socket) {         this.socket = socket;     }      public static void main(string[] args) {          system.out.println("opened database successfully");         try {             conn1 = drivermanager.getconnection("jdbc:sqlite:c:\\users\\pranav\\ideaprojects\\tryingwithjdbc\\data.db");             statement1 = conn1.createstatement();             statement1.execute("create table if not exists chattable(id text, chatwith text, chat text, date datetime)"); drivermanager.getconnection("jdbc:sqlite:c:\\users\\pranav\\ideaprojects\\tryingwithjdbc\\data.db"); text, password text)");             } catch (exception e) {                 system.out.println(e.getmessage());             }             try {                 int port = 5000;                 serversocket serversocket = new serversocket(port);                 system.out.println("server started , listening port 25000");                 while (true) {                     csocket = serversocket.accept();                     main main = new main(csocket);                     main.start();                 }             } catch (exception e) {                 system.out.println(e.getmessage());             }      }      public boolean checkdata(string s) {         system.out.println("checking data | " + s);         try {             string str = br.readline();             system.out.println("data | " + str + " | " + str.equals(s));             if (str.equals(s))                 return true;             else                 return false;         } catch (exception e) {             system.out.println("error while making streams connection!");         }         return false;     }      public boolean data_ispresent(string s, resultset rs) {         try {             system.out.println("in data present with: " + s);             arraylist<string> str = new arraylist<>();             while (rs.next()) {                 str.add(rs.getstring(1));             }             if (str.contains(s)) {                 system.out.println(s + " match!");                 return true;             } else {                 system.out.println(s + " wasn't match!");                 return false;             }         } catch (exception e) {             system.out.println(e.getmessage());         }         return false;     }      public void getchat(string username, string friend) {         try {             statement1.execute("select * chattable order date");             resultset rs = statement1.executequery("select * chattable order date");             while (rs.next()) {                 string inuser = rs.getstring(1);                 string infriend = rs.getstring(2);                 if ((inuser.equals(username) & infriend.equals(friend)) | (inuser.equals(friend) & infriend.equals(username))) {                     {                         senddatatoclient(rs.getstring(3));                     }                  }             }             system.out.println("string processed");             senddatatoclient("!!end of data pack!!");             rs.close();             statement1.execute("select * chattable order date");         } catch (exception e) {             system.out.println(e.getmessage());         }     }      public string getdatafromclient() {         string str1 = "exit";         try {             string str = br.readline();             return str;         } catch (exception e) {             system.out.println("error while making streams connection!");         }         return str1;     }      public void senddatatoclient(string s) {         try {             stringbuilder s1 = new stringbuilder(s);             s1.append("\n");             bw.write(s1.tostring());             bw.flush();         } catch (exception e) {             system.out.println("error while sending data client");         }     }      public void savechat(string username, string friend, string msg) {         try {             stringbuilder actual_chat = new stringbuilder();             actual_chat.append(username);             actual_chat.append(": ");             actual_chat.append(msg);             java.sql.timestamp date = new java.sql.timestamp(new java.util.date().gettime());             statement1.execute("insert 'chattable' values ('" + username + "','" + friend + "','" + actual_chat + "','" + date + "')");         } catch (exception e) {             system.out.println(e.getmessage());         }      }      public void run() {         string user_name;         string password;         string friends_name;         resultset id_set;         resultset pass_set;          try {              try {                  outputstream os = socket.getoutputstream();                 outputstreamwriter osw = new outputstreamwriter(os);                 inputstream = socket.getinputstream();                 inputstreamreader isr = new inputstreamreader(is);                 bw = new bufferedwriter(osw);                 br = new bufferedreader(isr);                 while (true) {                     system.out.println("inside first while");                     if (checkdata("login")) {                         //code login                         system.out.println("inside login code");                         user_name = getdatafromclient();                         password = getdatafromclient();                         try {                             id_set = statement1.executequery("select id userdata");                             boolean bool = data_ispresent(user_name, id_set);                             id_set.close();                             pass_set = statement1.executequery("select password userdata");                             boolean bool2 = data_ispresent(password, pass_set);                             pass_set.close();                             if (bool & bool2) {                                 system.out.println("executed this");                                 senddatatoclient("login success");                                 break;                             } else {                                 senddatatoclient("login failed");                             }                         } catch (exception e) {                             system.out.println(e.getmessage() + "in login code itself");                         }                     } else {                         user_name = getdatafromclient();                         boolean bool3 = false;                         try {                              id_set = statement1.executequery("select id userdata");                             bool3 = data_ispresent(user_name, id_set);                             id_set.close();                         } catch (exception e) {                             system.out.println(e.getmessage());                         }                         system.out.println("line0 " + user_name);                          if (bool3)                             senddatatoclient("already present user");                         else {                             senddatatoclient("user verified");                             string pass = getdatafromclient();                             system.out.println("line1 " + pass);                                      try {                                 statement1.execute("insert 'userdata' values ('" + user_name + "', '" + pass + "')");                                 catch (exception e) {                                 system.out.println(e.getmessage());                             }                             break;                         }                     }                 }                 while (true) {                     system.out.println("inside loop of connect window");                     friends_name = getdatafromclient();                     boolean bool3 = false;                     try {                          id_set = statement1.executequery("select id userdata");                         bool3 = data_ispresent(friends_name, id_set);                         id_set.close();                     } catch (exception e) {                         system.out.println(e.getmessage());                     }                     if (bool3)                         break;                     senddatatoclient("fail");                 }                 senddatatoclient("success");                  while (true) {                     string str1 = getdatafromclient();                     string s2;                     if (str1.equals("exit")) {                         system.out.println(str1);                         system.out.println("exiting server");                         break;                     } else if (str1.equals("chat history")) {                         system.out.println("chat history called");                         getchat(user_name, friends_name);                         system.out.println("chat history sent");                     } else if (str1.equals("just chat")) {                         s2 = getdatafromclient();                         savechat(user_name, friends_name, s2.substring(1));                         system.out.println("was executed");                         system.out.println("message received client " + s2);                     } else {                         system.out.println("invalid request client");                     }                  }             } catch (ioexception e) {                 e.printstacktrace();             } {                 try {                     socket.close();                     system.out.println("closing connection");                 } catch (exception e) {                     system.out.println("error closing!");                 }              }         } catch (exception e) {             if (socket != null)                 socket = null;             system.out.println(e.getmessage());         }     } } 

****the fxml files follow: ******** **chatwindow.fxml****

<gridpane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller">    <columnconstraints>       <columnconstraints />       <columnconstraints />    </columnconstraints>    <rowconstraints>       <rowconstraints />       <rowconstraints />       <rowconstraints />       <rowconstraints />       <rowconstraints />    </rowconstraints>    <children>       <label alignment="center" scalex="2.0" scaley="1.3" text="chat window" textalignment="center" textfill="#2336ff24" gridpane.columnspan="2">          <font>             <font size="25.0" />          </font>          <gridpane.margin>             <insets bottom="40.0" left="130.0" />          </gridpane.margin>       </label>       <textarea fx:id="text_area1" editable="false" prefheight="200.0" prefwidth="200.0" scalex="2.2" scaley="1.2" style="-fx-background-color: green; -fx-text-fill: black; visibility: true;" text="*************chat history*****************" gridpane.rowindex="1" gridpane.rowspan="2">          <gridpane.margin>             <insets left="120.0" />          </gridpane.margin></textarea>       <textfield fx:id="user_text" scalex="1.81" style="-fx-background-color:#c8c9d3; -fx-text-fill:white" gridpane.columnspan="2" gridpane.rowindex="4">          <gridpane.margin>             <insets left="130.0" right="130.0" />          </gridpane.margin>          <opaqueinsets>             <insets />          </opaqueinsets></textfield>       <button mnemonicparsing="false" onaction="#sendtoserver" scalex="1.3" text="send!" textfill="#079dd3" gridpane.columnindex="1" gridpane.rowindex="4">          <font>             <font name="system bold" size="12.0" />          </font>          <gridpane.margin>             <insets left="70.0" />          </gridpane.margin>       </button>       <button fx:id="refresh_button" mnemonicparsing="false" onaction="#getupdatefromserver" text="refresh" textfill="#11ae33" gridpane.rowindex="4" />    </children> </gridpane> 

sample.fxml

<gridpane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller">    <columnconstraints>       <columnconstraints />       <columnconstraints />    </columnconstraints>    <rowconstraints>       <rowconstraints />       <rowconstraints />       <rowconstraints />    </rowconstraints>    <children>       <label contentdisplay="right" linespacing="4.0" text="user name">          <font>             <font size="14.0" />          </font>       </label>       <label alignment="bottom_right" text="password" gridpane.rowindex="1">          <font>             <font size="15.0" />          </font>       </label>       <passwordfield fx:id="login_user_password" gridpane.columnindex="1" gridpane.rowindex="1" />       <textfield fx:id="login_user_id" gridpane.columnindex="1" />       <button fx:id="log_in_button" mnemonicparsing="false" onaction="#logtheuser" scalex="1.3" text="log in" gridpane.rowindex="2">          <font>             <font size="16.0" />          </font>          <gridpane.margin>             <insets left="5.0" top="25.0" />          </gridpane.margin></button>       <button fx:id="new_user_button" onaction="#registertheuser" mnemonicparsing="false" scalex="1.1" text="new user?" textalignment="center" gridpane.columnindex="1" gridpane.rowindex="2">          <font>             <font size="17.0" />          </font>          <gridpane.margin>             <insets left="27.0" top="25.0" />          </gridpane.margin>       </button>    </children> </gridpane> 

****connectto.fxml***

<gridpane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller">     <columnconstraints>         <columnconstraints />         <columnconstraints />     </columnconstraints>     <rowconstraints>         <rowconstraints />         <rowconstraints />         <rowconstraints />         <rowconstraints />         <rowconstraints />     </rowconstraints>     <children>         <label scaley="1.6" text="chat with:" gridpane.rowindex="1">             <font>                 <font size="15.0" />             </font>             <gridpane.margin>                 <insets left="25.0" top="30.0" />             </gridpane.margin></label>         <textfield fx:id="friends_id" scalex="1.2" scaley="1.3" text="enter friends id" gridpane.columnindex="1" gridpane.rowindex="1">             <padding>                 <insets left="5.0" />             </padding>             <gridpane.margin>                 <insets left="25.0" top="30.0" />             </gridpane.margin>             <opaqueinsets>                 <insets />             </opaqueinsets>         </textfield>         <button fx:id="connect_button" mnemonicparsing="false" onaction="#openchatwindow" scalex="1.7" scaley="1.15" text="connect!" textfill="#25d35f" gridpane.columnspan="2" gridpane.rowindex="2">             <gridpane.margin>                 <insets left="100.0" top="40.0" />             </gridpane.margin>             <font>                 <font name="system bold" size="12.0" />             </font>         </button>         <label text="welcome" textfill="#506dff" gridpane.columnspan="2">             <gridpane.margin>                 <insets left="69.0" />             </gridpane.margin>             <font>                 <font size="23.0" />             </font>         </label>     </children> </gridpane> 

unable upload code finalsignup.fxml because of character limit constraint.

the javafx main loads sample.fxml;

it great if suggest me how automatically , continually update/refresh text area of threads unable well.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -