Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

42
LINES

< > BotCompany Repo | #1000806 // installHelloMessage - outdated

JavaX fragment (include)

1  
// returns port number
2  
static int installHelloMessage(final S helloString) ctex {
3  
  final ServerSocket serverSocket = new ServerSocket(0);
4  
  int port = serverSocket.getLocalPort();
5  
  
6  
  //serverSocket.setSoTimeout(1000);
7  
  
8  
  daemon { // todo: higher priority
9  
    while (true) {
10  
      try {
11  
        final Socket s = serverSocket.accept();
12  
        print("connect");
13  
        thread {
14  
          try {
15  
            Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
16  
            w.write(helloString + "\n");
17  
            w.flush();
18  
19  
            final BufferedReader in = new BufferedReader(
20  
                new InputStreamReader(s.getInputStream(), "UTF-8"));
21  
                
22  
            while (true) {
23  
              String line = in.readLine();
24  
              if (line == null) break;
25  
              print("> incoming: " + line);
26  
              if (line == "kill" || line == "kill!")
27  
                if (io.isLocalConnection())
28  
                  killMyself();
29  
                else
30  
                  print("Ignoring kill command from non-local: " + io.getSocket().getInetAddress());
31  
            }
32  
          } finally {
33  
            print("disconnect");
34  
          }
35  
        }
36  
      } catch (SocketTimeoutException e) {
37  
      }
38  
    }
39  
  }
40  
  
41  
  return port;
42  
}

Author comment

Now includes a kill switch. (Might want to restrict this to local clients.)

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000806
Snippet name: installHelloMessage - outdated
Eternal ID of this version: #1000806/1
Text MD5: 55dd147e34a3b74e8bcbd2ee6a4a53de
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-19 18:10:45
Source code size: 1300 bytes / 42 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 707 / 737
Referenced in: [show references]