server_services_of_chat_room_by_Java.doc_第1页
server_services_of_chat_room_by_Java.doc_第2页
server_services_of_chat_room_by_Java.doc_第3页
server_services_of_chat_room_by_Java.doc_第4页
免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论