Java loop to check for inputstream reader

carolblush

New Member
I have a server and client.java which can send and accept a message both ways via input and output stream but as soon as there is a connection a message is sent and then the server closes its self, so far I have put the buffered reader ect. in a finally block which is only activated when e == true (e for exit so I can stop it when I want) but it still stops the build after sending/receiving a message1st question how can I stop it from closing its self?2nd question how can I put input stream reader in a loop to continually test for input from client.java?Client.java\[code\]package client;import java.io.*;import java.net.*;import java.io.BufferedReader; // Variables declaration - do not modify /** * * @author **** */public class Client extends javax.swing.JFrame {; public Client() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jTextField1 = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jTextField1) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { jTextField1.setText(""); input = jTextField1.getText(); send(input); } int port = 1234; String hostname = "localhost"; String input,output; public void send(String text) { try { Socket skt = new Socket(hostname, port); /*Connects to server*/ BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream())); /*Reads from server*/ System.out.println("Server:" + in.readLine()); PrintWriter out = new PrintWriter(skt.getOutputStream(), true); out.println(text); /*Writes to server*/ out.close(); /*Closes all*/ in.close(); skt.close(); } catch(Exception e) { System.out.print("Error Connecting to Server\n"); } } public void startUP() { } public static void main(String args[]) { Client c = new Client(); c.startUP(); c.send("Server is online"); new Client().setVisible(true); } // Variables declaration - do not modify private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; // End of variables declaration }\[/code\]Server.java\[code\]package server;import java.io.*;import java.net.*;class Server { /* To send string to client use "out.print(data)" To use info sent from client use "in.readLine()" */ int port = 1234; String input,output; boolean e = false; String question = "how are you"; String answer = "i am fine"; public void send(String text) { ServerSocket srvr = null; Socket skt = null; PrintWriter out = null; BufferedReader in = null; try { srvr = new ServerSocket(port); skt = srvr.accept(); /*Waiting for Connection from client*/ System.out.println("Connection = true"); out = new PrintWriter(skt.getOutputStream(), true); out.println(text); /*Write/Send to Client*/ in = new BufferedReader(new InputStreamReader(skt.getInputStream())); /*Read from Client*/ System.out.println("Client:" + in.readLine()); input = in.readLine(); } catch( Exception e) { System.out.println("Error Connecting\n"); } finally { if(e == true){ try { out.close(); in.close(); skt.close(); /*Closes all*/ srvr.close(); } catch (IOException e) { } } } }public void testFor() { if(input.equals(question)){ send(answer); }} public static void main(String args[]) { Server s = new Server(); s.send("Client is online"); //sends a message to client // s.testFor(); }}\[/code\]
 
Back
Top