how to call server side class method from client side outputstream in core java tcp

1.2k Views Asked by At

I want to call a method of server side java class from my client side class and pass username password params to it using output stream of client class.

Please let me know how i can achieve it in my code.

below mentioned is my code :

  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.net.UnknownHostException;

  public class TrialClient {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Socket socketOfClient = null;
        PrintWriter writer = null;
        BufferedReader is = null;
        final String serverHost = "192.168.9.150";
        String line;

        try {
               // Send a request to connect to the server is listening
               // on machine 'local host' port 7777.            
               socketOfClient = new Socket(serverHost, 9000);

            // Create output stream at the client (to send data to the server)
               OutputStream output = socketOfClient.getOutputStream();
               writer = new PrintWriter(output, true);

               writer.println("username " + "12345");
                writer.println("password" + "abcd");

               // Input stream at Client (Receive data from the server).
               is = new BufferedReader(new InputStreamReader(socketOfClient.getInputStream()));

               System.out.println("username " + "12345");
               System.out.println("password" + "abcd");
               if((line = is.readLine()) == null) {
                   System.out.println("No response from server !!");
               }else {
               while ((line = is.readLine()) != null) {
                    System.out.println(line);
                }
               }
               socketOfClient.close();
           } catch (UnknownHostException e) {
               System.err.println("Don't know about host " + serverHost);
               return;
           } catch (IOException e) {
               System.err.println("Couldn't get I/O for the connection to " + serverHost);
               return;
           }

    }

  }

and here is the method that i need to call :

public java.lang.String casLogin(int username, java.lang.String password)

Parameters: username - type int - mso username password - type String - mso password

0

There are 0 best solutions below