首页 > 解决方案 > 如何从核心java tcp中的客户端输出流调用服务器端类方法

问题描述

我想从我的客户端类调用服务器端 java 类的方法,并使用客户端类的输出流将用户名密码参数传递给它。

请让我知道如何在我的代码中实现它。

下面提到的是我的代码:

  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;
           }

    }

  }

这是我需要调用的方法:

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

参数:用户名——int类型——mso用户名密码——字符串类型——mso密码

标签: javatcpserversockettclientsocket

解决方案


推荐阅读