Java&socket实现客户端与客户端的联系

在这里插入图片描述

视频教程连接
server.java

package Server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class server {
    public static List<Mychannel> list=new ArrayList<Mychannel>();

    public static void main(String[] args) throws IOException {
        System.out.println("服务器已开启");
        //创建sercersocket对象
        ServerSocket server=new ServerSocket(9999);
        //监听客户端是否有人连接
        while (true){
            Socket socket= server.accept();
            //创建线程类对象
            Mychannel mychannel=new Mychannel(socket);
            //将连接的mychannel添加到集合中
            list.add(mychannel);
            //启动线程
            new Thread(mychannel).start();
        }
    }
}

CloseUtil.java

package Server;

import java.io.Closeable;
import java.io.IOException;

public class CloseUtil {
    public static void CloseAll(Closeable...able) {
        for (Closeable closeable:able){
            if(closeable!=null){
                try {
                    closeable.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        
    }
}

Mychannel.java

package Server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.List;

public class Mychannel implements Runnable {

    private DataInputStream dis;
    private DataOutputStream dos;
    private boolean flag=true;

    public Mychannel(Socket client)  {
        try {
            dis=new DataInputStream(client.getInputStream());
            dos=new DataOutputStream(client.getOutputStream());
        }catch (IOException e){
            flag=false;
            CloseUtil.CloseAll(dis,dos);
        }
    }

    //接受数据的方法
    private String recevie(){
        String str="";
        try {
            str=dis.readUTF();
        }catch (IOException e){
            flag=false;
            CloseUtil.CloseAll(dis,dos);
            server.list.remove(this);
        }
        return str;
    }
        //发送数据的方法
    private void Send(String str){
        if(str!=null&&str.length()!=0){
            try {
                dos.writeUTF(str);
                dos.flush();
            }catch (IOException e){
                flag=false;
                CloseUtil.CloseAll(dos,dis);
                server.list.remove(this);
            }
        }
    }
    private void sendother(){
        String str=this.recevie();
        List<Mychannel> list=server.list;
        for (Mychannel other:list){
            if (other==this){
                continue;
            }
            other.Send(str);
        }
    }
    @Override
    public void run(){
        while (flag)
            sendother();
    }
}

Client.java

package Client;

import javax.sound.midi.MidiMessage;
import javax.sound.midi.Receiver;
import java.io.IOException;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket client=new Socket("localhost",9999);

        ClientSend clientSend=new ClientSend(client);

        Receive receive=new Receive(client);

        new Thread(clientSend).start();
        new Thread(receive).start();
}
}

ClientSend.java

package Client;

import Server.CloseUtil;


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ClientSend implements Runnable{
    private BufferedReader br;

    private DataOutputStream dos;

    private boolean flag=true;
    public ClientSend(){
        br=new BufferedReader(new InputStreamReader(System.in));
    }
    public ClientSend(Socket client) {
        try {
            br=new BufferedReader(new InputStreamReader(System.in));
            dos=new DataOutputStream(client.getOutputStream());
        }catch (IOException e){
            flag=false;
            CloseUtil.CloseAll(dos,client);
        }
    }
    private String getMessage(){
        String str="";
        try {
            str=br.readLine();
        }catch (IOException e){
            flag=false;
            CloseUtil.CloseAll(br);
        }
        return str;
    }
    public void send(String str){
        try {
            dos.writeUTF(str);
            dos.flush();
        }catch (IOException e){
            flag=false;
            CloseUtil.CloseAll(dos);
        }
    }
    @Override
    public void run() {
        while (flag){
            this.send(getMessage());
        }
    }
}

Receive.java

package Client;


import Server.CloseUtil;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

public class Receive  implements Runnable{
    private DataInputStream dis;
    private boolean flag=true;

    public Receive(Socket client) {
        try {
            dis=new DataInputStream(client.getInputStream());
        }catch (IOException e){
            flag=false;
            CloseUtil.CloseAll(dis,client);
        }
    }
    private String getMessage(){
        String str="";
        try {
            str=dis.readUTF();
        }catch (IOException e){
            flag=false;
            CloseUtil.CloseAll(dis);
        }
        return str;
    }
    @Override
    public void run(){
        while (flag){
            System.out.println(this.getMessage());
        }
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值