?
Solved

client-server question.

Posted on 2003-03-02
22
Medium Priority
?
277 Views
Last Modified: 2010-03-31
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(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();
    }


    public void actionPerformed(ActionEvent 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.getInputStream()));
  System.out.println(br.readLine());
  }

catch(IOException iExc){
  //connection problem report
  System.out.println("Problem 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.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();

         
    }


   
    public void actionPerformed(ActionEvent 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.getOutputStream(), true);
  //true ensures autoflushing
  ps.println("Client is connected");
  ps.close();
  s.close();
}

catch(IOException iExc){
  System.out.println("Problem 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.


0
Comment
Question by:AliciaSV
[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
  • 10
  • 5
  • 4
  • +2
22 Comments
 
LVL 2

Expert Comment

by:functionpointer
ID: 8052118
>>>Socket s=null; // why null?!!

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(). End all your messages with an empty line, and you know when to stop reading by looking for BufferedReader.readLine() length == 0. As far as the data your passing, you can just use name=value, and switch off the name for data assignments.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8052136
>.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)
0
 

Author Comment

by:AliciaSV
ID: 8052183
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?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:functionpointer
ID: 8052223
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 ).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8052224
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.
0
 

Author Comment

by:AliciaSV
ID: 8052327
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.
0
 

Author Comment

by:AliciaSV
ID: 8052354
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?
0
 

Author Comment

by:AliciaSV
ID: 8053695
waiting ...
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8055201
any data you want to pass between the programs is done through the sockets.

One does:
byte[] data;
//load up some data
socket.getOutputStream().write( 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?
0
 

Expert Comment

by:dhool
ID: 8055729
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(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();
   }

   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.getInputStream()));
       System.out.println(br.readLine());




       // You can put the string to text box




 }

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

   }
   public void actionPerformed(ActionEvent 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.getInputStream()));
 System.out.println(br.readLine());
 }

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




In this similer way you can put the code in that client to in side the Simpleclient class
0
 

Author Comment

by:AliciaSV
ID: 8063782
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(ActionEvent 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.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8063815
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(ActionEvent 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'





0
 

Author Comment

by:AliciaSV
ID: 8063986
do you mean something like the following:
======
public void sendString(String s){
PrintWriter ps = null;
 try{
  ps = new PrintWriter(s.getOutputStream(), 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(ActionEvent 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???
0
 

Author Comment

by:AliciaSV
ID: 8064094
do you mean something like the following:
======
public void sendString(String s){
PrintWriter ps = null;
 try{
  ps = new PrintWriter(s.getOutputStream(), 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(ActionEvent 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???
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8064179
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.
0
 

Author Comment

by:AliciaSV
ID: 8064712
done ...
;)
if the answer worth the points
i'm waiting ...
0
 
LVL 35

Accepted Solution

by:
TimYates earned 100 total points
ID: 8064940
Ok.....not 100% sure what's going on here, but I'll try to help if I can :-)

Have you tried this then?  And it doesn't work?

---------------------------------

public void sendString(String s)
{
  PrintWriter ps = null;
  try
  {
    ps = new PrintWriter(s.getOutputStream(), true);//do we need to put true??!
    //true ensures autoflushing
    ps.println("I am the client connecting...");
  }
  catch (IOException Exp)
  {
    // i'm not sure what to put here!!
    Exp.printStackTrace() ;
  }
  finally
  {
    // what about these two are they necessary
    // ps.close() definately is...  if you leave
    // streams and writers open, you tend to run
    // out of system resources quite fast
    try { ps.close(); } catch( Exception ex ) { }
    try { s.close(); } catch( Exception ex ) { }
  }
}

//and now actoinPerform
public void actionPerformed(ActionEvent e)
{
  String str1 = null;
  String str2 = null;

  if (e.getSource == send)
  {
    str1 = tf1.getText();
    str2 = tf2.getText();

    sendString(str1);
    sendString(str2);
    // what about catching an exception??!
    // There won't be one, as you catch the exception
    // inside sendString()
  }
}
0
 

Expert Comment

by:dhool
ID: 8077389
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(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();
  }

  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.getInputStream()));
    //  System.out.println(br.readLine());

       pw=new PrintWriter(s.getOutputStream());


      // 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("Problem with connection");
iExc.printStackTrace();
}

  }

  public void sendString(String strOut){
             pw.println(strOut);
  }
  public void actionPerformed(ActionEvent 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();

}
}
=========================================================
0
 

Author Comment

by:AliciaSV
ID: 8088554
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(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.      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.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.

Thats all for now thanks.

Waiting .

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8088751
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
 

Author Comment

by:AliciaSV
ID: 8088834
ok as u like ...
but i might come with a different user name coz i aint got more points
;)
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8089257
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. ;-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

766 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