By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,288 Members | 3,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,288 IT Pros & Developers. It's quick & easy.

client server communication problem

oll3i
100+
P: 679
Expand|Select|Wrap|Line Numbers
  1. package zad41;
  2.  
  3.  
  4.  
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.*;
  8. import java.net.*;
  9.  
  10. import javax.swing.BorderFactory;
  11. import javax.swing.BoxLayout;
  12. import javax.swing.JButton;
  13. import javax.swing.JComboBox;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JPanel;
  17. import javax.swing.JTextArea;
  18. import javax.swing.JTextField;
  19.  
  20. public class SimpleClient {
  21.      String action=null;
  22.      String lastname=null;
  23.      String number_with=null;
  24.      PhoneDirectory phonedirectory = new PhoneDirectory("numbers.txt");
  25.      public SimpleClient(){
  26.  
  27.  
  28.  
  29.          search.addActionListener( new ActionListener() {
  30.          public void actionPerformed(ActionEvent e) {
  31.                 System.out.println("Get");
  32.                 action="get";
  33.                 lastname=(String)namesList1.getSelectedItem();
  34.          }});
  35.  
  36.          add.addActionListener( new ActionListener() {
  37.          public void actionPerformed(ActionEvent e) {
  38.               System.out.println("Add");
  39.               action="add";
  40.               lastname=add_name.getText();
  41.          }});    
  42.  
  43.          replace.addActionListener( new ActionListener() {
  44.          public void actionPerformed(ActionEvent e) {
  45.                 System.out.println("Replace");
  46.                 action="replace";
  47.                 lastname=(String)namesList2.getSelectedItem();
  48.                 number_with=replace_with.getText();
  49.          }});   
  50.          bye.addActionListener( new ActionListener() {
  51.              public void actionPerformed(ActionEvent e) {
  52.                     System.out.println("Bye");
  53.                     action="Bye";
  54.                     }});     
  55.      }
  56.      JButton    search    = new JButton("Wyszukaj");
  57.      JButton    add    = new JButton("Dodaj");
  58.      JButton    replace    = new JButton("Zastap");
  59.      JButton    bye    = new JButton("Bye");
  60.      JTextField add_name     = new JTextField(20);
  61.      JTextField replace_with     = new JTextField(20);
  62.      JTextField output     = new JTextField(20);
  63.      JComboBox namesList1 = new JComboBox(phonedirectory.getNames());
  64.      JComboBox namesList2 = new JComboBox(phonedirectory.getNames());
  65.  
  66.      Socket kkSocket = null;
  67.     PrintWriter out = null;
  68.     BufferedReader in = null;
  69.  
  70.     public JPanel createPanel(){
  71.  
  72.  
  73.         JPanel pane = new JPanel();
  74.         JLabel label1 = new JLabel("Wyszukaj");
  75.         JLabel label2 = new JLabel("Dodaj");
  76.         JLabel label3 = new JLabel("Zastap");
  77.         pane.setBorder(BorderFactory.createTitledBorder("Client"));
  78.         pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
  79.         pane.add(label1); 
  80.         pane.add(namesList1);
  81.         pane.add(search);
  82.         pane.add(label2);
  83.         pane.add(add_name);
  84.         pane.add(add);
  85.         pane.add(label3);
  86.         pane.add(replace_with);
  87.         pane.add(namesList2);
  88.         pane.add(replace);
  89.         pane.add(output);
  90.         pane.add(bye);
  91.         output.setEditable(false);
  92.         return pane;
  93.  
  94.     }
  95.  
  96.     public void connect(){
  97.  
  98.         try {
  99.             kkSocket = new Socket("localhost", 4445);
  100.             out = new PrintWriter(kkSocket.getOutputStream(), true);
  101.             in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
  102.         } catch (UnknownHostException e) {
  103.             System.err.println("Don't know about host: localhost.");
  104.             System.exit(1);
  105.         } catch (IOException e) {
  106.             System.err.println("Couldn't get I/O for the connection to: localhost.");
  107.             System.exit(1);
  108.         }
  109.     }
  110.  
  111.     public void communicate(){
  112.  
  113.         ///BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
  114.         String fromServer;
  115.          try {
  116.             while ((fromServer = in.readLine()) != null) {
  117.                  System.out.println("Server: " + fromServer);
  118.                  output.setText(fromServer);
  119.                  if (fromServer.equals("Bye."))
  120.                      break;
  121.  
  122.                  ////fromUser = stdIn.readLine();
  123.             if (action != null) {
  124.                      System.out.println("Client: " + action);
  125.  
  126.  
  127.                      ///out.println(action);
  128.  
  129.  
  130.                      if(lastname!=null){
  131.                           out.println(action+":"+lastname);
  132.                      }
  133.  
  134.  
  135.  
  136.                      if(number_with!=null){
  137.                          String info[]=number_with.split(" ");
  138.                          out.println(action+":"+info[0]+":"+info[1]);
  139.                      }
  140.             }
  141.              }
  142.         } catch (IOException e) {
  143.             // TODO Auto-generated catch block
  144.             e.printStackTrace();
  145.         }
  146.     }
  147.  
  148.     public void close() throws IOException{ 
  149.      out.close();
  150.     in.close();
  151.     ///stdIn.close();
  152.     kkSocket.close();}
  153.  
  154.  
  155.      public static void main(String[] args) throws IOException {
  156.  
  157.          SimpleClient simpleclient = new SimpleClient();
  158.  
  159.           JFrame frame = new JFrame("Client");
  160.          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  161.  
  162.          frame.setContentPane(simpleclient.createPanel());
  163.          frame.pack();
  164.  
  165.          frame.setVisible(true);
  166.          simpleclient.connect();
  167.          simpleclient.communicate();
  168.          simpleclient.close();
  169.  
  170.     }
  171. }
  172.  
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
May 12 '07 #1
Share this Question
Share on Google+
8 Replies


Expert 10K+
P: 11,448
Issue an "out.flush()" after writing to the server and read the API documentation
on flushing behaviour of PrintWriter objects.

kind regards,

Jos
May 13 '07 #2

oll3i
100+
P: 679
but out = new PrintWriter(kkSocket.getOutputStream(), true); is with automatic line flushing ?
May 13 '07 #3

Expert 10K+
P: 11,448
but out = new PrintWriter(kkSocket.getOutputStream(), 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
May 13 '07 #4

oll3i
100+
P: 679
i added out.flush() and still it doesnt send the message to the server the problem is somewhere else i think :(
May 13 '07 #5

oll3i
100+
P: 679
it goes through communicate() method ones and doesnt respond to buttons where i set the action var
May 13 '07 #6

oll3i
100+
P: 679
and it throws java.net.SocketException: when i close the client
May 14 '07 #7

oll3i
100+
P: 679
i solved it the server returns phone
numbers but i still get the java.net.SocketException when i close a client ?
May 14 '07 #8

Expert 10K+
P: 11,448
i solved it the server returns phone
numbers but i still get the java.net.SocketException when i close a client ?
Which side (client or server) throws the Exception? What is the reason the
Exception was thrown? Do a socketex.printStackTrace() when you catch
the exception.

kind regards,

Jos
May 14 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.