Listening to ObjectInputStream android
So I'm going to make a turn-based game where the players connect through network. The players will send an object containing what they did their round. I have gotten the connection between two emulators to work, one server and the other client. I'm using sockets, (TCP).
What I can't figure out now is how to listen for objects that are sent to the ObjectInputStream, so that when a new object is sent and received I can act on the contents of the objects. So maybe a listener which listens to the inStream is what I want, probably in another thread, but how? Any Ideas???
The code I'm using
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 {
开发者_StackOverflow中文版 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) {}
}
}
}
精彩评论