?
Solved

Server works once only

Posted on 2003-03-26
11
Medium Priority
?
235 Views
Last Modified: 2009-12-16
I've made a simple program in order to try and work out the problem with the real one. However, I can't seem to get around it. Help would be greatly appreciated.

There is a client and a server, both using swing (JApplet and JFrame respectively).

The client has a JCheckBox, a JTextField, and a JButton.

The server has a JLabel.

The idea is that the client selects, de-selects or leaves the JCheckBox alone. When he clicks on the JButton, a number - 0 or 1 - is sent to the server by the output stream, representing whether the checkbox is selected or not. The server checks the number, and sets the text of its JLabel to reflect whether the client's checkbox is selected. Having done that, it sends a 0 or 1 back to the client; as with the server, the 0 or 1 determines whether the checkbox is selected, and the JTextField's text is adjusted. So, the end result is that when the client presses the button, both the server and the client tell the user the state of the button.

It works the first time around. However, when the client presses the button a second time around, both sides' programs lock up completely. I'm certain it is something to do with the datastreams, but I can't determine what.

Client 'streams:
//--------------
     public void actionPerformed(ActionEvent action)
     {
     try {
          Socket client = new Socket(getCodeBase().getHost(), 3333);

          DataOutputStream output = new DataOutputStream(client.getOutputStream());
          DataInputStream input = new DataInputStream(client.getInputStream());

//...

//[if it is selected]
   output.writeInt(0);

//...

//[if it isn't selected]

   output.writeInt(1);

//....
   output.flush();
//---------

It's more or less the same on the client side (except for it being a socket instead of a serversocket etc.). Any ideas?
0
Comment
Question by:Snidely
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8212941
 Can you post the code of your server programme that hooks up? I guess it is where you read the data from the client.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8212947
 ... you probably have forgotten a while loop or something.
0
 

Author Comment

by:Snidely
ID: 8213157
Here goes.

I see where you're coming from with the while loop...but while what? I want it to run through the relevant code once each time someone presses the button - but if I want to get a value for the button (so that it "do"es when the button is pressed), doesn't the input stream have to go in the "do-while" itself?

("confirmation" is the (server's) JTextField.)
//---------------------------
try
   {
     ServerSocket server = new ServerSocket(3333, 100);
     Socket socket = server.accept();
                         
     DataInputStream input = new DataInputStream(socket.getInputStream());
     DataOutputStream output = new DataOutputStream(socket.getOutputStream());

     int boxCheck = 0;

     boxCheck = input.readInt();

     if (boxCheck == 0)
     {
      output.writeInt(0);
      confirmation.setText("Not selected");
     }
     else
      output.writeInt(1);
      confirmation.setText("Selected");
                   
     output.flush();

}catch (IOException  e) {
          System.out.println(e);
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Snidely
ID: 8213227
Here goes.

I see where you're coming from with the while loop...but while what? I want it to run through the relevant code once each time someone presses the button - but if I want to get a value for the button (so that it "do"es when the button is pressed), doesn't the input stream have to go in the "do-while" itself?

("confirmation" is the (server's) JTextField.)
//---------------------------
try
   {
     ServerSocket server = new ServerSocket(3333, 100);
     Socket socket = server.accept();
                         
     DataInputStream input = new DataInputStream(socket.getInputStream());
     DataOutputStream output = new DataOutputStream(socket.getOutputStream());

     int boxCheck = 0;

     boxCheck = input.readInt();

     if (boxCheck == 0)
     {
      output.writeInt(0);
      confirmation.setText("Not selected");
     }
     else
      output.writeInt(1);
      confirmation.setText("Selected");
                   
     output.flush();

}catch (IOException  e) {
          System.out.println(e);
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 8213262
 When a user clicks on a button the code that captures the button pressing will run anyway so you do not have to worry about it. You just need on the server side to listen for client requests continuously.

  Can you modify the snippet of code to this:

while (true)
{
    boxCheck = input.readInt();
    if (boxCheck == 0)
    {
     output.writeInt(0);
     confirmation.setText("Not selected");
    }
    else
     output.writeInt(1);
     confirmation.setText("Selected");
                   
    output.flush();
}

  and let us know if you still have problems?
0
 
LVL 35

Expert Comment

by:girionis
ID: 8213277
 You might want to leave that: output.flush(); outside the while loop.
0
 

Author Comment

by:Snidely
ID: 8213368
I still get the same problem, unfortunately.

I tried to leave the output.flush() outside the while loop, but when I did, it accused it of being an "unreachable statement".

Thanks for the feedback so far, by the way.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8213414
 Ok it is difficult to say without looking at your code. Is it possible to post it here, or at least a small compilable example so I can recreate the problem?
0
 

Author Comment

by:Snidely
ID: 8213775
The program's not all that long. I'll put it up here.

A few of the names are different ("outlook" instead of "output", for example). I was trying something that didn't work out. The code is virtually the same besides that.

Server code:

import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;

public class ServerTest extends JFrame {

     public ServerTest()
     {
     super("Meh");
     setSize(100, 50);
     Container content = getContentPane();
         
     JLabel confirmation = new JLabel("(Waiting)");

          content.add(confirmation);
          pack();
          setVisible(true);
         
          try
          {
          ServerSocket server = new ServerSocket(3333);
         
          Socket nahbrah = server.accept();
                         
          DataInputStream inlook = new DataInputStream(nahbrah.getInputStream());
          DataOutputStream outlook = new DataOutputStream(nahbrah.getOutputStream());

          int boxCheck = 0;

          while (true)
          {
     
          boxCheck = inlook.readInt();
                         if (boxCheck == 0)
                       {
                    outlook.writeInt(0);
                         confirmation.setText("Not selected");
          }
          else
          {
          outlook.writeInt(1);
                         confirmation.setText("Selected");
     }
     outlook.flush();
     }



     }catch (IOException  e) {
             System.out.println(e);
     }
         
     }

     public static void main(String[] args)
     {
          ServerTest application = new ServerTest();

          application.setDefaultCloseOperation(
               JFrame.EXIT_ON_CLOSE);
     }
}

//Client code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color.*;
import javax.swing.border.*;
import java.io.*;
import java.net.*;

public class ClientTest extends JApplet
     implements ItemListener, ActionListener {

     JCheckBox cb1;
     JTextField result;
     JButton send;
     int choice = 0;

     public void init()
     {
          Container pane = getContentPane();
          pane.setLayout(new GridLayout(4,2));
         
          cb1 = new JCheckBox("Click me");
          result = new JTextField();
          send = new JButton("Send >>");

          cb1.addItemListener(this);

          send.addActionListener(this);

          pane.setLayout(new GridLayout(4,0));
         
          pane.add(cb1);
          pane.add(result);
          pane.add(send);

          setVisible(true);
     }

     public void itemStateChanged(ItemEvent item)
     {
          if (item.getStateChange() == ItemEvent.SELECTED)
          {
               choice = 1;
          }
          else
               choice = 0;
         
     }

     public void actionPerformed(ActionEvent action)
     {
     try {
          Socket client = new Socket(getCodeBase().getHost(), 3333);

          DataOutputStream outlook = new DataOutputStream(client.getOutputStream());
          DataInputStream inlook = new DataInputStream(client.getInputStream());

          if (choice == 1)
               {
                    outlook.writeInt(1);
               }
          else
                    outlook.writeInt(0);
               
          int boxCheck = 0;
          boxCheck = inlook.readInt();

          if (boxCheck == 1)
          {
               result.setText("Selected");
          }
          else if (boxCheck == 1)
          {
               result.setText("Not selected");
          }
         
          outlook.close();

          } catch (IOException  e) {
               System.out.println(e);
          }
     }
}
0
 
LVL 4

Accepted Solution

by:
kokchoon78 earned 200 total points
ID: 8214786
Hi, i have made some changes on your ServerTest.java, i just simply put the following code inside the while loop

nahbrah = server.accept();
inlook = new DataInputStream(nahbrah.getInputStream());
outlook = new DataOutputStream(nahbrah.getOutputStream());

...
...

outlook.flush();
inlook.close();
outlook.close();
nahbrah.close();

That means, every time you click on the "Send" button, 1 connection will be created.

For more complete server implementation, you may need to make your server multi-thread. More infor on multi thread, please visit :
http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html
or
http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html

Here is the modified code :

ClientTest.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color.*;
import javax.swing.border.*;
import java.io.*;
import java.net.*;

public class ClientTest extends JApplet
    implements ItemListener, ActionListener {

    JCheckBox cb1;
    JTextField result;
    JButton send;
    int choice = 0;

    public void init()
    {
         Container pane = getContentPane();
         pane.setLayout(new GridLayout(4,2));

         cb1 = new JCheckBox("Click me");
         result = new JTextField();
         send = new JButton("Send >>");

         cb1.addItemListener(this);

         send.addActionListener(this);

         pane.setLayout(new GridLayout(4,0));

         pane.add(cb1);
         pane.add(result);
         pane.add(send);

         setVisible(true);
    }

    public void itemStateChanged(ItemEvent item)
    {
         if (item.getStateChange() == ItemEvent.SELECTED)
         {
              choice = 1;
         }
         else
              choice = 0;

    }

    public void actionPerformed(ActionEvent action)
    {
    try {
         Socket client = new Socket(getCodeBase().getHost(), 3333);

         DataOutputStream outlook = new DataOutputStream(client.getOutputStream());
         DataInputStream inlook = new DataInputStream(client.getInputStream());

         if (choice == 1)
              {
                   outlook.writeInt(1);
              }
         else
                   outlook.writeInt(0);

         int boxCheck = 0;
         boxCheck = inlook.readInt();

         if (boxCheck == 1)
         {
              result.setText("Selected");
         }
         else if (boxCheck == 0)
         {
              result.setText("Not selected");
         }

         outlook.close();

         } catch (IOException  e) {
              System.out.println(e);
         }
    }

}


ServerTest.java

import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;

public class ServerTest extends JFrame {

    public ServerTest()
    {
    super("Meh");
    setSize(100, 50);
    Container content = getContentPane();

    JLabel confirmation = new JLabel("(Waiting)");

         content.add(confirmation);
         pack();
         setVisible(true);

         try
         {
         ServerSocket server = new ServerSocket(3333);
         Socket nahbrah = null;

         DataInputStream inlook = null;
         DataOutputStream outlook = null;

         int boxCheck = 0;

         while (true)
         {
                 nahbrah = server.accept();

                 inlook = new DataInputStream(nahbrah.getInputStream());
                 outlook = new DataOutputStream(nahbrah.getOutputStream());

                 boxCheck = inlook.readInt();
                 System.out.println("inlook:" + boxCheck);
                 if (boxCheck == 0)
                 {
                     outlook.writeInt(0);
                     confirmation.setText("Not selected");
                 }
                 else
                 {
                        outlook.writeInt(1);
                        confirmation.setText("Selected");
                 }
                 outlook.flush();
                 inlook.close();
                 outlook.close();
                 nahbrah.close();
         }


    }
    catch (IOException  e)
    {
        e.printStackTrace();
    }

    }

    public static void main(String[] args)
    {
         ServerTest application = new ServerTest();

         application.setDefaultCloseOperation(
              JFrame.EXIT_ON_CLOSE);
    }
}



hth,
Kok Choon.

0
 

Author Comment

by:Snidely
ID: 8216502
Works like a charm. Thanks to both girionis and kokchoon78.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question