您的位置:首页 > 理论基础 > 计算机网络

网络编程之客户端之间的通信实例

2016-12-08 17:10 363 查看
本实例现象效果是多个客户端之间通过一个服务器端来相互通信.

辅助布尔类:

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) {
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  网络编程