Java聊天程序的实现

<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> Java聊天程序的实现 作者:Leon 日期: 2002-1-17 简述: 此文介绍的基于java 的聊天程序主要是利用java的DataInputStream和PrintStream类来实现服务器和客户端的数据流通信,用DataInputStream来接受数据,用PrintStream来发送数据。另外还涉及多线程的技术来实现多个客户同时连接服务器。 本程序包括服务器端的Application和客户端的Applet,开发环境为Windows2000,JDK1.3,Jcreator2.0。 服务器端程序用多线程实现了可以接受多个客户连接请求,并显示连接客户的ip信息和当前的连接数;而客户端可以通过输入用户名来取得服务器连接,并在客户list中显示所有连接客户的姓名。下面把主要程序源码附在下面,有详细注释
<图1> 源程序1:chatServer.java
src='http://www.77750.com/download/chat1.html' width=500 height=400>
<图2> 源程序2:chatApplet.java
src='http://www.77750.com/download/chat2.html' width=500 height=400>
<图3> 源程序3:chatServer_AboutBox.java
src='http://www.77750.com/download/chat3.html' width=500 height=400>
以上程序代码只是实现了聊天室的基本功能,可以扩展的地方还有很多,比如添加入数据库功能,和CGI程序交互,从CGI程序获得用户和其他一些信息,实现普通网站的聊天室功能。读者可以自己试着去添加功能使得chat程序更加完美,更加实用。本文仅起参考作用。有什么问题可以和 leon405@263.net联系。 打包下载代码
摘 要 随着互联网的快速发展,网络聊天工具已经作为一种重要的信息交流工具,受到越来越多的网民的青睐。目前,出现了很多非常不错的聊天工具,其中应用比较广泛的有Netmeeting、腾讯QQ、MSN-Messager等等。该系统开发主要包括一个网络聊天服务器程序和一个网络聊天客户程序两个方面。前者通过Socket套接字建立服务器服务器能读取、转发客户端发来信息,并能刷新用户列表。后者通过与服务器建立连接,来进行客户端与客户端的信息交流。其中用到了局域网通信机制的原理,通过直接继承Thread类来建立多线程。开发中利用了计算机网络编程的基本理论知识,如TCP/IP协议、客户端/服务器端模式(Client/Server模式)、网络编程的设计方法等。在网络编程中对信息的读取、发送,是利用流来实现信息的交换,其中介绍了对实现一个系统的信息流的分析,包含了一些基本的软件工程的方法。经过分析这些情况,该局域网聊天工具采用Eclipse为基本开发环境和java语言进行编写,首先可在短时间内建立系统应用原型,然后,对初始原型系统进行不断修正和改进,直到形成可行系统 关键词:局域网 聊天 socket java 聊天系统各功能模块 (1)服务器程序模块 服务器与客户间通过套接口Socket(TCP)连接。在java中使用套接口相当简单,Java API为处理套接口的通信提供了一个类java.net.Socket,使得编写网络应用程序相对容易。服务器采用多线程以满足多用户的请求,并通过创建一个ServerSocket对象来监听来自客户的连接请求,默认端口为9527,然后无限循环调用accept()方法接受客户程序的连接。 服务器线程源码: package qq.server; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.*; import qq.dao.hibernate.IServiceDao; import qq.entity.*; public class ServerController { private User user; private Socket s; private IServiceDao dao; private ObjectInputStream ois; private ObjectOutputStream oos; private OnlineUser onlineUser; www.bylw120.com public ServerController(Socket s) { super(); dao=ServerMainClass.userDao; this.s = s; } public void handle() throws Exception { ois=new ObjectInputStream(s.getInputStream()); oos=new ObjectOutputStream(s.getOutputStream()); onlineUser=new OnlineUser(ois,oos); while(true){ Request req=(Request)ois.readObject(); ois.read(); RequestType type=req.getType(); if(type.equals(RequestType.exit)){ exitHandle(); break; }else if(type.equals(RequestType.login)){ loginHandle(req); }else if(type.equals(RequestType.register)){ registerHandle(); }else if(type.equals(RequestType.offline)){ offlineHandle(); break; }else if(type.equals(RequestType.changeInformation)){ changeInformationHandle(); }else if(type.equals(RequestType.modifypasswd)){ modifypasswdHandle(req); }else if(type.equals(RequestType.sendMessage)){ sendMessageHandle(req); }else if(type.equals(RequestType.receiveFile)){ receiveFileHandle(req); }else if(type.equals(RequestType.sendFile)){ sendFileHandle(req); } } } private void modifypasswdHandle(Request req) { Long id=Long.parseLong(req.getData("id")); String oldpwd=req.getData("oldpwd"); String newpwd=req.getData("newpwd"); Response res=new Response(RequestType.modifypasswd); try { dao.updatePwd(id, oldpwd, newpwd); res.setData(1); try { oos.writeObject(res); } catch (IOException e) { e.printStackTrace(); } } catch (RuntimeException e) { try { oos.writeObject(res); } catch (IOException e1) { e1.printStackTrace(); } } } private void changeInformationHandle() { try { User user=(User)ois.readObject(); Response res=new Response(RequestType.changeInformation); try { dao.updateUser(user); res.setData(1);//修改成功返回值带一个整形值 oos.writeObject(res); oos.flush(); } catch (RuntimeException e) { oos.writeObject(res);//失败则返回值不带参数 oos.flush(); e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } private void exitHandle() { try { s.close(); } catch (IOException e) { e.printStackTrace(); } } //发送文件 private void sendFileHandle(Request req) { // try { // User u=(User)ois.readObject(); // // } catch (Exception e) { // e.printStackTrace(); // } } //接受文件 private void receiveFileHandle(Request req) {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值