免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java聊天室项目的服务器端 server services of chat room by Java基于shocket类似dos界面的importjava.io.*;.*;importjava.util.*;classCustomerimplementsSerializableStringcustname;Stringmsgofsay;Stringflag;publicclassChatServerimplementsRunnableServerSocketserversocket;/创建服务器套接字ThreadserverThread;publicChatServer()/构造符tryserversocket=newServerSocket(1001);/在1001端口监听客户请求catch(IOExceptione)System.out.println(无法启动服务器!+e);tryserverThread=newThread(this);System.out.println(Serverstarted.);serverThread.start();/启动服务器线程catch(Exceptione)System.out.println(不能启动线程+e);publicvoidrun()trywhile(true)Socketfromclient=serversocket.accept();/利用accept()监听客户端请求,并获得客户端套接字Connectioncon=newConnection(fromclient);/与客户端进行通信catch(IOExceptione)System.out.println(服务器无法侦听到客户端!+e);publicstaticvoidmain(Stringargs)newChatServer();classConnectionextendsThreadCustomercustsend;SocketnetClient;ObjectInputStreamstreamfromClient;ObjectOutputStreamstreamtoClient;staticmsgm=newmsg();publicConnection(Socketclient)netClient=client;trystreamfromClient=newObjectInputStream(netClient.getInputStream();/返回InputStream即输入流,该输入流的源是客户端套接字streamtoClient=newObjectOutputStream(netClient.getOutputStream();/返回OutputStream即输出流,该输出流的目标是客户端套接字,将该输出流包装到打印输出流即toClientcatch(IOExceptione)trynetClient.close();/关闭套接字catch(IOExceptione1)System.err.println(客户端已经断开!+e1);custsend=newCustomer();custsend.custname=Server;custsend.flag=Connect;custsend.msgofsay=欢迎进入公共聊天室!;trystreamtoClient.writeObject(custsend);catch(Exceptione2)System.out.println(cantsendtoclient);System.out.println(e2);StreamInsi=newStreamIn(m);/读出msgsi.start();StreamOutso=newStreamOut(m);/写入msgso.start();classStreamInextendsThread/从客户端写入vectorCustomercusttemp;publicStreamIn(msgm)publicvoidrun()trywhile(true)custtemp=(Customer)streamfromClient.readObject();/从客户端读一个对象m.writeMsg(custtemp);System.out.println(custtemp.custname+says:+custtemp.msgofsay);catch(Exceptione)System.out.println(服务器与客户端断开了,请重新打开客户端!+e);classStreamOutextendsThread/读出vector到客户端intlCount=0;Customercusttemp;publicStreamOut(msgm)publicvoidrun()trywhile(true)custtemp=m.readMsg(lCount);lCount+;streamtoClient.writeObject(custtemp);catch(Exceptione1)custtemp.custname=custtemp.custname;custtemp.flag=Logout;custtemp.msgofsay=已经断线离开了!;m.writeMsg(custtemp);trynetClient.close();catch(IOExceptionioe)System.out.println(cantclosessocket);System.out.println(ioe);System.out.println(不能发送到客户端!+e1);staticclassmsgstaticintmsgCount=0;staticVectormsgBox=newVector(1,1);publicsynchronizedvoidwriteMsg(Customertemp)msgBox.addElement(temp);msgCount+;notifyAll();publicsynchronizedCustomerreadMsg(intlCount)Customertemp;while(lCount=msgCount)trywait();catch(InterruptedExceptioni
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年江苏省镇江市中考语文真题卷含答案解析
- 幼儿园保育工作计划总结
- 2025年楚雄市高压电工证理论考试练习题含答案
- 安环部员工2025年度工作总结模版
- 小学六年级语文教师教学工作总结
- 脚手架工程量计算方法
- 2025年市场监督管理局业务考试复习题集及答案解析
- 花卉栽培试题库及答案
- 2025年社区公共卫生服务培训试题集含答案
- 电工三级(高级工)试题含答案
- 2025年大学大一(法学)法理学试题及答案
- 胆囊癌课件教学课件
- 广西2025年高等职业教育考试全区模拟测试 能源动力与材料 大类试题及逐题答案解说
- 2026江苏省公务员考试公安机关公务员(人民警察)历年真题汇编附答案解析
- 孕妇贫血教学课件
- 超市冷库应急预案(3篇)
- 5年(2021-2025)山东高考生物真题分类汇编:专题17 基因工程(解析版)
- 2025年10月自考00610高级日语(二)试题及答案
- 新华资产招聘笔试题库2025
- 2025年中国潜孔钻机行业细分市场研究及重点企业深度调查分析报告
- 食品经营场所及设施设备清洗消毒和维修保养制度
评论
0/150
提交评论