a
554325746@qq.com
2019-07-15 e6c8834e2aa2f3c1f46e588d94ae8ec6aa6c8dec
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
100
101
102
103
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;
 
public class AlertDataSocket {
 
    static final String CRLF = System.getProperty("line.separator");
 
    static ServerSocket server;
 
    static int port = 2016;
 
    public static void main(String[] args) {
        try {
            if (args.length > 0) {
                String port_V = args[0]; // port
                if (port_V != null && !"".equals(port_V)) {
                    port = Integer.parseInt(port_V);
                }
            }
            server = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String encoding = "UTF-8";
        BufferedReader iReader = null;
        BufferedWriter iWriter = null;
        while (true) {
            Socket client = null;
            try {
                client = server.accept();
                iReader = new BufferedReader(new InputStreamReader(client
                        .getInputStream(), encoding));
                iWriter = new BufferedWriter(new OutputStreamWriter(client
                        .getOutputStream(), encoding));
                System.out.println("Receive message: " + iReader.readLine());
                sendAck(iWriter);
            } catch (IOException ex) {
                sendNack(iWriter);
                ex.printStackTrace();
            } finally {
                try {
                    if (iWriter != null) {
                        iWriter.close();
                    }
                    if (iReader != null) {
                        iReader.close();
                    }
                    if (client != null) {
                        client.close();
                    }
                } catch (IOException e) {
                }
            }
        }
    }
 
    public static void sendAck(BufferedWriter iWriter) {
        String statusLine = "HTTP/1.1 200 OK" + CRLF;
        String contentTypeLine = "Content-type: text/html" + CRLF;
        String connectionClose = "Connection: close" + CRLF;
        String content = "<ErrorList><Success>success</Success></ErrorList>";
        String contentLengthLine = "Content-Length: "
                + (new Integer(content.length())).toString() + CRLF + CRLF;
 
        try {
            iWriter.write(statusLine);
            iWriter.write(contentTypeLine);
            iWriter.write(connectionClose);
            iWriter.write(contentLengthLine);
            iWriter.write(content);
            iWriter.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
 
    // Send nack
    public static void sendNack(BufferedWriter iWriter) {
        String statusLine = "HTTP/1.1 400 Bad Request" + CRLF;
        String contentTypeLine = "Content-type: text/html" + CRLF;
        String connectionClose = "Connection: close" + CRLF;
        String content = "<ErrorList><Error>error</Error></ErrorList>";
        String contentLengthLine = "Content-Length: "
                + (new Integer(content.length())).toString() + CRLF + CRLF;
 
        try {
            iWriter.write(statusLine);
            iWriter.write(contentTypeLine);
            iWriter.write(connectionClose);
            iWriter.write(contentLengthLine);
            iWriter.write(content);
            iWriter.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
 
}