CJEH
asked on
client-server question.(part 2)
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(thi s); // 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.getOutputStr eam(), true);
ps1 = new PrintWriter(s.getOutputStr eam(), true);
br = new BufferedReader(new InputStreamReader(s.getInp utStream() ));
result1.setText(br.readLin e());
br1 = new BufferedReader(new InputStreamReader(s.getInp utStream() ));
result2.setText(br.readLin e());
}
catch(IOException iExc){
//connection problem report
System.out.println("Proble m with connection");
iExc.printStackTrace();
}
}
public void actionPerformed(ActionEven t 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.gree n);
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(thi s);
// 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.getInp utStream() ));
result1.setText(br.readLin e());
br1 = new BufferedReader(new InputStreamReader(s.getInp utStream() ));
result2.setText(br.readLin e());
//true ensures autoflushing
ps = new PrintWriter(s.getOutputStr eam(), true);
ps1 = new PrintWriter(s.getOutputStr eam(), true);
}
catch(IOException iExc){
System.out.println("Proble m with client connection");
iExc.printStackTrace();
}
}
public void actionPerformed(ActionEven t 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.getOutputStr eam(), true);
ps1 = new PrintWriter(s.getOutputStr eam(), true);
br = new BufferedReader(new InputStreamReader(s.getInp utStream() ));
result1.setText(br.readLin e());
br1 = new BufferedReader(new InputStreamReader(s.getInp utStream() ));
result2.setText(br.readLin e());
/// should be here!!
s.close();
ss.close();
br.close();
br1.close();
ps.close();
ps1.close();
}
catch(IOException iExc){
//connection problem report
System.out.println("Proble m 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 ….
======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(thi
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
s = ss.accept();
ps = new PrintWriter(s.getOutputStr
ps1 = new PrintWriter(s.getOutputStr
br = new BufferedReader(new InputStreamReader(s.getInp
result1.setText(br.readLin
br1 = new BufferedReader(new InputStreamReader(s.getInp
result2.setText(br.readLin
}
catch(IOException iExc){
//connection problem report
System.out.println("Proble
iExc.printStackTrace();
}
}
public void actionPerformed(ActionEven
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.gree
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(thi
// 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.getInp
result1.setText(br.readLin
br1 = new BufferedReader(new InputStreamReader(s.getInp
result2.setText(br.readLin
//true ensures autoflushing
ps = new PrintWriter(s.getOutputStr
ps1 = new PrintWriter(s.getOutputStr
}
catch(IOException iExc){
System.out.println("Proble
iExc.printStackTrace();
}
}
public void actionPerformed(ActionEven
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
s = ss.accept();
ps = new PrintWriter(s.getOutputStr
ps1 = new PrintWriter(s.getOutputStr
br = new BufferedReader(new InputStreamReader(s.getInp
result1.setText(br.readLin
br1 = new BufferedReader(new InputStreamReader(s.getInp
result2.setText(br.readLin
/// should be here!!
s.close();
ss.close();
br.close();
br1.close();
ps.close();
ps1.close();
}
catch(IOException iExc){
//connection problem report
System.out.println("Proble
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 ….
Is that supposed to be a question or you just want to assign points to someone?
ASKER
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.
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.
Ah ok.. I won't answer it then.. I will leave Tim Yates or CEHJ to get the points :-)
ASKER
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!
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!
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?
I do not have personal problems with anybody. Do you?
ASKER
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 ...
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 ...
:-)
> 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.getInp utStream() ));
result1.setText(br.readLin e());
result2.setText(br.readLin e());
//true ensures autoflushing
ps = new PrintWriter(s.getOutputStr eam(), true);
ps1 = new PrintWriter(s.getOutputStr eam(), 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.readLin e());
}
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.
> 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.getInp
result1.setText(br.readLin
result2.setText(br.readLin
//true ensures autoflushing
ps = new PrintWriter(s.getOutputStr
ps1 = new PrintWriter(s.getOutputStr
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.readLin
}
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.
ASKER
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.readLin e());
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 :)
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.readLin
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 :)
> 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.read Line()); // 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.
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.read
}
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks alot ...
and sorry again if i've behaved rude.
and sorry again if i've behaved rude.
Thank you :-)