Menu

Client communication question

2003-08-07
2004-03-25
  • Sean Stallbaum

    Sean Stallbaum - 2003-08-07

    Hello again :)

    I have another question....can the client/server only send string data?  What if I wanted to send objects between the two? For instance a request object that contains several fields of differing types?

    Thanks again
    Sean

     
    • Akshathkumar Shetty

      hi,
      Below is an example program [from v1.2 beta - not released yet], but a easier way is being developed [support from with in QuickServer library] and should be available from v1.2 beta

      ////////////////////
      // DateServer.java
      ////////////////////
      package dateserver;

      import com.ddost.net.*;
      import com.ddost.net.server.*;

      import java.io.*;

      public class DateServer {
          public static String VER = "1.0";
          public static void main(String s[])    {
              String cmdHandle = "dateserver.CommandHandler";
              String auth = null;

              QuickServer myServer = new QuickServer(cmdHandle);
              myServer.setServerAuthenticator(auth);
              myServer.setPort(125);
              myServer.setName("Date Server v "+VER);

              try    {
                  myServer.startServer();           
              }catch (AppException e){
                  System.out.println("Error in server : "+e);
              }
          }
      }

      ////////////////////
      // CommandHandler.java
      ////////////////////
      package dateserver;

      import java.net.*;
      import java.io.*;
      import java.util.Date;
      import com.ddost.net.server.ClientCommandHandler;
      import com.ddost.net.server.ClientHandler;

      public class CommandHandler implements ClientCommandHandler {

          public void gotConnected(ClientHandler handler)
              throws SocketTimeoutException, IOException {
              handler.sendSystemMsg("Connection opened : "+
                  handler.getSocket().getInetAddress());

              handler.sendClientMsg("Welcome to DateServer v " +
                  DateServer.VER);
          }

          public void lostConnection(ClientHandler handler)
              throws IOException {
              handler.sendSystemMsg("Connection lost : " +
                  handler.getSocket().getInetAddress());
          }
          public void closingConnection(ClientHandler handler)
              throws IOException {
              handler.sendSystemMsg("Connection closing : " +
                  handler.getSocket().getInetAddress());
          }

          public void handleCommand(ClientHandler handler, String command)
                  throws SocketTimeoutException, IOException {
              if(command.toLowerCase().equals("quit")) {
                  handler.sendClientMsg("Bye ;-)");
                  handler.closeConnection();
              } else if(command.toLowerCase().equals("exchange date")) {
                  ObjectOutputStream oos =
                      new ObjectOutputStream(handler.getOutputStream());
                  oos.writeObject(new Date());
                  oos.flush();
                  oos = null;
                 
                  ObjectInputStream ois =
                      new ObjectInputStream(handler.getInputStream());
                  try    {
                      Date gotDate = (Date) ois.readObject();
                      System.out.println("Got date : " + gotDate);
                  } catch (ClassNotFoundException e) {
                      System.out.println("Unknown object got : "+e);
                  }
                  ois = null;
              } else {
                  System.out.println("Got cmd : " + command);
                  handler.sendClientMsg("You Sent : "+command);
              }
          }
      }

      ////////////////////
      // DateServerClient.java
      // Test code
      ////////////////////
      package dateserver;

      import java.io.*;
      import java.net.*;
      import java.util.*;

      public class DateServerClient {

          public static void main(String args[]) {
              ObjectOutputStream oos;
              ObjectInputStream ois;
              BufferedReader br;
              BufferedWriter bw;
              PrintWriter pw;
              Socket socket;
              int port = 125;
              Date date;
              String send;
              if(args.length<2) {
                  System.err.println("Usage : "+
                  "\n DateServerClient ip_address port");
                  System.exit(0);
              }
              try {
                  port = Integer.parseInt(args[1]);
                  socket = new Socket(args[0], port);
                  System.out.println("Connected");

                  br  = new BufferedReader(
                      new InputStreamReader(socket.getInputStream()));
                  bw  = new BufferedWriter(
                      new OutputStreamWriter(socket.getOutputStream()));
                  pw = new PrintWriter(bw, true);

                  System.out.println("Got : "+br.readLine());

                  send = "hi server";
                  pw.println(send);
                  System.out.println("Got : "+br.readLine());

                  send = "exchange date";
                  pw.println(send);

                  ois = new ObjectInputStream(socket.getInputStream());
                  date = (Date) ois.readObject();
                  System.out.println("Server date is: " + date);
                 
                  oos = new ObjectOutputStream(socket.getOutputStream());
                  date = new Date();
                  oos.writeObject(date);
                  oos.flush();

                  send = "thanks for the date";
                  pw.println(send);
                  System.out.println("Got : "+br.readLine());

                  send = "quit";
                  pw.println(send);
                  System.out.println("Got : "+br.readLine());

                  pw.close();
                  br.close();
                  bw.close();
                  oos.close();
                  ois.close();
              } catch(Exception e) {
                  System.err.println("Error " + e);
              }
          }
      }

      ////////////////////
      //  END of code
      ////////////////////

      enjoy,
      Akshath

       
    • Sean Stallbaum

      Sean Stallbaum - 2003-08-08

      Thanks :)

      I am going to start looking the code over today...

      Do you have a tentative date for v1.2 beta yet?

      Sean

       
    • Akshathkumar Shetty

      hi,
      its planned for 19-Aug-2003, ;-)

      akshath

       
    • Pascal DeMilly

      Pascal DeMilly - 2004-03-24

      What about XMLRPC or SOAP. How could I use QuickServer to provide the ConnectionPooling, remote adminstration and authentication but use XMLRPC for example for Object passing?

      TIA

      Pascal

       
      • Akshathkumar Shetty

        Hi,
        Currently QuickServer also support java object sending and receiving, using which one can implement SOAP or XMLRPC. SOAP or XMLRPC feature is currently not in the list of todo items, though it should be added in near future.

        akshath

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.