简体   繁体   English

通过套接字Java TCP服务器向Android应用发送对象

[英]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. 我正在编写一个客户端-服务器对,其中服务器是在Linux上运行的Java TCP服务器,客户端是在Android Studio中开发的Android应用。

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. 我已经成功地建立了一个客户端-服务器对,可以彼此发送Message对象,但是当我尝试在Android应用中实现类似功能时,似乎什么也没发生。

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. Android应用程序可以正常工作,而只是通过BufferedReaderPrintwriter类的readLine()println()方法发送Strings ,而不是通过ObjectOutput / InputStream类的readObject()writeObject()发送Strings

Have also tried writeUnshared() / readUnshared() methods without luck. 还尝试了writeUnshared() / readUnshared()方法,但没有readUnshared()运气。

//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. 您需要在两端的ObjectInputStream之前创建ObjectOutputStream 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. readObject()在流的末尾不会返回null,因此使用null作为循环条件没有任何意义。 It can return null any time you send a null. 发送空值时,它可以返回空值。 The loop should terminate when EOFException is caught. 当捕获到EOFException时,循环应终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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