public class ServerMain { public static void main(String[] args) throws IOException { SocketAddress address = new InetSocketAddress("localhost",4321); IoAcceptor acceptor = new SocketAcceptor(); IoServiceConfig config = acceptor.getDefaultConfig(); //配置数据的编解码器 config.getFilterChain().addLast("codec", newProtocolCodecFilter(newObjectSerializationCodecFactory())); config.getFilterChain().addLast("logger",newLoggingFilter()); //绑定服务器端口 acceptor.bind(address,newServerHandler()); System.out.println("服务器开始在8000端口监听......."); } }
public class ServerHandler extends IoHandlerAdapter { //创建会话 public void sessionOpened(IoSessionsession) throws Exception { System.out.println("服务器创建了会话 "); session.write("服务器创建会话时发送的信息。"); } //发送信息 public void messageSent(IoSessionsession,Objectmessage) throws Exception { } //接收信息 public void messageReceived(IoSessionsession,Objectmessage) throws Exception { } }
public class ClientMain { public static void main(String[] args) { SocketAddress address = new InetSocketAddress("localhost",4321); IoConnector connector = newSocketConnector(); IoServiceConfig config = connector.getDefaultConfig(); //配置数据的编解码器 config.getFilterChain().addLast("codec", newProtocolCodecFilter(newObjectSerializationCodecFactory())); config.getFilterChain().addLast("logger",newLoggingFilter()); //连接到服务器 connector.connect(address,newClientHandler()); System.out.println("已经连接到了服务器"+address); } }
public class ClientHandler extends IoHandlerAdapter { //发送信息 public void messageSent(IoSessionsession,Objectmessage) throws Exception { } //接收信息 public void messageReceived(IoSessionsession,Objectmessage) throws Exception { System.out.println("客户端接收到的服务器的信息是"+message); } }
public class KFCFoodPriceHandler extends IoHandlerAdapter { //创建会话 public void sessionOpened(IoSessionsession) throws Exception { // System.out.println(" 服务器创建了会话 "); } //接收信息 public void messageReceived(IoSessionsession,Objectmessage) throws Exception { HashMap<String,Object> map = (HashMap<String,Object>)message; String buythings = (String)map.get("购买"); // System.out.println(" 服务器接收到的信息"+buythings); if(buythings.equals("汉堡")) { HashMap<String,Object> map2 = new HashMap<String,Object>(); map2.put("食品","汉堡"); map2.put("价格",4); session.write(map2); } elseif(buythings.equals("鸡翅")) { HashMap<String,Object> map2 = new HashMap<String,Object>(); map2.put("食品","鸡翅"); map2.put("价格",5); session.write(map2); } else { session.write("该种物品已经出售完毕,谢谢惠顾!"); } } }
public class KFCSellerHandler extends IoHandlerAdapter { private Integer childInputMoney_Ham = 4; private Integer childInputMoney_Chick = 4; //创建会话 public void sessionOpened(IoSessionsession) throws Exception { HashMap<String,Object> map = new HashMap<String,Object>(); map.put("购买","汉堡"); session.write(map); } //接收信息 public void messageReceived(IoSessionsession,Objectmessage) throws Exception { //System.out.println(" 客户端接收到的服务器的信息是 " // +(HashMap<String,Object>)message); HashMap<String,Object> priceInfor = (HashMap<String,Object>)message; String foodName = (String)priceInfor.get("食品"); if(foodName.equals("汉堡")) { Integer foodPrice = (Integer)priceInfor.get("价格"); if(foodPrice.equals(childInputMoney_Ham)) { System.out.println(" 您好,请收好你的汉堡,欢迎下次光临!"); } else { System.out.println(" 对不起,你投如的钱币数量不够,钱已经如数归还,请收好!"); } } else if(foodName.equals("鸡翅")) { Integer foodPrice = (Integer)priceInfor.get("价格"); if(foodPrice.equals(childInputMoney_Chick)) { System.out.println(" 您好,请收好你的汉堡,欢迎下次光临!"); } else { System.out.println(" 对不起,你投如的钱币数量不够,钱已经如数归还,请收好!"); } } } }
QQ20160226-5.png (95.45 KB, 下载次数: 1271)
下载附件 保存到相册
8 年前 上传
来源:即时通讯网 - 即时通讯开发者社区!
轻量级开源移动端即时通讯框架。
快速入门 / 性能 / 指南 / 提问
轻量级Web端即时通讯框架。
详细介绍 / 精编源码 / 手册教程
移动端实时音视频框架。
详细介绍 / 性能测试 / 安装体验
基于MobileIMSDK的移动IM系统。
详细介绍 / 产品截图 / 安装体验
一套产品级Web端IM系统。
详细介绍 / 产品截图 / 演示视频
连续任职达3个月以上的合格正式版主
Copyright © 2014-2024 即时通讯网 - 即时通讯开发者社区 / 版本 V4.4
苏州网际时代信息科技有限公司 (苏ICP备16005070号-1)
Processed in 0.120109 second(s), 42 queries , Gzip On.