怎么在Java中利用socket实现的客户端和服务端通信功能完(java,socket,编程语言)

时间:2024-05-03 13:48:50 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

客户端实例

ChatClient.java

packagesocketDemo;importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjava.net.*;publicclassChatClientextendsFrame{Sockets=null;DataOutputStreamdos=null;DataInputStreamdis=null;privatebooleanbConnected=false;TextFieldtfTxt=newTextField();TextAreataContent=newTextArea();ThreadtRecv=newThread(newRecvThread());publicstaticvoidmain(String[]args){newChatClient().launchFrame();}publicvoidlaunchFrame(){setLocation(400,300);this.setSize(300,300);add(tfTxt,BorderLayout.SOUTH);add(taContent,BorderLayout.NORTH);pack();this.addWindowListener(newWindowAdapter(){@OverridepublicvoidwindowClosing(WindowEventarg0){disconnect();System.exit(0);}});tfTxt.addActionListener(newTFListener());setVisible(true);connect();tRecv.start();}publicvoidconnect(){try{s=newSocket("localhost",8888);dos=newDataOutputStream(s.getOutputStream());dis=newDataInputStream(s.getInputStream());System.out.println("connected!");bConnected=true;}catch(UnknownHostExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}publicvoiddisconnect(){try{dos.close();dis.close();s.close();}catch(IOExceptione){e.printStackTrace();}/**try{bConnected=false;tRecv.join();}catch(InterruptedException*e){e.printStackTrace();}finally{try{dos.close();dis.close();*s.close();}catch(IOExceptione){e.printStackTrace();}}*/}privateclassTFListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){Stringstr=tfTxt.getText().trim();//taContent.setText(str);tfTxt.setText("");try{//System.out.println(s);dos.writeUTF(str);dos.flush();//dos.close();}catch(IOExceptione1){e1.printStackTrace();}}}privateclassRecvThreadimplementsRunnable{publicvoidrun(){try{while(bConnected){Stringstr=dis.readUTF();//System.out.println(str);taContent.setText(taContent.getText()+str+'\n');}}catch(SocketExceptione){System.out.println("退出了,bye!");}catch(EOFExceptione){System.out.println("推出了,bye-bye!");}catch(IOExceptione){e.printStackTrace();}}}}

socket服务端代码

ChatServer.java

packagesocketDemo;importjava.io.*;importjava.net.*;importjava.util.*;publicclassChatServer{booleanstarted=false;ServerSocketss=null;List<Client>clients=newArrayList<Client>();publicstaticvoidmain(String[]args){newChatServer().start();}publicvoidstart(){try{ss=newServerSocket(8888);started=true;}catch(BindExceptione){System.out.println("端口使用中....");System.out.println("请关掉相关程序并重新运行服务器!");System.exit(0);}catch(IOExceptione){e.printStackTrace();}try{while(started){Sockets=ss.accept();Clientc=newClient(s);System.out.println("aclientconnected!");newThread(c).start();clients.add(c);//dis.close();}}catch(IOExceptione){e.printStackTrace();}finally{try{ss.close();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}classClientimplementsRunnable{privateSockets;privateDataInputStreamdis=null;privateDataOutputStreamdos=null;privatebooleanbConnected=false;publicClient(Sockets){this.s=s;try{dis=newDataInputStream(s.getInputStream());dos=newDataOutputStream(s.getOutputStream());bConnected=true;}catch(IOExceptione){e.printStackTrace();}}publicvoidsend(Stringstr){try{dos.writeUTF(str);}catch(IOExceptione){clients.remove(this);System.out.println("对方退出了!我从List里面去掉了!");//e.printStackTrace();}}publicvoidrun(){try{while(bConnected){Stringstr=dis.readUTF();System.out.println(str);for(inti=0;i<clients.size();i++){Clientc=clients.get(i);c.send(str);//System.out.println("astringsend!");}/**for(Iterator<Client>it=clients.iterator();*it.hasNext();){Clientc=it.next();c.send(str);}*//**Iterator<Client>it=clients.iterator();*while(it.hasNext()){Clientc=it.next();c.send(str);*}*/}}catch(EOFExceptione){System.out.println("Clientclosed!");}catch(IOExceptione){e.printStackTrace();}finally{try{if(dis!=null)dis.close();if(dos!=null)dos.close();if(s!=null){s.close();//s=null;}}catch(IOExceptione1){e1.printStackTrace();}}}}}

本机测试运行结果:

怎么在Java中利用socket实现的客户端和服务端通信功能完

关闭客户端窗口后,提示信息如下:

怎么在Java中利用socket实现的客户端和服务端通信功能完

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:怎么在Java中利用socket实现的客户端和服务端通信功能完的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:爬虫中常见的采集数据方法有哪些下一篇:

18 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18