473,670 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

client server communication problem

oll3i
679 Contributor
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 1856
JosAH
11,448 Recognized Expert MVP
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 Contributor
but out = new PrintWriter(kkS ocket.getOutput Stream(), true); is with automatic line flushing ?
May 13 '07 #3
JosAH
11,448 Recognized Expert MVP
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
May 13 '07 #4
oll3i
679 Contributor
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 Contributor
it goes through communicate() method ones and doesnt respond to buttons where i set the action var
May 13 '07 #6
oll3i
679 Contributor
and it throws java.net.Socket Exception: when i close the client
May 14 '07 #7
oll3i
679 Contributor
i solved it the server returns phone
numbers but i still get the java.net.Socket Exception when i close a client ?
May 14 '07 #8
JosAH
11,448 Recognized Expert MVP
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
May 14 '07 #9

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

Similar topics

88
12476
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
6583
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. There is no consistency when it fails. Could anyone here shed some light on how to debug/resolve the issue. I guess IBM looked at the issue and were not able to pinpoint where the issue is. When the program hangs and when force the DB2...
4
9320
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) Uses port 80, skips firewall The server computer doesn't have a IIS or web server
0
1780
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 client certificate to each client connecting to the web app, and use it to authenticate and identify the clients on the web server (and use the SSL for secure encrypted exchanges). There is no issue requiring SSL between the web server and the back...
4
1887
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 AIX, 64-bit instance. ___________________________________________ PROBLEM ___________________________________________
2
5178
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 destinations. must provide login security before make communication 2 telnet communication should not be a direct 1 to 1 communication and that must be as follows Web telnet client -à Web server (telnet client) -à destination (see figure 01)
0
3262
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 like that: Dane proxy = null; ORB orb = ORB.init(parent, null); org.omg.CORBA.Object obj = orb.string_to_object(sIOR); proxy = DaneHelper.narrow(obj); server`s ORB i initialize like that:
0
1227
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 ( 1 server and 2 or 3 clients ) connected each other in LAN. This project is located in industial automation field.
5
3170
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 if the client server communication is not directly visible to the user (who logs into the client), the fact that the server is publicly accessible (a port sniffer would be enough to find it) means the communication has to be secured. How...
0
8471
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8907
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8817
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8663
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6218
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.