简单的java socket TCP编程 每隔几秒服务器向客户端传时间

时间:2021-06-10 17:47:19

客户端

package javaSocket;
 
import java.io.*;
import java.net.*;

import org.junit.Test;

import jinghai.base.time.LocalDateTime;
import jinghai.base.utils.BufferUtils;
 
public class ClientThread extends Thread {
    private String IP;
    private int port;
    private OutputStream os ;
    private static Socket socket;
    private InputStream is;
    public ClientThread(String IP ,int port) {
        this.IP = IP;
        this.port = port;
    }
 
    @Override
    public void run() {    
        
        try {
            connect();
            doRun();
            while(true) {
                doSleep();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            doClose();
        }
    }
    
    public void connect() throws Exception {
        socket = new Socket(IP,port);
        os = socket.getOutputStream();
        is = socket.getInputStream();  
    }
    
    private void doRun() throws Exception {
        getTime();
        outputThread();                                                
    }
    
    private void outputThread() {
        new Thread(){
            public void run() {
                while(true) {            
                    try {
                        outTime();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                    
            }
                
        }.start();
        
    }

    protected void outTime() throws IOException {
        byte[] bt = new byte[8];                
        is.read(bt);
        transform(bt);
        
        
    }

    private void doSleep() throws Exception {
        sleep(5000);
    }
    
    private void getTime() throws Exception {
        os.write("date".getBytes());
        os.flush();
        
    }

    private void transform(byte[] bt) throws IOException {
        Long l = BufferUtils.toLong(bt, 0, 8);
        LocalDateTime dt = new LocalDateTime(l);    
        show(dt);
    }

    
    
    private void show(LocalDateTime dt) {
        System.out.println(dt);
    }
    
    private void doClose() {
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }
    
    
}
package javaSocket;
 
public class MyClientSocket{
    public static void main(String[] args) {
        System.out.println("客户端");
        ClientThread cThread = new ClientThread("127.0.0.1",5555);
        cThread.start();
 
    }
}
//*******************服务器端线程类********************//
package javaSocket;
 
import java.io.*;
import java.net.*;
import jinghai.base.time.LocalDateTime;
import jinghai.base.utils.BufferUtils;
 
public class ServerThread extends Thread {
    private int port;
    private Socket socket;
    private ServerSocket ssocket;
    private InputStream is;
    private OutputStream os;
    public ServerThread(int port) {
        this.port = port;
    }
 
    @Override
    public void run() {
        try {
            connect();
            doRun();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            doClose();
        }
    }

    private void connect() throws IOException {
         ssocket = new ServerSocket(port);
         socket = ssocket.accept();
         is = socket.getInputStream();
         os = socket.getOutputStream();
    }

    private void doRun() throws Exception {
        byte[] kk = new byte[8];
        int len = 0;
        if ((len=is.read(kk,0,8))<16) {
            while(true) {
                doSleep();
                byte[] bt = dateTime();
                outputTime(bt);
            
        }}
    }
    
    

    private void doSleep() throws InterruptedException {
        sleep(5000);        
    }

    private byte[] dateTime() {
        LocalDateTime dt;
        dt = LocalDateTime.now();
        return BufferUtils.toBytes(dt, 8);
    }
    
    private void outputTime(byte[] bt) throws IOException {
        
        os.write(bt);
        os.flush();
    }
    
    private void doClose() {
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}
package javaSocket;

public class MyServerSocket{
    
    public static void main(String[] args) {
        System.out.println("服务器");
        ServerThread sThread = new ServerThread(5555);
        sThread.start();
    }
}