Results 1 to 1 of 1
Thread: Connecting 2 emulator instances
- 06-03-2012, 08:52 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
Connecting 2 emulator instances
I want to connect 2 emulator instances so that I would be able to send simple chat messages between them. For this I set up a server on one emulator and made other emulator a client. So I ran the server on emulator 5554 and the client on 5556.
My server code, listening on 6000:
Java Code:public class NewServerActivity extends Activity { ServerSocket ss; String mClientMsg = ""; Thread myCommsThread = null; protected static final int MSG_ID = 0x1337; public static final int SERVERPORT = 6000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.TextView01); tv.setText("Nothing from client yet"); this.myCommsThread = new Thread(new CommsThread()); this.myCommsThread.start(); } Handler myUpdateHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case MSG_ID: TextView tv = (TextView) findViewById(R.id.TextView01); tv.setText(mClientMsg); break; default: break; } super.handleMessage(msg); } }; class CommsThread implements Runnable { public void run() { //Socket s; try { ss = new ServerSocket(SERVERPORT ); } catch (IOException e) { e.printStackTrace(); } while (!Thread.currentThread().isInterrupted()) { Message m = new Message(); m.what = MSG_ID; try { // if (s == null) Socket s = ss.accept(); BufferedReader input = new BufferedReader( new InputStreamReader(s.getInputStream())); String st = null; st = input.readLine(); mClientMsg = st; myUpdateHandler.sendMessage(m); } catch (IOException e) { e.printStackTrace(); } } } } @Override protected void onStop() { super.onStop(); try { // make sure you close the socket upon exiting ss.close(); } catch (IOException e) { e.printStackTrace(); } } }
I redirected the ports as :
Java Code:telnet localhost 5554 redir add tcp:5000:6000
My client code establshing connection on port 5000:
I ran the server on emulator 5554 and the client on 5556.Java Code:public class NewClientActivity extends Activity { private Button bt; private TextView tv; private Socket socket; private String serverIpAddress = "10.0.2.2"; // AND THAT'S MY DEV'T MACHINE WHERE PACKETS TO // PORT 5000 GET REDIRECTED TO THE SERVER EMULATOR'S // PORT 6000 private static final int REDIRECTED_SERVERPORT = 5000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bt = (Button) findViewById(R.id.myButton); tv = (TextView) findViewById(R.id.myTextView); try { InetAddress serverAddr = InetAddress.getByName(serverIpAddress); socket = new Socket(serverAddr, REDIRECTED_SERVERPORT); } catch (UnknownHostException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } bt.setOnClickListener(new OnClickListener() { public void onClick(View v) { try { EditText et = (EditText) findViewById(R.id.EditText01); String str = et.getText().toString(); PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true); out.println(str); Log.d("Client", "Client sent message"); } catch (UnknownHostException e) { tv.setText("Error1"); e.printStackTrace(); } catch (IOException e) { tv.setText("Error2"); e.printStackTrace(); } catch (Exception e) { tv.setText("Error3"); e.printStackTrace(); } } }); } }
My client side is working fine but unfortunately, on starting the server app the emulator shows a message Unfortunately APPLICATION was stopped. And eclipse shows a java.lang.NullPointerException at "s = ss.accept();" line. Is it somehow related to the configuration of my AVD.
Similar Threads
-
Samsung Emulator
By pabloma2002 in forum Sun Java Wireless ToolkitReplies: 0Last Post: 03-11-2011, 03:35 PM -
How do you go about writing an emulator?
By noahssite in forum Advanced JavaReplies: 2Last Post: 02-11-2011, 07:54 AM -
Emulator
By pabloma2002 in forum New To JavaReplies: 5Last Post: 12-29-2010, 02:33 AM -
Testing on ME Emulator
By phams in forum New To JavaReplies: 3Last Post: 06-16-2010, 04:02 AM -
Emulator Testing
By phams in forum Sun Java Wireless ToolkitReplies: 0Last Post: 06-15-2010, 11:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks