Click here to Skip to main content
14,301,484 members
Rate this:
Please Sign up or sign in to vote.
See more:
basicaly i think i've done everything corectly,however the program suddenly stops running at bis.readline();,before this i had the server and client in separate classes,however it was getting stuck at cn.accept(),so while trying to fix i got to this code (its basicaly inside the action of the button "ok" that should initiate the connection to the server ,also textField 1 and 2 are the fields where the numbers are added,if youre wondering what's with all the system.out.prints ,it was just me trying to figure where the code stopped working.

What I have tried:

try {
    ServerSocket sServer = new ServerSocket(2121);
    Socket  cns = new Socket("localhost", 2121);
    System.out.println(cns.isConnected());
    Socket cn = sServer.accept();
    System.out.println(cns.isConnected());
    String a = textField1.getText();
    String b = textField2.getText();
    System.out.println("ok");
    BufferedReader bis = new BufferedReader(new InputStreamReader(cn.getInputStream()));
    BufferedOutputStream bos = new BufferedOutputStream(cn.getOutputStream());
    BufferedReader bist = new BufferedReader(new InputStreamReader(cns.getInputStream()));
    BufferedOutputStream bost = new BufferedOutputStream(cns.getOutputStream());
    System.out.println("ok");
    bost.write(a.getBytes());
    bost.write(b.getBytes());
    System.out.println("ok");

    String req = bis.readLine();
    String rew = bis.readLine();
    int sekta = Integer.parseInt(req);
    int sekts = Integer.parseInt(rew);
     System.out.println("ok");
    bos.write(sekta+sekts);
    String c = bist.readLine();

    JOptionPane.showMessageDialog(null, "result is" + c, "result", 300);


} catch (IOException ex) {
    Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
Posted
Updated 29-Aug-19 3:45am
Comments
Richard MacCutchan 29-Aug-19 8:55am
   
You should run this in the debugger to find out exactly what has happened at the point that the program stops.
Member 14566920 29-Aug-19 9:06am
   
well i started the debugger,put some breakpoints and the code stops at String req = bis.readLine();
maybe i dont know how to use the debugger corectly?
Richard MacCutchan 29-Aug-19 9:51am
   
What exactly do you mean by, "the code stops"? Remember we cannot see you screen so have no idea what state your application is in. Is it waiting for a message, has it crashed, has it produced an exception, etc.?
Member 14566920 29-Aug-19 9:56am
   
basicaly everything executes corectly untill bis.readLine() the program simply remains at that spot as if waiting for the message to read,although i already sent it in bost.write
Richard MacCutchan 29-Aug-19 10:19am
   
So it is clearly not receiving anything to read. it is impossible for us to guess why.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Your "pattern" is like connecting a garden hose to itself. Better go more standard.

Socket Programming in Java - GeeksforGeeks[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100