socket编程 android.doc_第1页
socket编程 android.doc_第2页
socket编程 android.doc_第3页
socket编程 android.doc_第4页
socket编程 android.doc_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Android socket编程 以非阻塞I/O服务器及Service为例介绍Socket通信,有两个不足的地方,1.服务器会造成IO的阻塞即服务器一旦执行server.accept();将一直处于阻塞状态,直到有客户端请求连接。2.服务器端没有建立用户列表,无法将某一客户端发送的消息广播给所有正在连接的客户端。就好象是一个人自说自话,自己发送给客户端,自己接收服务器返回的消息。基于以上两点,我改进了我的程序。服务器端的改进:1.通过采用socketchannel的非阻塞方式进行通信2.建立Userlist客户端的哈希表,存储 已连接客户端的 ip地址和 服务器为其分发的socketchannel客户端的改进:1.采用Service 与服务器端进行连接,发送数据,实时监听服务器返回的数据。流程图:需要改进的地方服务器端:1.当一个客户端断开连接以后,另一个客户端在收到消息之前也断开连接,而此时服务器正在向客户端发送消息,因此,服务器的Thread.sleep时间不能太长,但也不能太短,因为考虑到服务器的负荷问题。2.服务器容错处理机制需要改进。客户端:1.将Notificationbar改为其他更为直观方式刷新显示。2.容错处理机制的处理。下面是效果图:服务器端:dos客户端:Android客户端:效果图的意思是,Android的客户端通过绑定Service与服务器端进行了连接,并发送消息。服务器向所有正在连接的客户端广播消息。之后,Dos终端也进行连接,并发送消息,服务器接到消息后向所有正在连接的客户端广播消息(其中包括在线的android手机)第1页 第2页 第3页 第4页 第5页 第6页 之前采用聊天敲门的方式来介绍Socket通信,有两个不足的地方,1.服务器会造成IO的阻塞即服务器一旦执行server.accept();将一直处于阻塞状态,直到有客户端请求连接。2.服务器端没有建立用户列表,无法将某一客户端发送的消息广播给所有正在连接的客户端。就好象是一个人自说自话,自己发送给客户端,自己接收服务器返回的消息。基于以上两点,我改进了我的程序。服务器端的改进:1.通过采用socketchannel的非阻塞方式进行通信2.建立Userlist客户端的哈希表,存储 已连接客户端的 ip地址和 服务器为其分发的socketchannel客户端的改进:1.采用Service 与服务器端进行连接,发送数据,实时监听服务器返回的数据。流程图:需要改进的地方服务器端:1.当一个客户端断开连接以后,另一个客户端在收到消息之前也断开连接,而此时服务器正在向客户端发送消息,因此,服务器的Thread.sleep时间不能太长,但也不能太短,因为考虑到服务器的负荷问题。2.服务器容错处理机制需要改进。客户端:1.将Notificationbar改为其他更为直观方式刷新显示。2.容错处理机制的处理。下面是效果图:服务器端:dos客户端:Android客户端:效果图的意思是,Android的客户端通过绑定Service与服务器端进行了连接,并发送消息。服务器向所有正在连接的客户端广播消息。之后,Dos终端也进行连接,并发送消息,服务器接到消息后向所有正在连接的客户端广播消息(其中包括在线的android手机)第1页 第2页 第3页 第4页 第5页 第6页 接上页源代码如下:Server端:package com.android.Yao;import java.io.*;import java.nio.*;import java.nio.channels.*;import .*;import java.util.*;import java.nio.charset.*;import java.lang.*;public class YaoChatServerpublic Selector sel = null;public ServerSocketChannel server = null;public SocketChannel socket = null;public int thisport = 4900;private String result = null;private Hashtableuserlists ;private SocketChannel readingsocket = null;public YaoChatServer()System.out.println(Inside startserver );public YaoChatServer(int port)System.out.println(Inside startserver );thisport = port;public void initializeOperations() throws IOException,UnknownHostExceptionSystem.out.println(Inside initialization );sel = Selector.open();server = ServerSocketChannel.open();server.configureBlocking(false);InetAddress ia = InetAddress.getLocalHost();InetSocketAddress isa = new InetSocketAddress(ia,thisport);server.socket().bind(isa);userlists = new Hashtable();public void startServer() throws IOExceptioninitializeOperations();server.register(sel, SelectionKey.OP_ACCEPT);while (sel.select() 0 )Set readyKeys = sel.selectedKeys();Iterator it = readyKeys.iterator();while(it.hasNext()SelectionKey key = (SelectionKey)it.next();it.remove();if (key.isAcceptable()ServerSocketChannel ssc = (ServerSocketChannel) key.channel();socket = (SocketChannel) ssc.accept();socket.configureBlocking(false);String socketname = socket.socket().getRemoteSocketAddress().toString();socket.register(sel, SelectionKey.OP_WRITE);userlists.put(socketname,socket);System.out.println(socketname + is connected!);if (key.isWritable() readingsocket =(SocketChannel)key.channel();String ret=readMessage(readingsocket);if (ret.equalsIgnoreCase( is going to say goodbye!)key.cancel();readingsocket.close();userlists.remove(readingsocket.socket().getRemoteSocketAddress().toString();System.out.println(send server msg:+ret.replace(, readingsocket.socket().getRemoteSocketAddress().toString();sendMessage(ret.replace(, readingsocket.socket().getRemoteSocketAddress().toString();else if (ret.length() 0 ) System.out.println(send server msg:+ret);sendMessage(ret);public void sendMessage(String msg) throws IOExceptionByteBuffer buffer = ByteBuffer.allocate(1024);buffer = ByteBuffer.wrap(msg.getBytes();/ ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes(UTF-8);Collection channels = userlists.values();SocketChannel sc;for(Object o:channels)sc = (SocketChannel)o;sc.write(buffer);buffer.flip();try Thread.sleep(500); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();public String readMessage(SocketChannel sc)int nBytes = 0;ByteBuffer buf = ByteBuffer.allocate(1024);try nBytes = sc.read(buf);buf.flip();Charset charset = Charset.forName(UTF-8);CharsetDecoder decoder = charset.newDecoder();CharBuffer charBuffer = decoder.decode(buf);result = charBuffer.toString(); catch (IOException e) result = is going to say goodbye!;return result;public static void main(String args)YaoChatServer nb = new YaoChatServer();trynb.startServer();catch (IOException e)e.printStackTrace();System.exit(-1);Android客户端的Service类:package com.android.Yao;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import .InetSocketAddress;import java.nio.ByteBuffer;import java.nio.CharBuffer;import java.nio.channels.SocketChannel;import java.nio.charset.CharacterCodingException;import java.nio.charset.Charset;import java.nio.charset.CharsetDecoder;import java.util.Collection;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Context;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class ReceiveMessage extends Serviceprivate SocketChannel client = null;private InetSocketAddress isa = null;private String message=;public void onCreate() super.onCreate();ConnectToServer();StartServerListener();public void onDestroy() super.onDestroy();DisConnectToServer();public void onStart(Intent intent, int startId) super.onStart(intent, startId);/* IBinder方法 , LocalBinder 类,mBinder接口这三项用于* Activity进行Service的绑定,点击发送消息按钮之后触发绑定* 并通过Intent将Activity中的EditText的值* 传送到Service中向服务器发送* */public IBinder onBind(Intent intent) message = intent.getStringExtra(chatmessage);if(message.length()0)SendMessageToServer(message);return mBinder;public class LocalBinder extends Binder ReceiveMessage getService() return ReceiveMessage.this;private final IBinder mBinder = new LocalBinder();/用于链接服务器端public void ConnectToServer()try client = SocketChannel.open();isa = new InetSocketAddress(07,4900);client.connect(isa);client.configureBlocking(false); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();/断开与服务器端的链接public void DisConnectToServer()try client.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();/启动服务器端的监听线程,从Server端接收消息public void StartServerListener()ServerListener a=new ServerListener();a.start();/向Server端发送消息public void SendMessageToServer(String msg)tryByteBuffer bytebuf = ByteBuffer.allocate(1024);bytebuf = ByteBuffer.wrap(msg.getBytes(UTF-8);client.write(bytebuf);bytebuf.flip();catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();private void shownotification(String tab)NotificationManager barmanager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);Notification msg=new Notification(android.R.drawable.stat_notify_chat,A Message Coming!,System.currentTimeMillis();PendingIntent contentIntent=Pe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论