听ObjectInputStream android

时间:2011-04-18 17:13:47

标签: android sockets listener

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

我现在无法弄清楚的是如何监听发送到ObjectInputStream的对象,以便在发送和接收新对象时我可以对对象的内容进行操作。 那么听一个监听inStream的监听器就是我想要的,可能在另一个线程中,但是怎么样? 任何想法???

我正在使用的代码

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) {}
    }
}

}

ServerActivity.java

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

}

Server.java

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) {}
    }  
}

}

0 个答案:

没有答案