简体   繁体   中英

Sending objects through sockets java TCP server to android app

I'm writing a client-server pair where the server is a java TCP server running on Linux and the client is an Android app developed in Android Studio.

I've successfully made a client-server pair that sends Message objects to each other, but when I try to implement similar functionality in my Android app nothing seems to happen.

The Android app works while just sending Strings with the readLine() and println() methods from the BufferedReader and Printwriter classes, but not with the readObject() and writeObject() from ObjectOutput / InputStream classes.

Have also tried writeUnshared() / readUnshared() methods without luck.

//Message.java
package Message;
import java.io.*;

public class Message implements Serializable {
    String msg;
    String tag;
    String username;

    private static final long serialVersionUID = 4L;
        // Methods.
    }

    // Reading MessageObjects in Server.java.
    @Override
    public void run() {
        Message message = null;
        try {
            while ((message = (Message)reader.readObject()) != null) {
                // Processing message.
            }
        }
    }

    // Sending Message Objects in android App.
    public void onClick(View v) {
        if(!msgBox.getText().toString().equals("")) {
            final String msg;
            try {
                msg = msgBox.getText().toString();
                writer.writeObject(new Message(msg, CLIENT, username));
                msgBox.setText("");
                writer.flush();
                // Updating ui etc.
            }
            catch (IOException e) {
                 e.printStackTrace();
            }
            textBox.smoothScrollBy(textBox.getMaxScrollAmount(), 100);
        }
    }

    // Connection-method in android app, initalizes streams.
    private boolean connect(String username, String address, int port) {
        boolean connected = false;
        try {
            server = new Socket(address, port);
            InputStreamReader(server.getInputStream());
            reader = new ObjectInputStream(server.getInputStream());
            writer = new ObjectOutputStream(server.getOutputStream());
            writer.writeObject(new Message("!newUser",AUTOMATED,username));
            writer.flush();
            connected = true;
            System.out.println("Connected!");
        }
        catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Cannot Connect!");
            connected = false;
            // UI-things.
        }
        if(connected){
            // Thread that listens for replies.
            listenThread(); 
        }
        return connected;
    }
  • You need to create the ObjectOutputStream before the ObjectInputStream , at both ends. Otherwise you can get a deadlock.
  • Your read loop is incorrect. readObject() doesn't return null at end of stream, so using null as a loop condition doesn't make sense. It can return null any time you send a null. The loop should terminate when EOFException is caught.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM