0

Hello I need some quick help with this Server/Client socket program I am writing in Java. Everything works as intended when entering standard input on the client side. The server responds with the correct data every time. But when I send data to the Server using the actionListener, nothing is returned. The data is being sent over and read by the Server and can be printed server-side, it just won't come back to the Client.

So this must be an issue with the formatting or type of data being sent from the actionListener (I don't know enough about streams unfortunately), or it is an issue with the stream on the server side?

Any help is most appreciated!

public class Serv {
    public static void main(String[] args) throws IOException {
  ServerSocket serverSocket = null;
        try {
             serverSocket = new ServerSocket(8889);
             System.out.println("Waiting for client...");
        } catch (IOException e) {
            System.err.println("Could not listen on port: 8889.");
            System.exit(1);
        }
            Socket clientSocket = serverSocket.accept();
   BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
   PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
   String inputLine, outputLine;
   Protocol p = new Protocol();
   out.println("welcome");
        while ((inputLine = in.readLine()) != null) {
       outputLine = p.processInput(inputLine);    
    System.out.println("Input: "+inputLine + "\nOutput: "+outputLine);
             out.println(outputLine);
   if (outputLine.equals("Exit")) { break; }
  }
  out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}





public class Cl extends JFrame {
 
 public static PrintWriter out = null; 
 public static String fromUser;  
  
 public static void Client() {  
  saveAnswer.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
     ButtonModel b = group.getSelection();
     if (b.getActionCommand() == "A") { sendAnswer = radioA.getText(); }
     if (b.getActionCommand() == "B") { sendAnswer = radioB.getText(); }
     if (b.getActionCommand() == "C") { sendAnswer = radioC.getText(); }
     String data = "÷" + sendAnswer;
     out.println(data);
    }
  });
 }

 public static void main(String[] args) throws IOException {

        Socket Socket = null;
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
  boolean checkOpen = false;
  Socket clientS = new Socket("localhost", 8889);
        out = new PrintWriter(clientS.getOutputStream(), true);
  BufferedReader in = new BufferedReader(new InputStreamReader(clientS.getInputStream()));
  String fromServer;
  
        while ((fromServer = in.readLine()) != null) {
   if (fromServer.startsWith("®")) {
    if (checkOpen == false) { Cl.Client(); checkOpen = true; }
    qA.splitter(fromServer);
   }
            if (fromServer.equals("Exit")) { break; }
      fromUser = stdIn.readLine();
   if (fromUser != null) {
                System.out.println("Client: " + fromUser);
                out.println(fromUser);
    }
    else { System.out.println("trouble"); }
        }
        out.close();
        in.close();
        stdIn.close();
        Socket.close();
     }
}
Maguire
  • 7
  • 2
  • Sorry I forgot to add, I removed all non-essential data as I am 99% sure I have narrowed the problem down to something within this code. I can post the entire thing if it will help tho! – Maguire Mar 09 '15 at 21:49
  • Code that is not well formatted is often code that folks won't read. Consider trying to post well-formatted readable code. Best if you could create and post an [mcve](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Mar 09 '15 at 21:55
  • 1
    Why are you reading from stdin in a Swing program? – user207421 Mar 09 '15 at 21:57

2 Answers2

1

In the server, you're sending one line and then reading from the client until EOS.

In the client, you're reading from the server until EOS and then sending whatever the user types.

Your protocol doesn't make sense. All you have here is a deadlock.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for the advice, I think I need to do some more reading on the basics of streams – Maguire Mar 09 '15 at 22:10
  • EOS (end of stream) means that the peer has disconnected. `readLine()` returns null at EOS. Not just when there is temporarily no more data. If you want one line, read one line. – user207421 Mar 09 '15 at 22:12
-1

You might need to do out.flush() after writing the response on the server side.

Balint Domokos
  • 1,021
  • 8
  • 12