【问题标题】:ObjectInputStream readInt() isn't workingObjectInputStream readInt() 不工作
【发布时间】:2016-03-19 06:31:17
【问题描述】:

我无法执行 readInt() 但 readObject 工作正常。建议我一些解决方案。 我在服务器端使用以下代码:

ObjectInputStream din = new ObjectInputStream(s.getInputStream());
ObjectOutputStream dout = new ObjectOutputStream(s.getOutputStream());
     s1 = (String) din.readObject();
        switch (s1) {
           case "signin":
                    int a = din.readInt();
                    dout.writeInt(3);
                    System.out.println(a);
                    Mobile = (String) din.readObject();
                    String Pass = (String) din.readObject();
                      dout.writeInt(1);
                    break;
           }

我的客户端代码是:-

 public int getAuthenticate(final String Mobile, final String Pass,final String signin)
{
    m_objThreadClient=new Thread(new Runnable() {
        public void run()
        {

            try
            {
                clientSocket= new Socket(ipAddress,11);
                oos = new ObjectOutputStream(clientSocket.getOutputStream());
                ObjectInputStream ois =new ObjectInputStream(clientSocket.getInputStream());
                oos.writeObject(signin);
                oos.writeInt(2);
                status = ois.readInt();
                System.out.println(status);
                oos.writeObject(Mobile);
                oos.writeObject(Pass);
                status = ois.read();
                System.out.println("Server status="+status);
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

  m_objThreadClient.start();

   try{m_objThreadClient.join();}
   catch (Exception e){e.printStackTrace();}
           return status;
}

在执行这些时,'status' 的值和'a' 的值都不是打印的。它只是挂起。如果我删除 readInt() 或 read() 它工作正常。

【问题讨论】:

  • 你有什么错误吗??
  • 不,只是挂断了
  • 好像在等待输入

标签: java android objectinputstream


【解决方案1】:

试试

oos.writeObject(signin);
oos.flush();
oos.writeInt(2);
oos.flush();

希望它会起作用。

【讨论】:

  • 第一次刷新是浪费时间。
猜你喜欢
  • 2012-01-12
  • 1970-01-01
  • 2013-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多