收听ObjectInputStream android

收听ObjectInputStream android,android,sockets,listener,Android,Sockets,Listener,所以我要做一个回合制游戏,玩家通过网络连接。玩家将发送一个对象,其中包含他们在回合中所做的事情。我已经实现了两个模拟器之间的连接,一个服务器和另一个客户端。我正在使用套接字(TCP) 我现在不明白的是如何监听发送到ObjectInputStream的对象,以便在发送和接收新对象时,我可以对对象的内容进行操作。 所以,也许我想要的是一个监听流内消息的侦听器,可能是另一个线程,但是如何呢? 有什么想法吗 我正在使用的代码 ClientActivity.java: public class Clien

所以我要做一个回合制游戏,玩家通过网络连接。玩家将发送一个对象,其中包含他们在回合中所做的事情。我已经实现了两个模拟器之间的连接,一个服务器和另一个客户端。我正在使用套接字(TCP)

我现在不明白的是如何监听发送到ObjectInputStream的对象,以便在发送和接收新对象时,我可以对对象的内容进行操作。 所以,也许我想要的是一个监听流内消息的侦听器,可能是另一个线程,但是如何呢? 有什么想法吗

我正在使用的代码

ClientActivity.java:

public class ClientActivity extends Activity {
private EditText et;
private Client c;
private TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    et =(EditText)findViewById(R.id.clientTxt);
    tv = (TextView)findViewById(R.id.recievedTxt);

    c = new Client(tv);
    c.start();

    try {
        tv.setText(c.setText());
    } catch (Exception e) {}
}
}

Client.java

public class Client extends Thread {
private final static String TAG ="Client";
private final static String IP = "10.0.2.2";
private final static int PORT = 12345;
private Socket s;
private ObjectOutputStream out;
private ObjectInputStream in;
private TextView tv;

public Client(TextView tv) {
    this.tv = tv;

}

public void run(){
    s = null;
    out = null;
    in = null;

    try {
        s = new Socket(IP, PORT);
        Log.v(TAG, "C: Connected to server" + s.toString());

        out = new ObjectOutputStream(s.getOutputStream());
        in = new ObjectInputStream(s.getInputStream());

        out.writeChars("PING to server from client");

    } catch(IOException e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
            in.close();
            s.close();
        } catch(IOException e) {}
    }
}
public class ServerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new Server().run();
}
public class Server extends Thread {
private static final String TAG = "ServerThread";
private static final int PORT = 12345;
private boolean connected;

public void run() {
    ServerSocket ss = null;
    Socket s = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
        Log.i(TAG, "Start server");
        ss = new ServerSocket(PORT);
        Log.i(TAG, "ServerSocket created waiting for Client..");
        s = ss.accept();
        Log.v(TAG, "Client connected");
        out = new PrintWriter(s.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));

        out.println("Welcome client.."); //send text to client
        String res = in.readLine(); //reading text from client
        Log.i(TAG, "message from client " + res);

    }catch(IOException e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
            in.close();
            s.close();
            ss.close();
        } catch (IOException e) {}
    }  
}
}

ServerActivity.java

public class Client extends Thread {
private final static String TAG ="Client";
private final static String IP = "10.0.2.2";
private final static int PORT = 12345;
private Socket s;
private ObjectOutputStream out;
private ObjectInputStream in;
private TextView tv;

public Client(TextView tv) {
    this.tv = tv;

}

public void run(){
    s = null;
    out = null;
    in = null;

    try {
        s = new Socket(IP, PORT);
        Log.v(TAG, "C: Connected to server" + s.toString());

        out = new ObjectOutputStream(s.getOutputStream());
        in = new ObjectInputStream(s.getInputStream());

        out.writeChars("PING to server from client");

    } catch(IOException e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
            in.close();
            s.close();
        } catch(IOException e) {}
    }
}
public class ServerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new Server().run();
}
public class Server extends Thread {
private static final String TAG = "ServerThread";
private static final int PORT = 12345;
private boolean connected;

public void run() {
    ServerSocket ss = null;
    Socket s = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
        Log.i(TAG, "Start server");
        ss = new ServerSocket(PORT);
        Log.i(TAG, "ServerSocket created waiting for Client..");
        s = ss.accept();
        Log.v(TAG, "Client connected");
        out = new PrintWriter(s.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));

        out.println("Welcome client.."); //send text to client
        String res = in.readLine(); //reading text from client
        Log.i(TAG, "message from client " + res);

    }catch(IOException e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
            in.close();
            s.close();
            ss.close();
        } catch (IOException e) {}
    }  
}
}

Server.java

public class Client extends Thread {
private final static String TAG ="Client";
private final static String IP = "10.0.2.2";
private final static int PORT = 12345;
private Socket s;
private ObjectOutputStream out;
private ObjectInputStream in;
private TextView tv;

public Client(TextView tv) {
    this.tv = tv;

}

public void run(){
    s = null;
    out = null;
    in = null;

    try {
        s = new Socket(IP, PORT);
        Log.v(TAG, "C: Connected to server" + s.toString());

        out = new ObjectOutputStream(s.getOutputStream());
        in = new ObjectInputStream(s.getInputStream());

        out.writeChars("PING to server from client");

    } catch(IOException e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
            in.close();
            s.close();
        } catch(IOException e) {}
    }
}
public class ServerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new Server().run();
}
public class Server extends Thread {
private static final String TAG = "ServerThread";
private static final int PORT = 12345;
private boolean connected;

public void run() {
    ServerSocket ss = null;
    Socket s = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
        Log.i(TAG, "Start server");
        ss = new ServerSocket(PORT);
        Log.i(TAG, "ServerSocket created waiting for Client..");
        s = ss.accept();
        Log.v(TAG, "Client connected");
        out = new PrintWriter(s.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));

        out.println("Welcome client.."); //send text to client
        String res = in.readLine(); //reading text from client
        Log.i(TAG, "message from client " + res);

    }catch(IOException e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
            in.close();
            s.close();
            ss.close();
        } catch (IOException e) {}
    }  
}
}