?
Solved

client-server question.(part 2)

Posted on 2003-03-07
12
Medium Priority
?
299 Views
Last Modified: 2010-03-30
Hi guys I’ve done it another way:
======the server==========
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleServer extends Frame implements ActionListener{


//definig variables
  private TextField tf1, tf2, result1, result2;
  private Button send;
  Label L,L1,L2,L4;
  Panel p1,p2,p3,p4;
  Socket s=null;
  ServerSocket ss=null;
  BufferedReader br = null;
  BufferedReader br1 = null;
  PrintWriter ps = null;
  PrintWriter ps1 = null;

  public SimpleServer (){

        // giving a lable to the frame
                super("simple Server");
       // setting layout
        setLayout(new GridLayout(5,1));
        p1 = new Panel();
        p2 = new Panel();
        p3 = new Panel();
        p4 = new Panel();
        add(p1);
        add(p2);
        add(p3);
        add(p4);

        // setting up panel 1
        p1.setLayout(new FlowLayout(FlowLayout.LEFT));
        L = new Label("Fill up the textfiled and send to client:");
        L.setBackground(Color.pink);
        p1.add(L);

        // setting up panel 2
        p2.setLayout(new FlowLayout(FlowLayout.LEFT));
        L1  = new Label("server field 1");
        L2  = new Label("server field 2");
        tf1 = new TextField(10);
        tf2 = new TextField(10);
        p2.add(L1);
        p2.add(tf1);
        p2.add(L2);
        p2.add(tf2);

        // setting up panel 3
        p3.setLayout(new FlowLayout(FlowLayout.LEFT));
        L4  = new Label("press the button to send to client: ");
        send = new Button("Send to client");
        send.addActionListener(this); // adding action listener
        p3.add(L4);
        p3.add(send);

         // setting up panel 4
        p4.setLayout(new FlowLayout(FlowLayout.LEFT));
        result1 = new TextField("result1 from client",20);
        result2 = new TextField("result2 from client",20);
        p4.add(result1);
        p4.add(result2);
        result1.setEditable(false);
        result2.setEditable(false);

        //seting the size of the frame
            setSize(new Dimension(700, 180));

        // show the GUI
        show();

       addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent e){

         System.exit(0);
       }
     });

try{
ss = new ServerSocket(1024);
//Blocks waiting for the client
System.out.println("Server started waiting for client");
s = ss.accept();
ps = new PrintWriter(s.getOutputStream(), true);
ps1 = new PrintWriter(s.getOutputStream(), true);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
result1.setText(br.readLine());
br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
result2.setText(br.readLine());

}

catch(IOException iExc){
//connection problem report
System.out.println("Problem with connection");
iExc.printStackTrace();
}
  }


  public void actionPerformed(ActionEvent e) {
  if(e.getSource() == send){

      ps.println(tf1.getText());
      ps1.println(tf2.getText());

      }

  }



public static void main(String args[]) {

SimpleServer server = new SimpleServer();


}
}

