android Socket实现简单聊天小程序

android Socket实现简单聊天小程序,第1张

概述androidSocket实现简单聊天程序,供大家参考,具体内容如下服务器端:packageorg.hwq.echo;

androID Socket实现简单聊天小程序,供大家参考,具体内容如下

服务器端:

package org.hwq.echo;  import java.io.BufferedReader; import java.io.IOException; import java.io.inputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.socket;  public class TalkServer {   public static voID main(String[] args) throws IOException{     ServerSocket server = null;     Socket clIEnt = null;     BufferedReader in = null;     PrintWriter out = null;     try{       server = new ServerSocket(4700);       clIEnt = server.accept();       out = new PrintWriter(clIEnt.getoutputStream());       in = new BufferedReader(new inputStreamReader(clIEnt.getinputStream()));       String line = in.readline();       while(!"bye".equals(line)){         System.out.println("clIEnt:"+line);         out.println("echo:"+line);         out.flush();         line = in.readline();       }     }catch (Exception e) {       e.printstacktrace();       if(clIEnt !=null)         clIEnt.close();       if(server != null)         server.close();     }   } } 

手机端:

package org.hwq.cho;  import java.io.BufferedReader; import java.io.IOException; import java.io.inputStreamReader; import java.io.PrintWriter; import java.net.socket; import java.net.UnkNownHostException;  import androID.app.Activity; import androID.content.Context; import androID.os.Bundle; import androID.os.Handler; import androID.os.Message; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; import androID.Widget.EditText; import androID.Widget.Toast;  public class EchoActivity extends Activity implements OnClickListener {   EditText show,msg;   button send;   Handler handler;   Socket clIEnt;   /** Called when the activity is first created. */   @OverrIDe   public voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentVIEw(R.layout.main);     handler = new MyHandler();     show = (EditText) findVIEwByID(R.ID.show);     msg = (EditText) findVIEwByID(R.ID.msg);     send = (button) findVIEwByID(R.ID.send);     send.setonClickListener(this);   }   @OverrIDe   public voID onClick(VIEw arg0) {     String message = msg.getText().toString(); //   System.out.println("msg:"+message);     new EchoThread(EchoActivity.this,message).start();   }   public class MyHandler extends Handler{     @OverrIDe     public voID handleMessage(Message msg) {       switch(msg.what){       case 1:         Toast.makeText(EchoActivity.this,"建立连接失败",0).show();         break;       case 2:         String message = (String) msg.obj;         System.out.println("Handler:"+message);         show.append("\n"+message);         break;       }     }   }   private class EchoThread extends Thread{     private Context context;     private String msg;     EchoThread(Context context,String msg){       this.context = context;       this.msg = msg;     }     public voID run(){       if(clIEnt == null){         try {           clIEnt = new Socket("192.168.1.102",4700);         } catch (IOException e) {           Message message = new Message();           message.what = 1;           handler.sendMessage(message);         }       }       System.out.println("建立连接");       try{         BufferedReader in;         BufferedReader input;         PrintWriter out;         in = new BufferedReader(new inputStreamReader(clIEnt.getinputStream()));         out = new PrintWriter(clIEnt.getoutputStream());         String line = msg;         if(!"bye".equals(line)){           System.out.println("line:"+line);           out.println(line);           out.flush();           String echo = in.readline();           System.out.println("server:"+echo);           Message message = new Message();           message.obj = echo;           message.what = 2;           handler.sendMessage(message);         }       }catch (Exception e) {                }     }   } } 

注意几点:

1、添加网络权限

<uses-permission androID:name="androID.permission.INTERNET"/> 

如果没添加,无法使用socket连接网络。
2、在新启线程中不要使用androID系统UI界面
在EchoThrad的run()方法里面,有下面代码:

if(clIEnt == null){         try {           clIEnt = new Socket("192.168.1.102",4700);         } catch (IOException e) {           Message message = new Message();           message.what = 1;           handler.sendMessage(message);         }       } 

这里的handler.sendMessage(message);是发送一个消息给handler,然后handler根据消息d出一个Toast显示连接失败。如果这里直接使用

Toast.makeText(EchoActivity.this,0).show(); 

会报如下错:

Can't create handler insIDe thread that has not called Looper.prepare() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的android Socket实现简单聊天小程序全部内容,希望文章能够帮你解决android Socket实现简单聊天小程序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1146053.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存