网络编程之客户端之间的通信实例
2016-12-08 17:10
363 查看
本实例现象效果是多个客户端之间通过一个服务器端来相互通信.
辅助布尔类:
服务器端:
服务器端等新的客户端的加入,将每个加入到客户端的socket收集到集合中。
服务器端的线程实现:
创建与客户端的连接,通过遍历集合实现群发
客户端:
客户端线程实现:
辅助布尔类:
public interface Demo_Continue { public abstract void d_continue(boolean d_continue); }
服务器端:
服务器端等新的客户端的加入,将每个加入到客户端的socket收集到集合中。
public class Demo_Server implements Demo_Continue{ private static List<Socket> list=new ArrayList<Socket>(); boolean d_continue=true; public static List<Socket> getList() { return list; } public static void setList(Socket s) { list.add(s); } public void start(){ try { @SuppressWarnings("resource") ServerSocket serverSocket=new ServerSocket(1212); System.out.println("服务器启动"); Socket socket; Demo_SocketHandler demo_Sockethandler; while(d_continue){ socket=serverSocket.accept(); setList(socket); demo_Sockethandler=new Demo_SocketHandler(socket); demo_Sockethandler.start(); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[]args){ Demo_Server server=new Demo_Server(); server.start(); } @Override public void d_continue(boolean d_continue) { this.d_continue=d_continue; } }
服务器端的线程实现:
创建与客户端的连接,通过遍历集合实现群发
public class Demo_SocketHandler extends Thread{ public boolean ifContinue=true; Socket socket; List<Socket>list; public Demo_SocketHandler(Socket socket){ this.socket=socket; this.list=Demo_Server.getList(); } public void run(){ while(ifContinue){ String str=""; try { BufferedReader input; input = new BufferedReader(new InputStreamReader(socket.getInputStream())); str = input.readLine(); } catch (IOException e1) { // e1.printStackTrace(); } Iterator<Socket> i=list.iterator(); while(i.hasNext()){ try { Socket temp=i.next(); if(temp!=socket){ BufferedWriter output=new BufferedWriter(new OutputStreamWriter(temp.getOutputStream())); if(str!=null&&str!=""){ output.write(str+"\r"); System.out.println("通讯记录"+str); output.flush(); } } } catch (IOException e) { // e.printStackTrace(); } } } } }
客户端:
public class Demo_Client implements Demo_Continue{ boolean d_continue=true; public void start(){ try { System.out.println("一个客户端加入"); Socket socket=new Socket("localhost",1212); BufferedReader input=new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedWriter output=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); Demo_ClientHandle receive=new Demo_ClientHandle(input,this); receive.start(); Scanner sc=new Scanner(System.in); String msg2; while(d_continue){ msg2=sc.nextLine(); output.write(msg2+"\r"); output.flush(); if("bye".equals(msg2)){ this.d_continue=false; } } receive.key=false; sc.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[]args){ Demo_Client client=new Demo_Client(); client.start(); } @Override public void d_continue(boolean d_continue) { this.d_continue=d_continu 4000 e; } }
客户端线程实现:
public class Demo_ClientHandle extends Thread{ public BufferedReader input; public boolean key=true; Demo_Continue d_r; public Demo_ClientHandle(BufferedReader input,Demo_Continue d_r){ this.input=input; this.d_r=d_r; } public void run(){ while(key){ try { String str1= input.readLine(); if("bye".equals(str1)){ d_r.d_continue(false); key=false; } System.out.println(Thread.currentThread().getName()+"发来消息:"+str1); } catch (IOException e) { } } } }
相关文章推荐
- Python网络编程-主机-虚拟机之间TCP协议简单通信实例
- java udp网络编程及实例代码,实现客户端和服务端通信
- python网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
- 【Linux网络编程实例】实例二:利用TCP协议进行客户端与服务器通信
- python网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
- linux下网络编程2:服务端和客户端进行TCP通信实例
- linux 网络编程:客户端与服务器通过TCP协议相互通信 + UDP
- vc 基于对话框多线程编程实例——线程之间的通信
- LInux下网络通信编程实例
- 嵌入式linux下socket网络通信编程实例一
- iOS网络编程实践--蓝牙对等网络通信实例讲解
- udp客户端与服务端之间的通信实例
- iOS网络编程实践--蓝牙对等网络通信实例讲解
- Android 的网络编程(15)-Http JSon服务器端和客户端通信
- 网络编程之即时通信程序(聊天室)------(四)客户端搭建
- 【Windows编程】【网络编程】【基于网络端口通信的客户端应用程序】解决方案【示意程序】
- 网络编程之即时通信程序(聊天室)------(三)客户端登录
- 网络编程之即时通信程序(聊天室)------(三)客户端登录
- LInux 下网络编程之一UDP通信实例详解
- iOS网络编程实践--蓝牙对等网络通信实例讲解