Send objects between devices and acting on them, listener?

Put your problem here if it does not fit any of the other categories.

Send objects between devices and acting on them, listener?

Postby Mizzhell » Mon Apr 18, 2011 5:50 pm

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???

Here's my code which I'm using

ClientActivity.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ClientActivity extends Activity {
  2.     private EditText et;
  3.     private Client c;
  4.     private TextView tv;
  5.     /** Called when the activity is first created. */
  6.     @Override
  7.     public void onCreate(Bundle savedInstanceState)
  8.     {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.main);
  11.         et =(EditText)findViewById(R.id.clientTxt);
  12.         tv = (TextView)findViewById(R.id.recievedTxt);
  13.  
  14.         c = new Client(tv);
  15.         c.start();
  16.  
  17.         try {
  18.             tv.setText(c.setText());
  19.         } catch (Exception e) {}
  20.  
  21.  
  22.     }
  23. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Client.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Client extends Thread {
  2.     private final static String TAG ="Client";
  3.     private final static String IP = "10.0.2.2";
  4.     private final static int PORT = 12345;
  5.     private Socket s;
  6.     private ObjectOutputStream out;
  7.     private ObjectInputStream in;
  8.     private TextView tv;
  9.  
  10.     public Client(TextView tv) {
  11.         this.tv = tv;
  12.  
  13.     }
  14.  
  15.     public void run(){
  16.         s = null;
  17.         out = null;
  18.         in = null;
  19.  
  20.         try {
  21.             s = new Socket(IP, PORT);
  22.             Log.v(TAG, "C: Connected to server" + s.toString());
  23.  
  24.             out = new ObjectOutputStream(s.getOutputStream());
  25.             in = new ObjectInputStream(s.getInputStream());
  26.  
  27.             out.writeChars("PING to server from client");
  28.  
  29.         } catch(IOException e) {
  30.             e.printStackTrace();
  31.         } finally {
  32.             try {
  33.                 out.close();
  34.                 in.close();
  35.                 s.close();
  36.             } catch(IOException e) {}
  37.         }
  38.     }
  39. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Server.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Server extends Thread {
  2.     private static final String TAG = "ServerThread";
  3.     private static final int PORT = 12345;
  4.     private boolean connected;
  5.  
  6.     public void run() {
  7.         ServerSocket ss = null;
  8.         Socket s = null;
  9.         PrintWriter out = null;
  10.         BufferedReader in = null;
  11.  
  12.         try {
  13.             Log.i(TAG, "Start server");
  14.             ss = new ServerSocket(PORT);
  15.             Log.i(TAG, "ServerSocket created waiting for Client..");
  16.             s = ss.accept();
  17.             Log.v(TAG, "Client connected");
  18.             out = new PrintWriter(s.getOutputStream(), true);
  19.             in = new BufferedReader(new InputStreamReader(s.getInputStream()));
  20.  
  21.             out.println("Welcome client.."); //send text to client
  22.             String res = in.readLine(); //reading text from client
  23.             Log.i(TAG, "message from client " + res);
  24.  
  25.         }catch(IOException e) {
  26.             e.printStackTrace();
  27.         } finally {
  28.             try {
  29.                 out.close();
  30.                 in.close();
  31.                 s.close();
  32.                 ss.close();
  33.             } catch (IOException e) {}
  34.         }  
  35.     }
  36. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


ServerActivity.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ServerActivity extends Activity {
  2.     /** Called when the activity is first created. */
  3.     @Override
  4.     public void onCreate(Bundle savedInstanceState)
  5.     {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.main);
  8.  
  9.         new Server().run();
  10.     }
  11. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
Mizzhell
Freshman
Freshman
 
Posts: 2
Joined: Tue Apr 12, 2011 12:21 pm

Top

Re: Send objects between devices and acting on them, listene

Postby AedonEtLIRA » Mon Apr 18, 2011 10:48 pm

Try packing all of your data into a bundle if you can. Break the bundle into a byte array. Create a buffer array of xxx bytes. Send the buffer to the other device that will take the buffer and repackage the bytes into a bundle. I have no idea if this efficient but I think it should work for you.
Walk with purpose, and build something.
User avatar
AedonEtLIRA
Senior Developer
Senior Developer
 
Posts: 146
Joined: Wed Jul 28, 2010 11:52 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 19 guests