Socket 客户端 服务端 心跳监测

public class ClientHeart extends Thread {
    @Override
    public void run() {
        try {
            while (true) {
                ClientSender.getInstance().send();
                synchronized (ClientHeart.class) {
// this.wait(5000);
                    Thread.sleep(2000);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 程序的入口main方法
     *
     * @param args
     */
    public static void main(String[] args) {
        ClientHeart client = new ClientHeart();
        client.start();
    }
}
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class ClientSender {
    private ClientSender() {
    }
    Socket sender = null;
    private static ClientSender instance;
    public static ClientSender getInstance() {
        if (instance == null) {
            synchronized (ClientHeart.class) {
                instance = new ClientSender();
            }
        }
        return instance;
    }
    public void send() {
        try {
            sender = new Socket(InetAddress.getLocalHost(), 9090);
            while (true) {
                ObjectOutputStream out = new ObjectOutputStream(sender.getOutputStream());
                Entity obj = new Entity();
                obj.setName("xiaoming");
                obj.setSex("男");
                out.writeObject(obj);
                out.flush();
                System.out.println("已发送...");
                Thread.sleep(5000);
            }
        } catch (Exception e) {
        }
    }
}
import java.io.Serializable;
public class Entity implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private String sex;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Entity [name=" + name + ", sex=" + sex + "]";
    }
}
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerHeart extends Thread {
    private ServerSocket server = null;
    Object obj = new Object();
    @Override
    public void run() {
        try {
            server = new ServerSocket(9090);
            while (true) {
                Socket client = server.accept();
                synchronized (obj) {
                    new Thread(new Client(client)).start();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 客户端线程
     *
     * @author USER
     *
     */
    class Client implements Runnable {
        Socket client;
        public Client(Socket client) {
            this.client = client;
        }
        @Override
        public void run() {
            try {
                while (true) {
                    ObjectInput in = new ObjectInputStream(client.getInputStream());
                    Entity entity = (Entity) in.readObject();
                    System.out.println(entity);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 程序的入口main方法
     *
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("开始检测客户端是否在线...");
        new ServerHeart().start();
    }
}

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值