Ashu
Ashu

Reputation: 98

ObjectInputStream readInt() isn't working

I am not able to execute readInt() but readObject is working fine. Suggest me some solutions. I am using following code on server side:

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

My Client side code is:-

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

On executing these I neither the value of 'status' is printing nor the value of 'a'. It just hang. And if I remove readInt() or read() it works fine.

Upvotes: 0

Views: 837

Answers (1)

ELITE
ELITE

Reputation: 5940

try

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

Hope it'll work.

Upvotes: 1

Related Questions