android Socket实现简单聊天小程序
程序员文章站
2023-12-04 18:31:22
android socket实现简单聊天小程序,供大家参考,具体内容如下
服务器端:
package org.hwq.echo;
import jav...
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根据消息弹出一个toast显示连接失败。如果这里直接使用
toast.maketext(echoactivity.this, "建立连接失败", 0).show();
会报如下错:
can't create handler inside thread that has not called looper.prepare()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Android中SeekBar拖动条控件使用方法详解
下一篇: Mysql数据库实现多字段过滤的方法