socket聊天室 楼主去网上下载一个socket聊天室的demo,自己在demo基础上再改改。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 采用NIO来实现,以下是NIO简单示例;你可以通过windows的telnet localhost 8080连接测试.package com.issrv.nioexample;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SelectionKey;import java.nio.channels.Selector;import java.nio.channels.ServerSocketChannel;import java.nio.channels.SocketChannel;import java.util.Iterator;public class NIOExample { public static void main(String[] args) throws IOException { ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); ssc.socket().bind(new InetSocketAddress(8080)); Selector s = Selector.open(); ssc.register(s , SelectionKey.OP_ACCEPT).attach(new Acceptable()); // while(true){ int keyCount = s.select(); if(keyCount > 0){ Iterator<SelectionKey> keys = s.selectedKeys().iterator(); SelectionKey key = keys.next(); keys.remove(); if(!key.isValid()){ continue; } if(key.isAcceptable()){ System.out.println("isAcceptable:"+key.attachment()); ServerSocketChannel sschannel = (ServerSocketChannel)key.channel(); SocketChannel schannel = sschannel.accept(); schannel.configureBlocking(false); schannel.register(s, SelectionKey.OP_WRITE).attach(new Writable()); } if(key.isReadable()){ SocketChannel schannel = (SocketChannel)key.channel(); System.out.println("isReadable:"+key.attachment()); ByteBuffer bytes = ByteBuffer.allocate(4096); int c = schannel.read(bytes); // if(c == -1){ System.out.println("Disconnection!"); key.channel().close(); }else{ System.out.println(new String(bytes.array(),"UTF-8")); } } if(key.isValid() && key.isWritable()){ System.out.println("isWritable:"+key.attachment()); SocketChannel schannel = (SocketChannel)key.channel(); String text = "Welcome to NIO World!"; ByteBuffer bytes = ByteBuffer.wrap(text.getBytes()); schannel.write(bytes); schannel.register(s, SelectionKey.OP_READ).attach(new Readable()); } } } } static class Acceptable{} static class Readable{} static class Writable{}} 仅仅想做IM的话,下载openfire和spark,采用smack来开发XMPP协议聊天. 有资料吗?我有试过用这个,但是担心不够时间看源码,进行二次开发、真心急求。。openfire结合安卓的游戏房间里面的聊天室 有资料吗?我有试过用这个,但是担心不够时间看源码,进行二次开发、真心急求。。openfire结合安卓的游戏房间里面的聊天室网上Openfire和Spark的资料一大把啊,你可以先拿Spark登陆上去看,或者自己把spark源码下载下来放在eclipse运行调试着看,不难的. java lucene做搜索时 庖丁分词器报异常的问题 我刚学struts 能给我一些简单的带数据库的例子吗 问:一日一更新的日志怎么编写呀? jsp刷新记录的问题 关于不同服务器间移植 Struts的validator怎么验证<html:checkbox/> properties文件定位问题 linux下如何安装apache2+tomcat5.5? javawebstudio 老是提示 'ant'不是内部或外部命令 ---请求各位帮助 第一次进入这个论坛,还请多多指教!!! 紧急求助各位大神求指点 servlet程序部署到tomcat上为什么总是连接显示404找不到资源
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
public class NIOExample {
public static void main(String[] args) throws IOException {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
ssc.socket().bind(new InetSocketAddress(8080));
Selector s = Selector.open();
ssc.register(s , SelectionKey.OP_ACCEPT).attach(new Acceptable());
//
while(true){
int keyCount = s.select();
if(keyCount > 0){
Iterator<SelectionKey> keys = s.selectedKeys().iterator();
SelectionKey key = keys.next();
keys.remove();
if(!key.isValid()){
continue;
}
if(key.isAcceptable()){
System.out.println("isAcceptable:"+key.attachment());
ServerSocketChannel sschannel = (ServerSocketChannel)key.channel();
SocketChannel schannel = sschannel.accept();
schannel.configureBlocking(false);
schannel.register(s, SelectionKey.OP_WRITE).attach(new Writable());
}
if(key.isReadable()){
SocketChannel schannel = (SocketChannel)key.channel();
System.out.println("isReadable:"+key.attachment());
ByteBuffer bytes = ByteBuffer.allocate(4096);
int c = schannel.read(bytes);
//
if(c == -1){
System.out.println("Disconnection!");
key.channel().close();
}else{
System.out.println(new String(bytes.array(),"UTF-8"));
}
}
if(key.isValid() && key.isWritable()){
System.out.println("isWritable:"+key.attachment());
SocketChannel schannel = (SocketChannel)key.channel();
String text = "Welcome to NIO World!";
ByteBuffer bytes = ByteBuffer.wrap(text.getBytes());
schannel.write(bytes);
schannel.register(s, SelectionKey.OP_READ).attach(new Readable());
}
}
}
}
static class Acceptable{}
static class Readable{}
static class Writable{}
}
有资料吗?我有试过用这个,但是担心不够时间看源码,进行二次开发、真心急求。。openfire结合安卓的游戏房间里面的聊天室
有资料吗?我有试过用这个,但是担心不够时间看源码,进行二次开发、真心急求。。openfire结合安卓的游戏房间里面的聊天室
网上Openfire和Spark的资料一大把啊,你可以先拿Spark登陆上去看,或者自己把spark源码下载下来放在eclipse运行调试着看,不难的.