-3

Hej,

I've got a Problem with my Messenger I'm writing for Android. The Problem is, that the socket on the Phone AND on the PC connected without any problems. But they cant get the ObjectInputStream as well as the ObjectOuputStream (InputStream and OutputStream works fine :/ ). Not on the Phone nor the PC.

I cant find any solvings on Google; I was searching the whole day.... Is this normal due to some indifferences between the systems?

Thanx a lot for your help ;)

@Override
public void run() {
    inputStream=null;
    outputStream=null;
    System.out.println("Streams initialized");

    try{
        inputStream=new ObjectInputStream(clientSocket.getInputStream());
        System.out.println("Inputstream");
        outputStream=new ObjectOutputStream(clientSocket.getOutputStream());
        System.out.println("Outputstream");

        SendObject sendObject=null;

        System.out.println("Streams get");
        connected=true;


        while(inputStream!=null&&connected){


        }

    }catch (IOException e){
        e.printStackTrace();
    }





}

This is the code on the serverside (int the ThreadHandler), just stopping at the inputStream=new ...

And following, this is the code on the Smartphone

private void initNetwork(){
    try {
        Log.d("Networking","init");
        final int SERVERPORT = 21000;
        final String SERVERADRESS = "192.168.2.22";
        socket = new Socket();

        Log.d("Networking", "new Socket created");
        socket.connect(new InetSocketAddress(SERVERADRESS, SERVERPORT), 3000);
        Log.d("Networking", "Socket connected");
        ois =new ObjectInputStream(socket.getInputStream());
        oos =new ObjectOutputStream(socket.getOutputStream());
        connected = true;
        Log.d("Internet", "Connected");
    }catch (InterruptedIOException e){
        Log.d("Networking","Imeout");
    }catch (IOException e){
        connected=false;
        Log.d("Internet","Not Connected");
        e.printStackTrace();
    }
}

The last log dislpayed by AndroidStudio is Log.d("Networking", "Socket connected");

Ananas1232
  • 11
  • 3
  • Please add your code to the question and error stack traces if you are encountering any. – vk239 Dec 15 '15 at 20:52
  • you do have to share the same definitions for your objects, including the serial id. I assume you did that, but this that few information, that's all I can think of – njzk2 Dec 15 '15 at 20:53
  • There arent any errors, looking like a Timeout :/ – Ananas1232 Dec 15 '15 at 20:55

1 Answers1

1

This works exactly as expected. The constructor for an ObjectInputStream will try to read immediately from the provided stream. Have you tried writing something on the socket?

From the Javadoc of ObjectInputStream(InputStream):

Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

Daniel
  • 4,033
  • 4
  • 24
  • 33
  • The ois and oos on the phone and on the server are created at the same time, after connecting the socket. So therefore they should be able to communicate. But they don't. The progress of initialization just stops in front of the initialization of the ois and oos.... So I can't write any serial data (I've got an object called SendObject for this, that implements serializable) – Ananas1232 Dec 16 '15 at 06:51
  • 1
    I don't understand. If they are running on separate devices, how can you make sure that they are created "at the same time"? Have you tried invoking `oos =new ObjectOutputStream(socket.getOutputStream());` **before** `ois =new ObjectInputStream(socket.getInputStream());`? Otherwise, you would have two threads blocked in their respective constructor. – Daniel Dec 16 '15 at 08:10