yzt
2023-05-26 2f70f6727314edd84d8ec2bfe3ce832803f1ea77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.hxzkoa.tools;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CountDownLatch;
 
import javax.annotation.PostConstruct;
 
import org.springframework.stereotype.Component;
 
import com.hxzkoa.util.StringUtil2;
 
@Component
public class SocketUtil {
    
    static Socket socket = new Socket();
    
    @PostConstruct
    public void init() {
        try {
            final ServerSocket serverSocket = new ServerSocket(8888);
//            System.out.println("Æô¶¯·þÎñÆ÷......");
            Thread thread = new Thread(
                    new Runnable() {
                        public void run() {
                            while (true) {
                                try {
                                    socket = serverSocket.accept();
//                                    System.out.println(socket.getRemoteSocketAddress());
//                                    System.out.println("¿Í»§¶Ë:"+socket.getInetAddress().getHostAddress()+"ÒÑÁ¬½Óµ½·þÎñÆ÷");
                                    System.out.println(socket.getRemoteSocketAddress());
                                    final BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                                    CountDownLatch countDownLatch = new CountDownLatch(1);
                                    Timer timer = new Timer();
                                    TimerTask task = new TimerTask() {
                                        @Override
                                        public void run() {
                                            String msg;
                                            try {
                                                msg = br.readLine();
                                                if (StringUtil2.isNotEmpty(msg)) {
                                                    System.out.println("------"+msg);
                                                }
                                            } catch (IOException e) {
                                                // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
                                                e.printStackTrace();
                                            }
                                        }
                                    };
                                    timer.schedule(task, 0, 5000);
                                    new SocketUtil().sendmsg("ok");
                                } catch (Exception e) {
 
                                }
                            }
                        }
                    });
            thread.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void sendmsg(String msg) {
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(msg+"\n");
            bw.flush();
//            System.out.println("·¢ËÍÍê³É");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    public void receivemsg() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String msg;
            while (true) {
                msg = br.readLine();
                if (StringUtil2.isNotEmpty(msg)) {
//                    System.out.println("É豸·¢À´ÏûÏ¢:"+msg);
                    System.out.println("------"+msg);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
}