client server communication problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    #1

    client server communication problem

    Code:
    package zad41;
    
    
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.net.*;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class SimpleClient {
    	 String action=null;
    	 String lastname=null;
         String number_with=null;
         PhoneDirectory phonedirectory = new PhoneDirectory("numbers.txt");
     	public SimpleClient(){
    
            
            
     		search.addActionListener( new ActionListener() {
     		public void actionPerformed(ActionEvent e) {
     			   System.out.println("Get");
     			   action="get";
     			   lastname=(String)namesList1.getSelectedItem();
     		}});
     		        
     		add.addActionListener( new ActionListener() {
     		public void actionPerformed(ActionEvent e) {
     			 System.out.println("Add");
     		     action="add";
     		     lastname=add_name.getText();
     		}});    
     		        
     		replace.addActionListener( new ActionListener() {
     		public void actionPerformed(ActionEvent e) {
     			   System.out.println("Replace");
     		       action="replace";
     		       lastname=(String)namesList2.getSelectedItem();
     		       number_with=replace_with.getText();
     		}});   
     		bye.addActionListener( new ActionListener() {
     			public void actionPerformed(ActionEvent e) {
     				   System.out.println("Bye");
     			       action="Bye";
     			       }}); 	
     	}
     	JButton    search    = new JButton("Wyszukaj");
     	JButton    add    = new JButton("Dodaj");
     	JButton    replace    = new JButton("Zastap");
     	JButton    bye    = new JButton("Bye");
     	JTextField add_name     = new JTextField(20);
     	JTextField replace_with     = new JTextField(20);
     	JTextField output     = new JTextField(20);
     	JComboBox namesList1 = new JComboBox(phonedirectory.getNames());
     	JComboBox namesList2 = new JComboBox(phonedirectory.getNames());
        
     	Socket kkSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        
        public JPanel createPanel(){
      
            
            JPanel pane = new JPanel();
            JLabel label1 = new JLabel("Wyszukaj");
            JLabel label2 = new JLabel("Dodaj");
            JLabel label3 = new JLabel("Zastap");
            pane.setBorder(BorderFactory.createTitledBorder("Client"));
            pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
            pane.add(label1); 
            pane.add(namesList1);
            pane.add(search);
            pane.add(label2);
            pane.add(add_name);
            pane.add(add);
            pane.add(label3);
            pane.add(replace_with);
            pane.add(namesList2);
            pane.add(replace);
            pane.add(output);
            pane.add(bye);
            output.setEditable(false);
            return pane;
             
        }
        
        public void connect(){
            
            try {
                kkSocket = new Socket("localhost", 4445);
                out = new PrintWriter(kkSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host: localhost.");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: localhost.");
                System.exit(1);
            }
        }
        
        public void communicate(){
    
            ///BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String fromServer;
        	 try {
    			while ((fromServer = in.readLine()) != null) {
    			     System.out.println("Server: " + fromServer);
    			     output.setText(fromServer);
    			     if (fromServer.equals("Bye."))
    			         break;
    			    
    			     ////fromUser = stdIn.readLine();
    			if (action != null) {
    			         System.out.println("Client: " + action);
    
    
    			         ///out.println(action);
    			         
    			         
    			         if(lastname!=null){
    			         	 out.println(action+":"+lastname);
    			         }
    			         
    			        
    			         
    			         if(number_with!=null){
    			        	 String info[]=number_with.split(" ");
    			         	out.println(action+":"+info[0]+":"+info[1]);
    			         }
    			}
    			 }
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
        }
        
        public void close() throws IOException{ 
         out.close();
        in.close();
        ///stdIn.close();
        kkSocket.close();}
        
        
     	public static void main(String[] args) throws IOException {
        	
     		SimpleClient simpleclient = new SimpleClient();
    
     		 JFrame frame = new JFrame("Client");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
             frame.setContentPane(simpleclient.createPanel());
             frame.pack();
    
             frame.setVisible(true);
             simpleclient.connect();
             simpleclient.communicate();
             simpleclient.close();
     
        }
    }
    client receives the hello message from server
    but it doesnt send the messages to server
    i want when a button is clicked send an action to the server
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Issue an "out.flush( )" after writing to the server and read the API documentation
    on flushing behaviour of PrintWriter objects.

    kind regards,

    Jos

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      but out = new PrintWriter(kkS ocket.getOutput Stream(), true); is with automatic line flushing ?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by oll3i
        but out = new PrintWriter(kkS ocket.getOutput Stream(), true); is with automatic line flushing ?
        I know it sounds silly but do try the explicit flush(). I know it is supposed to
        flush at every println(), but it doesn't. It depends on whether or not the wrapped
        Writer flushes on a new line character.

        kind regards,

        Jos

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #5
          i added out.flush() and still it doesnt send the message to the server the problem is somewhere else i think :(

          Comment

          • oll3i
            Contributor
            • Mar 2007
            • 679

            #6
            it goes through communicate() method ones and doesnt respond to buttons where i set the action var

            Comment

            • oll3i
              Contributor
              • Mar 2007
              • 679

              #7
              and it throws java.net.Socket Exception: when i close the client

              Comment

              • oll3i
                Contributor
                • Mar 2007
                • 679

                #8
                i solved it the server returns phone
                numbers but i still get the java.net.Socket Exception when i close a client ?

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by oll3i
                  i solved it the server returns phone
                  numbers but i still get the java.net.Socket Exception when i close a client ?
                  Which side (client or server) throws the Exception? What is the reason the
                  Exception was thrown? Do a socketex.printS tackTrace() when you catch
                  the exception.

                  kind regards,

                  Jos

                  Comment

                  Working...