=============the client==============
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleClient extends Frame implements ActionListener{


//definig variables
  private TextField tf1, tf2,result1, result2;
  private Button send;
  Label L,L1,L2,L3,L4;
  Panel p1,p2,p3,p4;
  Socket s = null;
  PrintWriter ps = null;
  PrintWriter ps1 = null;
  BufferedReader br = null;
  BufferedReader br1 = null;

  public SimpleClient (){

        // giving a lable to the frame
                super("Simple Client");

        setLayout(new GridLayout(6,1));
        p1 = new Panel();
        p2 = new Panel();
        p3 = new Panel();
        p4 = new Panel();
        add(p1);
        add(p2);
        add(p3);
        add(p4);

        // setting up panel 1
        p1.setLayout(new FlowLayout(FlowLayout.LEFT));
        L = new Label("Fill up the textfiled and send to sever:");
        L.setBackground(Color.green);
        p1.add(L);

        // setting up panel 2
        p2.setLayout(new FlowLayout(FlowLayout.LEFT));
        L1  = new Label("client field 1:");
        L2  = new Label("client field 2:");
        tf1 = new TextField(10);
        tf2 = new TextField(10);
        p2.add(L1);
        p2.add(tf1);
        p2.add(L2);
        p2.add(tf2);




        // setting up panel 3
        p3.setLayout(new FlowLayout(FlowLayout.LEFT));
        L4  = new Label("press the button to send to server: ");
        send = new Button("send to sever");
        p3.add(L4);
        p3.add(send);
        send.addActionListener(this);


        // setting up panel 4
        p4.setLayout(new FlowLayout(FlowLayout.LEFT));
        result1 = new TextField("result1 from server",20);
        result2 = new TextField("result2 from server",20);
        p4.add(result1);
        p4.add(result2);
        result1.setEditable(false);
        result2.setEditable(false);
        //seting the size of the frame
            setSize(new Dimension(700, 190));

        // show the GUI
        show();

        addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent e){

         System.exit(0);
       }
     });

        try{
s = new Socket("127.0.0.1", 1024);

br = new BufferedReader(new InputStreamReader(s.getInputStream()));
result1.setText(br.readLine());
br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
result2.setText(br.readLine());
//true ensures autoflushing
      ps = new PrintWriter(s.getOutputStream(), true);
      ps1 = new PrintWriter(s.getOutputStream(), true);

}

catch(IOException iExc){
System.out.println("Problem with client connection");
iExc.printStackTrace();
}

  }



  public void actionPerformed(ActionEvent e) {
      if(e.getSource() == send){

      ps.println(tf1.getText());
      ps1.println(tf2.getText());

      }

  }




static public void main(String args[]) {

SimpleClient client = new SimpleClient();

}

}
===========finished================

its working well, but there are some points:
1.     I’m able to send and receive data through the textfields but it just happens once…
why is that?!!

2.  I don’t know where to put  
 s.close();
 ss.close();
 br.close();
 br1.close();
 ps.close();
 ps1.close();
in the server program, but I ‘m not sure where exactly!!!
I’ve tried to put them like :

        addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent e){
  s.close();
 ss.close();
 br.close();
 br1.close();
 ps.close();
 ps1.close();

         System.exit(0);
       }
     });

but doesn’t work :|

I know that these codes should be in  the try block like this:

try{
ss = new ServerSocket(1024);
//Blocks waiting for the client
System.out.println("Server started waiting for client");
s = ss.accept();
ps = new PrintWriter(s.getOutputStream(), true);
ps1 = new PrintWriter(s.getOutputStream(), true);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
result1.setText(br.readLine());
br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
result2.setText(br.readLine());

/// should be here!!
  s.close();
 ss.close();
 br.close();
 br1.close();
 ps.close();
 ps1.close();

}

catch(IOException iExc){
//connection problem report
System.out.println("Problem with connection");
iExc.printStackTrace();
}
  }

but I need to put them somewhere when I can close the window these should be closed at the same time.

That’s all for now thanks.

Waiting ….
0
Comment
Question by:CJEH
[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
  • 7
  • 5
12 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8089084
 Is that supposed to be a question or you just want to assign points to someone?
0
 

Author Comment

by:CJEH
ID: 8089139
a question ...
as CEHJ said
>>Well you should really post this as a new question. Tim Yates came over and answered your questions at my request. Please accept his answer and post a new question.

0
 
LVL 35

Expert Comment

by:girionis
ID: 8089249
 Ah ok.. I won't answer it then.. I will leave Tim Yates or CEHJ to get the points :-)
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:CJEH
ID: 8089635
ohh common ...
whats wrong with this forum
do u have a personal problem with those guys??!
i even dont know who they are, i thought that they are the moderators of this forum or something!!!!
...
i dont care who will answer my question
but i need an answer to my question!!!

i am not joking here, its serious!
0
 
LVL 35

Expert Comment

by:girionis
ID: 8090114
 Hey relax... I just said it would be better for them to answer since they know the question's history that's all.

  I do not have personal problems with anybody. Do you?
0
 

Author Comment

by:CJEH
ID: 8091204
either me.
sorry about that.
now assume it as a new question ok.
i have the above codes for the cielnt and the server program, what i want is to pass some data from the server program to the client and the same other way around.
its all woking but only once meaning that once you sent the data from server to client you cannot send it again!
i want it to be unlimited( the sending and recieving thing)
hope that its clear now.

i really appritiate any help ...
0
 
LVL 35

Expert Comment

by:girionis
ID: 8091538
 :-)

