AliciaSV
asked on
client-server question.
hi ...
i have a simple client server program and i just want to know how to pass arguments between them.
======the server's source code========
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;
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();
}
public void actionPerformed(ActionEven t e) {
}
public static void main(String args[]) {
SimpleServer server = new SimpleServer();
Socket s=null;
ServerSocket ss=null;
BufferedReader br = null;
try{
ss = new ServerSocket(1024);
//Blocks waiting for the client
System.out.println("Server started waiting for client");
s = ss.accept(); // InputStreamReader converts bytes to charecters
br = new BufferedReader(new InputStreamReader(s.getInp utStream() ));
System.out.println(br.read Line());
}
catch(IOException iExc){
//connection problem report
System.out.println("Proble m with connection");
iExc.printStackTrace();
}
}
}
========================== ========== ========== ========== =
============the Client source code====================== =
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;
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();
}
public void actionPerformed(ActionEven t e) {
}
static public void main(String args[]) {
SimpleClient client = new SimpleClient();
Socket s = null;
PrintWriter ps = null;
try{
s = new Socket("127.0.0.1", 1024);
ps = new PrintWriter(s.getOutputStr eam(), true);
//true ensures autoflushing
ps.println("Client is connected");
ps.close();
s.close();
}
catch(IOException iExc){
System.out.println("Proble m with client connection");
iExc.printStackTrace();
}
}
}
========================== finish==== ========== ==========
1.what i need is to pass arguments from server's textfiels to the client result fields, and the same with the client program.
2.i've found this bit of code from the net and there are some points to be clear of:
=========from the server program======
Socket s=null; // why null?!!
ServerSocket ss=null;// why null?!!
BufferedReader br = null;// why null?!!
=========
thats all for now.
Note: i'm not using java swing.
i have a simple client server program and i just want to know how to pass arguments between them.
======the server's source code========
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;
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();
}
public void actionPerformed(ActionEven
}
public static void main(String args[]) {
SimpleServer server = new SimpleServer();
Socket s=null;
ServerSocket ss=null;
BufferedReader br = null;
try{
ss = new ServerSocket(1024);
//Blocks waiting for the client
System.out.println("Server
s = ss.accept(); // InputStreamReader converts bytes to charecters
br = new BufferedReader(new InputStreamReader(s.getInp
System.out.println(br.read
}
catch(IOException iExc){
//connection problem report
System.out.println("Proble
iExc.printStackTrace();
}
}
}
==========================
============the Client source code======================
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;
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();
}
public void actionPerformed(ActionEven
}
static public void main(String args[]) {
SimpleClient client = new SimpleClient();
Socket s = null;
PrintWriter ps = null;
try{
s = new Socket("127.0.0.1", 1024);
ps = new PrintWriter(s.getOutputStr
//true ensures autoflushing
ps.println("Client is connected");
ps.close();
s.close();
}
catch(IOException iExc){
System.out.println("Proble
iExc.printStackTrace();
}
}
}
==========================
1.what i need is to pass arguments from server's textfiels to the client result fields, and the same with the client program.
2.i've found this bit of code from the net and there are some points to be clear of:
=========from the server program======
Socket s=null; // why null?!!
ServerSocket ss=null;// why null?!!
BufferedReader br = null;// why null?!!
=========
thats all for now.
Note: i'm not using java swing.
>.what i need is to pass arguments from server's textfiels to the client result fields, and the same with the client program.
Don't you mean pass the client's text fields to the server? (that's the way round the communication works, otherwise you'd have to have two clients and two servers; one pair on each end)
Don't you mean pass the client's text fields to the server? (that's the way round the communication works, otherwise you'd have to have two clients and two servers; one pair on each end)
ASKER
thanks guys for your help ...
CEHJ
>>Don't you mean pass the client's text fields to the server?
yes that what i communication between a single client and a single server.
its still unclear for me, but is there a usefull toutorial that i can refer to not the sun's one becouse i've gone through it.
another question:
why do we need to import java.io, why can't we establish a communication (passing data) without it?
CEHJ
>>Don't you mean pass the client's text fields to the server?
yes that what i communication between a single client and a single server.
its still unclear for me, but is there a usefull toutorial that i can refer to not the sun's one becouse i've gone through it.
another question:
why do we need to import java.io, why can't we establish a communication (passing data) without it?
You dont need to import java.io.*. That is prety nasty form and one day, you'll pay the price for throwing the kitchen sink at a compile.
You DO need to use some of the classes in the java.io package, like java.io.PrintWriter, java.io.BufferedReader, java.io.IOException.
>> why can't we establish a communication (passing data) without it
How else would you read/write data? I suppose you could write your own IO in C/C++ and use JNI if you REALLY were determined not to use java.io. That sure would test one's resolve ( and programming ability ).
You DO need to use some of the classes in the java.io package, like java.io.PrintWriter, java.io.BufferedReader, java.io.IOException.
>> why can't we establish a communication (passing data) without it
How else would you read/write data? I suppose you could write your own IO in C/C++ and use JNI if you REALLY were determined not to use java.io. That sure would test one's resolve ( and programming ability ).
functionpointers remarks show the general way to go about things. There's another tutorial at http://www.cs.unc.edu/Courses/jbs/lessons/java/java_client_server1/
>>why do we need to import java.io
Because certain library classes from the java.io package are required for use in networking, so these need to be imported, as you'll see if you look at the tutorials.
>>why do we need to import java.io
Because certain library classes from the java.io package are required for use in networking, so these need to be imported, as you'll see if you look at the tutorials.
ASKER
thanks for that link, but there is no enough explanation if you try those links
http://www.cs.unc.edu/Courses/jbs/lessons/java/java_client_server1/server_basic.html
http://www.cs.unc.edu/Courses/jbs/lessons/java/java_client_server1/client_basic.html
these are just codes without much information on what each code doing and why.
sorry for that.
http://www.cs.unc.edu/Courses/jbs/lessons/java/java_client_server1/server_basic.html
http://www.cs.unc.edu/Courses/jbs/lessons/java/java_client_server1/client_basic.html
these are just codes without much information on what each code doing and why.
sorry for that.
ASKER
lets forget about that ...
now can anyone help me using the provided codes showing how to pass data between client and server.
-is there anything missing in the provided codes in therms of communication.
-do i need to used actionPerformed method to pass data between the two programs.
-how can it be done?
now can anyone help me using the provided codes showing how to pass data between client and server.
-is there anything missing in the provided codes in therms of communication.
-do i need to used actionPerformed method to pass data between the two programs.
-how can it be done?
ASKER
waiting ...
any data you want to pass between the programs is done through the sockets.
One does:
byte[] data;
//load up some data
socket.getOutputStream().w rite( data );
The other does something like:
BufferedInputStream bis = new BufferedInputStream( socket.getInputStream() );
while( ( len = bis.read( data ) ) != -1 )
The examples CEHJ gave you are great examples of client/server socket communication.
It's your data. If anyone should know what to do with it on either end, I would think it would be you. I am not quite sure what else you are looking for. Data Format? Protocol?
One does:
byte[] data;
//load up some data
socket.getOutputStream().w
The other does something like:
BufferedInputStream bis = new BufferedInputStream( socket.getInputStream() );
while( ( len = bis.read( data ) ) != -1 )
The examples CEHJ gave you are great examples of client/server socket communication.
It's your data. If anyone should know what to do with it on either end, I would think it would be you. I am not quite sure what else you are looking for. Data Format? Protocol?
hi,
You have put the socket and the connection in the main method. Instead of that one if you put your code in side that class as a separate method, then you can able to access the text field and send the details from a text field.
as follows.
======the server's source code========
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;
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();
}
public void startServer(){
try{
ss = new ServerSocket(1024);
//Blocks waiting for the client
System.out.println("Server started waiting for client");
s = ss.accept(); // InputStreamReader converts bytes to charecters
br = new BufferedReader(new InputStreamReader(s.getInp utStream() ));
System.out.println(br.read Line());
// You can put the string to text box
}
catch(IOException iExc){
//connection problem report
System.out.println("Proble m with connection");
iExc.printStackTrace();
}
}
public void actionPerformed(ActionEven t e) {
}
public static void main(String args[]) {
SimpleServer server = new SimpleServer();
server.startServer();
//Socket s=null;
//ServerSocket ss=null;
//BufferedReader br = null;
/*try{
ss = new ServerSocket(1024);
//Blocks waiting for the client
System.out.println("Server started waiting for client");
s = ss.accept(); // InputStreamReader converts bytes to charecters
br = new BufferedReader(new InputStreamReader(s.getInp utStream() ));
System.out.println(br.read Line());
}
catch(IOException iExc){
//connection problem report
System.out.println("Proble m with connection");
iExc.printStackTrace();
}*/
}
}
========================== ========== ========== ========== =
In this similer way you can put the code in that client to in side the Simpleclient class
You have put the socket and the connection in the main method. Instead of that one if you put your code in side that class as a separate method, then you can able to access the text field and send the details from a text field.
as follows.
======the server's source code========
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;
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();
}
public void startServer(){
try{
ss = new ServerSocket(1024);
//Blocks waiting for the client
System.out.println("Server
s = ss.accept(); // InputStreamReader converts bytes to charecters
br = new BufferedReader(new InputStreamReader(s.getInp
System.out.println(br.read
// You can put the string to text box
}
catch(IOException iExc){
//connection problem report
System.out.println("Proble
iExc.printStackTrace();
}
}
public void actionPerformed(ActionEven
}
public static void main(String args[]) {
SimpleServer server = new SimpleServer();
server.startServer();
//Socket s=null;
//ServerSocket ss=null;
//BufferedReader br = null;
/*try{
ss = new ServerSocket(1024);
//Blocks waiting for the client
System.out.println("Server
s = ss.accept(); // InputStreamReader converts bytes to charecters
br = new BufferedReader(new InputStreamReader(s.getInp
System.out.println(br.read
}
catch(IOException iExc){
//connection problem report
System.out.println("Proble
iExc.printStackTrace();
}*/
}
}
==========================
In this similer way you can put the code in that client to in side the Simpleclient class
ASKER
hey dhool, thanks for your help
sorry about late responding,
but i have some questions:
1.">>// You can put the string to text box"
how can that be done ??? is it done in anctionPerformed method??
i've gone throught some tutorials about java.net(sockets) did't find what i'm aiming for. all i found is just how to establish a connection. there is nothing with sending and reciving strings or whatever by filling a textfield and pressing a button.
i've done somthing like this in the server program
======
public void actionPerformed(ActionEven t e) {
String str1 = null;
String str2 = null;
if (e.getSource == send){
try {
str1 = tf1.getText();
str2 = tf2.getText();
========
and dont know how to complete it
i'm stuck
i'm not sure that its right or wrong!!
2. or do i need to go throught java.io tutorials a bit in depth to sort it out?
thanks guys.
sorry about late responding,
but i have some questions:
1.">>// You can put the string to text box"
how can that be done ??? is it done in anctionPerformed method??
i've gone throught some tutorials about java.net(sockets) did't find what i'm aiming for. all i found is just how to establish a connection. there is nothing with sending and reciving strings or whatever by filling a textfield and pressing a button.
i've done somthing like this in the server program
======
public void actionPerformed(ActionEven
String str1 = null;
String str2 = null;
if (e.getSource == send){
try {
str1 = tf1.getText();
str2 = tf2.getText();
========
and dont know how to complete it
i'm stuck
i'm not sure that its right or wrong!!
2. or do i need to go throught java.io tutorials a bit in depth to sort it out?
thanks guys.
You're almost there. All you need to do is connect this up with the code above:
>>ps.println("Client is connected");
(incidentally, it would be more appropriate to say)
>>ps.println("I am the client connecting...");
Then you can do
public void actionPerformed(ActionEven t e) {
String str1 = null;
String str2 = null;
if (e.getSource == send){
try {
str1 = tf1.getText();
str2 = tf2.getText();
sendString(str1);
sendString(str2);
.....
Connect up a function sendString(String s)
with the PrintWriter 'ps'
>>ps.println("Client is connected");
(incidentally, it would be more appropriate to say)
>>ps.println("I am the client connecting...");
Then you can do
public void actionPerformed(ActionEven
String str1 = null;
String str2 = null;
if (e.getSource == send){
try {
str1 = tf1.getText();
str2 = tf2.getText();
sendString(str1);
sendString(str2);
.....
Connect up a function sendString(String s)
with the PrintWriter 'ps'
ASKER
do you mean something like the following:
======
public void sendString(String s){
PrintWriter ps = null;
try{
ps = new PrintWriter(s.getOutputStr eam(), true);//do we need to put true??!
//true ensures autoflushing
ps.println("I am the client connecting...");
ps.close();// what about these two are they necessary
s.close();
}
catch (IOException Exp){
// i'm not sure what to put here!!
}
//and now actoinPerform
public void actionPerformed(ActionEven t e) {
String str1 = null;
String str2 = null;
if (e.getSource == send){
try {
str1 = tf1.getText();
str2 = tf2.getText();
sendString(str1);
sendString(str2);
// what about catching an exception??!
is that what u ment???
======
public void sendString(String s){
PrintWriter ps = null;
try{
ps = new PrintWriter(s.getOutputStr
//true ensures autoflushing
ps.println("I am the client connecting...");
ps.close();// what about these two are they necessary
s.close();
}
catch (IOException Exp){
// i'm not sure what to put here!!
}
//and now actoinPerform
public void actionPerformed(ActionEven
String str1 = null;
String str2 = null;
if (e.getSource == send){
try {
str1 = tf1.getText();
str2 = tf2.getText();
sendString(str1);
sendString(str2);
// what about catching an exception??!
is that what u ment???
ASKER
do you mean something like the following:
======
public void sendString(String s){
PrintWriter ps = null;
try{
ps = new PrintWriter(s.getOutputStr eam(), true);//do we need to put true??!
//true ensures autoflushing
ps.println("I am the client connecting...");
ps.close();// what about these two are they necessary
s.close();
}
catch (IOException Exp){
// i'm not sure what to put here!!
}
//and now actoinPerform
public void actionPerformed(ActionEven t e) {
String str1 = null;
String str2 = null;
if (e.getSource == send){
try {
str1 = tf1.getText();
str2 = tf2.getText();
sendString(str1);
sendString(str2);
// what about catching an exception??!
is that what u ment???
======
public void sendString(String s){
PrintWriter ps = null;
try{
ps = new PrintWriter(s.getOutputStr
//true ensures autoflushing
ps.println("I am the client connecting...");
ps.close();// what about these two are they necessary
s.close();
}
catch (IOException Exp){
// i'm not sure what to put here!!
}
//and now actoinPerform
public void actionPerformed(ActionEven
String str1 = null;
String str2 = null;
if (e.getSource == send){
try {
str1 = tf1.getText();
str2 = tf2.getText();
sendString(str1);
sendString(str2);
// what about catching an exception??!
is that what u ment???
I haven't got time to answer the individual quetsions, but i'll try and get somebody else to do this. If you can accpet his answer and give him the points as soon as it works please.
ASKER
done ...
;)
if the answer worth the points
i'm waiting ...
;)
if the answer worth the points
i'm waiting ...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi AliciaSv,
Here i have added some code to get (read ) the string an put it to the text box.you can run the server by placing some codes in the comments section. you can also follow the threading for client also.
======the server's source code========
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleServer extends Frame implements ActionListener,Runnable{
//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;
PrintWriter pw=null; // for sending strings
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();
}
public void startServer(){
try{
ss = new ServerSocket(1024);
//Blocks waiting for the client
System.out.println("Server started waiting for client");
s = ss.accept(); // InputStreamReader converts bytes to charecters
br = new BufferedReader(new InputStreamReader(s.getInp utStream() ));
// System.out.println(br.read Line());
pw=new PrintWriter(s.getOutputStr eam());
// You can put the string to text box
Thread thread=new Thread(this);
thread.start(); // to recive the input in a separate thread
}
catch(IOException iExc){
//connection problem report
System.out.println("Proble m with connection");
iExc.printStackTrace();
}
}
public void sendString(String strOut){
pw.println(strOut);
}
public void actionPerformed(ActionEven t e) {
// if action is send
// get string from text box and send it by
// sendstring
}
public void run(){ // run in a separate thread
String readStr=null;
while((readStr=br.readLine ())!=null) {
// assign the readStr to textbox
}
}
public static void main(String args[]) {
SimpleServer server = new SimpleServer();
server.startServer();
}
}
========================== ========== ========== ========== =
Here i have added some code to get (read ) the string an put it to the text box.you can run the server by placing some codes in the comments section. you can also follow the threading for client also.
======the server's source code========
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleServer extends Frame implements ActionListener,Runnable{
//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;
PrintWriter pw=null; // for sending strings
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();
}
public void startServer(){
try{
ss = new ServerSocket(1024);
//Blocks waiting for the client
System.out.println("Server
s = ss.accept(); // InputStreamReader converts bytes to charecters
br = new BufferedReader(new InputStreamReader(s.getInp
// System.out.println(br.read
pw=new PrintWriter(s.getOutputStr
// You can put the string to text box
Thread thread=new Thread(this);
thread.start(); // to recive the input in a separate thread
}
catch(IOException iExc){
//connection problem report
System.out.println("Proble
iExc.printStackTrace();
}
}
public void sendString(String strOut){
pw.println(strOut);
}
public void actionPerformed(ActionEven
// if action is send
// get string from text box and send it by
// sendstring
}
public void run(){ // run in a separate thread
String readStr=null;
while((readStr=br.readLine
// assign the readStr to textbox
}
}
public static void main(String args[]) {
SimpleServer server = new SimpleServer();
server.startServer();
}
}
==========================
ASKER
Hi guys thank for your help, but Ive 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. Im able to send and receive data through the textfields but it just happens once
why is that?!!
2. I dont 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!!!
Ive 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 doesnt 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.
Thats 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. Im able to send and receive data through the textfields but it just happens once
why is that?!!
2. I dont 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!!!
Ive 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 doesnt 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.
Thats all for now thanks.
Waiting .
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.
ASKER
ok as u like ...
but i might come with a different user name coz i aint got more points
;)
but i might come with a different user name coz i aint got more points
;)
no kidding? comparatively speaking, i was wondering what kind of gas milage you get. Because if you can stretch your MPG like you do your PPQ ( points per question(s) ), you could drive to Timbuktu on about a buck fifty. ;-)
Normally, you will want to delcare these variables outside the scope of your try block. This way, you can have a 'finally' block to close the resources and they will still be 'in scope' regardless of what error may have occured in the try block.
If you want to pass data between your sockets, you have to actually write someting to the output stream and read it on the other side. You have to have some convention/protocol for passing data over your sockets and putting it back together on the other side. If all you ever want to pass are text fields, its easy.
Pick some delimiter (CRLF, for instance ) and write the delimited data to the socket and put it back together on the other side using BufferedReader.readLine().