资源描述:
《java_socket聊天室完整源代码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、说明:本人不才,Java初学者,自己平时也喜欢总结一些东西,然后与大家分享,同时希望得到Java高手的指正和指导,更希望能和广大的Java爱好者交朋友,共同学习Java,分享经验,一起进步。本人QQ:1134135987,非Java爱好者勿加,谢谢!以下代码实现的功能:在一个局域网内,两人在Eclipse的控制台上,可以点对点的进行聊天,并且某一方可以同时输入多句话,以下时实例图片:Server端的示意图:Client端的示意图:otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectoro
2、fthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitt
3、eeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersand5/5实现代码如下:服务端代码:importjava.io.*;importjava.net.*;publicclassServerTest3{publicstaticvoidmain(String[]args)throwsException{//设置sever端的链接ServerSockets=newServerSocket(8888);Sockets1=s.accept();//sever等待链接System.out.println(s1.ge
4、tInetAddress().getHostAddress()+"上线了");//实例化输出流DataOutputStreamdos=newDataOutputStream(s1.getOutputStream());//实例化输入流DataInputStreamdis=newDataInputStream(s1.getInputStream());//实例化两个线程的对象Threadmsr=newMyServerReader(dis);Threadmsw=newMyServerWriter(dos);//启动线程msr.start();msw.start();}}//创建一个线程用
5、来进行接收,读取数据classMyServerReaderextendsThread{privateDataInputStreamdis;publicMyServerReader(DataInputStreamdis){this.dis=dis;}otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNo
6、rthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersand5/5publicvoidrun(){Strin
7、gmsg;try{while(true){msg=dis.readUTF();System.out.println("客户端说:"+msg);if(msg.equals("bye")){System.out.println("客户端下线,程序退出");System.exit(0);}}}catch(IOExceptione){System.out.println(e);}}}//创建一个进程用来写入并发送数据classMyServerWriterexten