通过套接字发送对象和字符串

时间:2018-04-04 15:19:33

标签: java java-io

我已经尝试了几种方法将字符串然后一个对象发送到我的服务器并让我的服务器doSomething()然后将一个字符串和一个对象发送回客户端,但截至目前服务器甚至没有收到字符串。以下是我目前的方法。

完成发送/接收的服务器代码的一部分:

try
        {

            ObjectInputStream fromClientObject = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream toClientObject = new ObjectOutputStream(socket.getOutputStream());

            while(!socket.isClosed())
            {
                //If server has received a message
                if(fromClientObject.available() > 0)
                {
                    //Set received message to string and print
                    String input = fromClientObject.readUTF();
                    System.out.println(input);

                    //If clients sets .register command then register new user
                    if (input.equals(".register"))
                    {
                        Object obj = fromClientObject.readObject();
                        Users user = (Users) obj;
                        server.registerUsers(user);
                    }

                    //If clients sends .findUser command then see if user exists in DB
                    if (input.equals(".findUser"))
                    {
                        //Read object from client
                        Object obj = fromClientObject.readObject();
                        Users user = (Users) obj;

                        //Create a pair and find the user
                        Pair<Boolean,Users> findUsers;
                        findUsers = server.findUsers(user); //This checks if the user exists and returns true or false

                        if(findUsers.getFirst())
                        {
                            toClientObject.writeUTF("True");
                            toClientObject.flush();

                            toClientObject.writeObject(findUsers.getSecond()); //Send the found user object to the client
                            toClientObject.flush();
                        }
                        else
                        {
                            toClientObject.writeUTF("false");
                            toClientObject.flush();
                        }
                    }
                }
            }
        }
        catch (IOException | ClassNotFoundException e)
        {
            e.printStackTrace();
        }

我也尝试使用Scanner来接收字符串,并使用单独的ObjectInputStream来接收对象,但无济于事。

以下是客户端如何接收从服务器发送的字符串和对象以及它如何向服务器发送信息

try(Socket socket = new Socket(host, portNumber);
    ObjectInputStream fromServerObject = new ObjectInputStream(socket.getInputStream());
    ObjectOutputStream toServerObject  = new ObjectOutputStream(socket.getOutputStream()))
    {
        Thread.sleep(1000);

        //Setup I/O
        toServerObject.writeUTF(code); //This is the string
        toServerObject.flush();

        toServerObject.writeObject(user); //This is a serialised object
        toServerObject.flush();


        if (code.equals(".findUser"))
        {
            String input = fromServerObject.readUTF();
            Users readUser = (Users) fromServerObject.readObject();

            if (input.equals("True"))
            {
                updateMessage("True");
                updateValue(readUser);
            } else if (input.equals("false"))
            {
                updateMessage("False");
            }
        } else
        {
            updateMessage("True");
        }
    }
    catch (IOException e)
    {
        System.err.println("Fatal Connection error!");
        e.printStackTrace();
        updateMessage("Failed");
    }
    return null;

有关如何规避此事的任何想法?我正在考虑制作单独的函数来编写一个消息/接收和一个单独的写入和接收和对象,但是如果没有得到我当前实现的输入,就不会尝试它。

1 个答案:

答案 0 :(得分:0)

here

所解释的那样,流的排序似乎解决了这个问题

服务器意味着接收然后发送,因此它将是

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

而客户端意味着发送然后接收 所以顺序是相反的