android Socket实现简单聊天小程序
androidSocket实现简单聊天小程序,供大家参考,具体内容如下
服务器端:
packageorg.hwq.echo; importjava.io.BufferedReader; importjava.io.IOException; importjava.io.InputStreamReader; importjava.io.PrintWriter; importjava.net.ServerSocket; importjava.net.Socket; publicclassTalkServer{ publicstaticvoidmain(String[]args)throwsIOException{ ServerSocketserver=null; Socketclient=null; BufferedReaderin=null; PrintWriterout=null; try{ server=newServerSocket(4700); client=server.accept(); out=newPrintWriter(client.getOutputStream()); in=newBufferedReader(newInputStreamReader(client.getInputStream())); Stringline=in.readLine(); while(!"bye".equals(line)){ System.out.println("client:"+line); out.println("echo:"+line); out.flush(); line=in.readLine(); } }catch(Exceptione){ e.printStackTrace(); if(client!=null) client.close(); if(server!=null) server.close(); } } }
手机端:
packageorg.hwq.cho; importjava.io.BufferedReader; importjava.io.IOException; importjava.io.InputStreamReader; importjava.io.PrintWriter; importjava.net.Socket; importjava.net.UnknownHostException; importandroid.app.Activity; importandroid.content.Context; importandroid.os.Bundle; importandroid.os.Handler; importandroid.os.Message; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.Toast; publicclassEchoActivityextendsActivityimplementsOnClickListener{ EditTextshow,msg; Buttonsend; Handlerhandler; Socketclient; /**Calledwhentheactivityisfirstcreated.*/ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); handler=newMyHandler(); show=(EditText)findViewById(R.id.show); msg=(EditText)findViewById(R.id.msg); send=(Button)findViewById(R.id.send); send.setOnClickListener(this); } @Override publicvoidonClick(Viewarg0){ Stringmessage=msg.getText().toString(); //System.out.println("msg:"+message); newEchoThread(EchoActivity.this,message).start(); } publicclassMyHandlerextendsHandler{ @Override publicvoidhandleMessage(Messagemsg){ switch(msg.what){ case1: Toast.makeText(EchoActivity.this,"建立连接失败",0).show(); break; case2: Stringmessage=(String)msg.obj; System.out.println("Handler:"+message); show.append("\n"+message); break; } } } privateclassEchoThreadextendsThread{ privateContextcontext; privateStringmsg; EchoThread(Contextcontext,Stringmsg){ this.context=context; this.msg=msg; } publicvoidrun(){ if(client==null){ try{ client=newSocket("192.168.1.102",4700); }catch(IOExceptione){ Messagemessage=newMessage(); message.what=1; handler.sendMessage(message); } } System.out.println("建立连接"); try{ BufferedReaderin; BufferedReaderinput; PrintWriterout; in=newBufferedReader(newInputStreamReader(client.getInputStream())); out=newPrintWriter(client.getOutputStream()); Stringline=msg; if(!"bye".equals(line)){ System.out.println("line:"+line); out.println(line); out.flush(); Stringecho=in.readLine(); System.out.println("server:"+echo); Messagemessage=newMessage(); message.obj=echo; message.what=2; handler.sendMessage(message); } }catch(Exceptione){ } } } }
注意几点:
1、添加网络权限
如果没添加,无法使用socket连接网络。
2、在新启线程中不要使用android系统UI界面
在EchoThrad的run()方法里面,有下面代码:
if(client==null){ try{ client=newSocket("192.168.1.102",4700); }catch(IOExceptione){ Messagemessage=newMessage(); message.what=1; handler.sendMessage(message); } }
这里的handler.sendMessage(message);是发送一个消息给handler,然后handler根据消息弹出一个Toast显示连接失败。如果这里直接使用
Toast.makeText(EchoActivity.this,"建立连接失败",0).show();
会报如下错:
Can'tcreatehandlerinsidethreadthathasnotcalledLooper.prepare()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。