> I’m able to send and receive data through the textfields but it just happens once…
why is that?!!

  This is because you only run your programme once. You need to have a while loop that will be running while there is still data coming from the server. For example you have to add something like this in your client (inside your constructor in your try block of code:


s = new Socket("127.0.0.1", 1024);

br = new BufferedReader(new InputStreamReader(s.getInputStream()));
result1.setText(br.readLine());
result2.setText(br.readLine());

//true ensures autoflushing
ps = new PrintWriter(s.getOutputStream(), true);
ps1 = new PrintWriter(s.getOutputStream(), true);
                 
String line = null;  // Line to be read from the server

// While there is more data coming...
while ( (line = br.readLine()) != null)
{
     result1.setText(line);
     result2.setText(br.readLine());
}

  You need to do likewise for the server.

> I don’t know where to put ... but doesn’t work :|

  What do you mean it doesn't work? I'd put them myself into the windows closing so all the connections are closed before the user exits the application.
0
 

Author Comment

by:CJEH
ID: 8091694
now that what i call real answer;)
dont worry you'll get your points
just few notes:
for window listener i have added a try block to let it work prorerly and it works:

       //adding window listener
        addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
          try{
          s.close();
          br.close();
          ps.close();
          ps1.close();
             }
             catch(IOException iE){}

          System.exit(0);
        }
      });

1.why did you put (String)line in result1 while putting br.readLine()); in result2

    result1.setText(line);
    result2.setText(br.readLine());

2. do we need just one bufferedReader for the whole code?
3.how many PrintWriters do we need if we want to send Strings from about ... say 5 textfileds?

thats all and thanks for your answer.
you'll soon get your points :)
0
 
LVL 35

Expert Comment

by:girionis
ID: 8091772
> 1.why did you put (String)line in result1 while putting br.readLine()); in result2

  Because we only read two lines from the server. Every br.readLine() you do reads a new line. So we already read the first line and check for it in the while loop. But we need to store this line somewhere becase we will ned it later. If we do not store it in the "line" variable then we wil lloose it since the br.readLine() will read the next line. So in the following example:

while (br.readLine() != null) // read one line here
{
   result1.setText(line);  // another here
   result2.setText(br.readLine());  // and another here
}

  We read in total of 3 lines while we can read 2. Therefore the third line we will be getting will not be valid and the text field will not be updated properly. Try it and see for yourself if you are curious.

> 2. do we need just one bufferedReader for the whole code?

  Yes one is sufficient since you can be using this one over and over.

> 3.how many PrintWriters do we need if we want to send Strings from about ... say 5 textfileds?

  Again one. You can use the same PrinWriter instance to write to the text fields, no matter how many text fields you have.

> thats all and thanks for your answer.

  Np :-) Sorry for being a bit "weird" in the beginning but I thought you asked specific people for your problem.

0
 
LVL 35

Accepted Solution

by:
girionis earned 100 total points
ID: 8091789
 Sorry this:

> result1.setText(line);  // another here

  should be in my previous message:

result1.setText(br.readLine());  // another here
0
 

Author Comment

by:CJEH
ID: 8091836
thanks alot ...
and sorry again if i've behaved rude.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8091867
 Thank you :-)
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

801 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