



免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工作中自我情绪管理
- 巨细胞贫血护理要点
- 《建筑工程深厚软土地层基坑施工监测技术规范》编制说明
- 如何提升临床护理水平
- 粒细胞白血病护理
- 专题03 成长故事读后续写-2025年高考英语话题写作高频热点通关攻略(解析版)
- 小儿悬吊牵引护理
- 护理礼仪课汇演
- 网红带货培训体系构建
- 2020gcp考试题库及答案图文
- 2025年新高考数学选填压轴好题汇编(一)(解析版)
- 长距离小直径隧洞TBM施工安全风险评价
- 自考《社会保障概论00071》历年真题和答案
- DL∕T 5210.4-2018 电力建设施工质量验收规程 第4部分:热工仪表及控制装置
- HG+20231-2014化学工业建设项目试车规范
- 江苏省徐州市2023-2024学年八年级下学期期末英语试卷(含答案解析)
- 安徽省阜阳市2023-2024学年七年级下学期期末数学试题
- 急性胰腺炎护理查房课件
- 2024年河南省洛阳市中考第三次模拟考试语文试卷
- 医疗器械(耗材)项目售后服务能力及方案
- FZT 73013-2017 针织泳装行业标准
评论
0/150
提交评论