zsh_root
2024-01-02 7b595546af704983dbafcd0d385c8768ddacefc2
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
package PbuliClass;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
 
import Frame.TcpIpManage;
 
public class TcpServer implements Runnable {
 
    int port = 50000;    
    ServerSocket server = null;    
    Socket client = null;
    String recivemessage="";
    //¹¹Ôì·½·¨
    public TcpServer(){
        try {
            server = new ServerSocket(port);//·þÎñÆ÷Ì×½Ó×Ö
            TcpIpManage.get_text_area().append("·þÎñÆ÷Æô¶¯³É¹¦µÈ´ý¿Í»§½ÓÈë......");
        } catch (IOException e) {
        }
 
    }
 
 
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
 
    /**½«×Ö½ÚÊý×éת»»Îª16½øÖÆÊä³ö*/
    public static String BytesHexString(byte[] b) {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }
 
 
    /**Æô¶¯Ï̵߳ķ½·¨*/
    public void startThread() {
        Thread t=new Thread(this);
        t.start();
    }
 
    public void run() {
        while (true) {
            try {
 
                client= server.accept();//µÈ´ý¿Í»§¶ËÁ¬½Ó£¬·µ»ØÁ¬½Ó³É¹¦µÄSOCKET¶ÔÏó
                TcpIpManage.get_text_area().append("¿Í»§¶ËµØÖ·£º" + client.getInetAddress());
 
                // ×°ÊÎÁ÷BufferedReader·â×°ÊäÈëÁ÷£¨½ÓÊÕ¿Í»§¶ËµÄÁ÷£©
                BufferedInputStream buffin = new BufferedInputStream(client.getInputStream());
                DataInputStream datainput = new DataInputStream(buffin );
                byte[] bytes = new byte[1]; //ÉêÃ÷Ò»¸ö×Ö½ÚÊý×é
 
                while (datainput.read(bytes) != -1) {
                    recivemessage += bytesToHexString(bytes) + " ";
                    if (datainput.available() == 0) { //Ò»¸öÇëÇó
                        TcpIpManage.get_text_area().append(recivemessage+"\n");
                    }
                }
 
            } catch (IOException e) {
                ShowMessage.zidingyi(e.getMessage());
            } finally {
                try {
                    client.close();
                } catch (IOException e) {
                    ShowMessage.zidingyi(e.getMessage());
                }
            }
        }
    }
 
 
}