473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

client server communication problem

oll3i
679 512MB
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
8 1842
JosAH
11,448 Expert 8TB
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
679 512MB
but out = new PrintWriter(kkSocket.getOutputStream(), true); is with automatic line flushing ?
May 13 '07 #3
JosAH
11,448 Expert 8TB
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
679 512MB
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
679 512MB
it goes through communicate() method ones and doesnt respond to buttons where i set the action var
May 13 '07 #6
oll3i
679 512MB
and it throws java.net.SocketException: when i close the client
May 14 '07 #7
oll3i
679 512MB
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
JosAH
11,448 Expert 8TB
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

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

Similar topics

88
by: Mike | last post by:
Is there a way to determine what a user's default email client is? I read a post from 3 years ago that said no. I guess I'm hoping something has come along since then.
4
by: Prince Kumar | last post by:
I joined a company recently and they have a java program which hangs (does nothing) after a while. This is no way consistent. It could succeed quite a few times and can fail a few other times....
4
by: Christian Westerlund | last post by:
Hi! Does anyone know if it is possible for a client server communication easy in .NET with these requirements: The client always initiates the communication The server only answers (responds)...
0
by: Russ | last post by:
I have set up a C# web application that runs on my test Web Server (Windows 2003 Server, Web Edition). It in turn calls a web service running on the internal network. Now I want to issue a...
4
by: Anurag | last post by:
___________________________________________ ENVIRONMENT ___________________________________________ (1) Server = DB2 UDB DPF, 8.2.3 on AIX, 64-bit instance. (2) Client = DB2 RTC 8.1 FP10 on...
2
by: thilandeneth | last post by:
i need to do telnet via a web server please give me a idia to initiate the project following requirements are needed 1 Create web based custom telnet client to communicate with remote...
0
by: news.onet.pl | last post by:
I hava a problem with communication between Java/Corba server based on JDK ORB with Java/Corba client (applet) based on the same ORB. I`m using IOR to localize server. client`s ORB i initialize...
0
by: Maurizio | last post by:
I'd like to ask some suggestion regarding a software that I'm developping. For develop the project I've to use VB.NET and Framework 3.5 This is a Client Server application. I've some computer...
5
by: AeonOfTime | last post by:
Let's assume a web application (in this case a browser-based game) with a custom HTTP server built on PHP, and a client also built on PHP. The client uses the server to access and change data. Even...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.