我想在2 Emulator中创建一个Server和一个ClIEnt来写入和读取数据.
我为Server编写代码:
public class ServerActivity extends Activity { /** Called when the activity is first created. */ private ServerSocket serverSocket = null; private TextVIEw tv; public static final int SERVERPORT = 4444; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); tv= (TextVIEw) findVIEwByID(R.ID.myTextVIEw); try { Connect(); } catch (IOException e) { // Todo auto-generated catch block tv.setText("Not connected"); e.printstacktrace(); } } public voID Connect() throws IOException { serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress("10.0.2.15", 4444)); while(true) { Socket socket = serverSocket.accept(); tv.setText("Connected..."); } }
和客户代码
public class ClIEntActivity extends Activity { /** Called when the activity is first created. */ private button bt; private TextVIEw tv; @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); bt.setonClickListener(new OnClickListener() { public voID onClick(VIEw v) { // Todo auto-generated method stub try { Socket socket = new Socket("10.0.2.2", 4445); } catch (UnkNownHostException e) { // Todo auto-generated catch block tv.setText("Error1"); e.printstacktrace(); } catch (IOException e) { // Todo auto-generated catch block tv.setText("Error2"); e.printstacktrace(); } } }); }}
我设置了一个重定向:
telnet localhost 5554redir add tcp:4445:4444
但它没有连接….请帮助我.我很感激.
解决方法:
过了一会儿,我成功了.我没有在服务器或客户端对10.0.2.15做任何引用.我以不同方式打开服务器套接字,并在单独的线程中处理了通信.我在模拟器5554上运行服务器,在5556上运行客户端.
我的服务器代码,听6000
public class SocketServer extends Activity { ServerSocket ss = null; 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(); } @OverrIDe protected voID onStop() { super.onStop(); try { // make sure you close the socket upon exiting ss.close(); } catch (IOException e) { e.printstacktrace(); } } 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 = null; 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) 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(); } } } }}
我重定向了类似于你的端口
telnet localhost 5554redir add tcp:5000:6000
我的客户端代码在端口5000上建立连接:
public class SocketClIEnt 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(); } } }); }}
服务器有一个TextVIEw来接收消息,客户端有一个EditText来组成消息,一个按钮来发送它.
总结以上是内存溢出为你收集整理的在Android中连接2个仿真器实例全部内容,希望文章能够帮你解决在Android中连接2个仿真器实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)