TCP客户端和服务器,cmd提示使用对象流

问题描述:

我有一个客户端文件clientRPC.java和服务器文件serverRPC.java。两者都使用TCP协议进行通信,并使用对象输入和输出流传输数据。

I have one client file clientRPC.java and server file serverRPC.java. Both communicate using TCP protocol and use objectinput and output stream to transfer data.

我的客户端文件:

public class clientRPC {

public static void main(String args[]) {

    Socket s = null;

    try {

        int serverPort = 8888;

        s = new Socket("localhost", serverPort);// server name is local host    

        //initializing input and output streams object and referencing them to get input and output
        ObjectInputStream in = null;
        ObjectOutputStream out = null;

        out = new ObjectOutputStream(s.getOutputStream());
        in = new ObjectInputStream(s.getInputStream());

        MathsTutor mt = new MathsTutor();


        out.writeObject(mt);
        out.flush();

        System.out.println("Welcome to Maths Tutor Service. The available maths exercises are:\n"
                + "Addition: Enter 'A' or 'a'\n"
                + "Subtraction: Enter 'S' or 's'\n"
                + "Multiplication: Enter 'M' or 'm'\n"
                + "Division: Enter 'D' or 'd'\n"
                + "Enter 'Q' or 'q' to quit");                       

        //System.out.println();

        MathsTutor mt1 = (MathsTutor) in.readObject();

        String response = in.readUTF();
        System.out.println(response);

    } catch (UnknownHostException e) {
        System.out.println("Socket:" + e.getMessage());
    } catch (EOFException e) {
        System.out.println("EOF:" + e.getMessage());
    } catch (IOException e) {
        System.out.println("readline:" + e.getMessage());
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    } finally {
        if (s != null) {
            try {
                s.close();
            } catch (IOException e) {
                System.out.println("close:" + e.getMessage());
            }
        }
    }
}
}


$ b b

和我的服务器文件:

and my server file :

public class serverRPC extends Thread {

String request;
String response;

public static void main(String args[]) {

    try {
        int serverPort = 8888;
        ServerSocket listen_socket = new ServerSocket(serverPort);

        while (true) {
            Socket clientSocket = listen_socket.accept();
            Connection c = new Connection(clientSocket);
        }
    } catch (IOException e) {
        System.out.println("Listen socket:" + e.getMessage());
    }

public serverRPC(String s) {
    request = s;
}
}

class Connection extends Thread {

ObjectInputStream in;
ObjectOutputStream out;
Socket clientSocket;

public Connection(Socket aClientSocket) {

    try {
        clientSocket = aClientSocket;
        in = new ObjectInputStream(clientSocket.getInputStream());
        out = new ObjectOutputStream(clientSocket.getOutputStream());
        this.start();
    } catch (IOException e) {
        System.out.println("Connection:" + e.getMessage());
    }
}


public void run() {

    try {

        MathsTutor mt = (MathsTutor) in.readObject();
        InetAddress ip = clientSocket.getInetAddress();

        System.out.println("The Received Message from Client at address:/" + ip.getHostAddress());
        System.out.println("====================================");

        MathsTutor mt1 = new MathsTutor();
        out.writeObject(mt1);

         while(true)  {
            // Read from input
            String command = in.readUTF();
            System.out.println(command);

            }

        //System.out.println();

    } catch (EOFException e) {
        System.out.println("EOF:" + e.getMessage());
    } catch (IOException e) {
        System.out.println("readline:" + e.getMessage());
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    } finally {
        try {
            clientSocket.close();
        } catch (IOException e) {/*close failed*/

        }
    }
}
}

问题是当我在cmd上运行server和client时,客户端显示welcome msg,并将光标放在另一行用于用户输入, ,我不能输入任何东西,光标只是闪烁...我知道这可能很简单,但它已经花了3个小时,我被困在同一个东西。

The problem is when I run server and then client on cmd, the client side displays the welcome msg and puts cursor on another line for user input but, I can't type anything, the cursor just blinks... I know this might be simple but it has taken already 3 hours for me and I'm stuck in the same thing.

标有红色的光标不断闪烁,但不允许我输入任何内容。

The cursor marked with red keeps blinking but doesn't let me type anything.

您正在使用 writeObject()编写一个对象, code> readUTF()。 Ilical。

You're writing an object with writeObject() and trying to read it with readUTF(). Illogical.


  • 使用 writeObject()写的对象必须使用 readObject()

  • 使用 writeUTF c $ c> readUTF()

  • 使用 writeXXX code> readXXX()对于大多数X值。

  • objects written with writeObject() must be read with readObject().
  • strings written with writeUTF() must be read with readUTF().
  • primitives written with writeXXX() must be read with readXXX(), for most values of X.