欢迎来到天天文库
浏览记录
ID:49287260
大小:470.50 KB
页数:48页
时间:2020-02-03
《Java网络编程技术-08.ppt》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、第8章JavaSocket网络编程案例Java网络五子棋程序设计28.1Java网络五子棋需求分析1.应用程序架构:Client/Server2.通信协议:TCP协议3.程序界面3ServerClientClient48.2ServerHashtablekeyvalueclientDataHashSocketDataOutputStreamclientNameHashSochetclientNameclientPeerHashgameCreatorgameJoiner1)集合5publicclassFIRServerextendsFrameimplementsActionListener{JB
2、uttonclearMsgButton=newJButton("清空列表");JButtonserverStatusButton=newJButton("服务器状态");JButtoncloseServerButton=newJButton("关闭服务器");PanelbuttonPanel=newPanel();ServerMsgPanelserverMsgPanel=newServerMsgPanel();ServerSocketserverSocket;HashtableclientDataHash=newHashtable(50);HashtableclientNameHash=new
3、Hashtable(50);HashtablechessPeerHash=newHashtable(50);publicFIRServer(){try{createServer(4331,serverMsgPanel);}catch(Exceptione){e.printStackTrace();}}2)ServerClass6createServer()方法publicvoidcreateServer(intport,ServerMsgPanelserverMsgPanel)throwsIOException{SocketclientSocket;longclientAccessNumber
4、=1;this.serverMsgPanel=serverMsgPaneltry{serverSocket=newServerSocket(port);while(true){clientSocket=serverSocket.accept();DataOutputStreamoutputData=newDataOutputStream(clientSocket.getOutputStream());clientDataHash.put(clientSocket,outputData);clientNameHash.put(clientSocket,("新玩家"+clientAccessNum
5、ber++));FIRServerThreadthread=newFIRServerThread(clientSocket,clientDataHash,clientNameHash,chessPeerHash,serverMsgPanel);thread.start();}}catch(IOExceptionex){ex.printStackTrace();}}73)FIRServerThreadClasspublicclassFIRServerThreadextendsThread{SocketclientSocket;HashtableclientDataHash;Hashtablecl
6、ientNameHash;HashtablechessPeerHash;ServerMsgPanelserverMsgPanel;booleanisClientClosed=false;publicFIRServerThread(SocketclientSocket,HashtableclientDataHash,HashtableclientNameHash,HashtablechessPeerHash,ServerMsgPanelserver){this.clientSocket=clientSocket;this.clientDataHash=clientDataHash;this.cl
7、ientNameHash=clientNameHash;this.chessPeerHash=chessPeerHash;this.serverMsgPanel=server;}}8Feedback()方法publicvoidFeedback(StringfeedBackMsg){synchronized(clientDataHash){DataOutputStreamoutputData=(Da
此文档下载收益归作者所有