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

43
LINES

< > BotCompany Repo | #1000983 // installHelloMessage - new port finding method

JavaX fragment (include)

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

Author comment

Began life as a copy of #1000806

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: #1000983
Snippet name: installHelloMessage - new port finding method
Eternal ID of this version: #1000983/1
Text MD5: e175be7e49fb075614c228c04e6277d9
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:14:59
Source code size: 1344 bytes / 43 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 677 / 1009
Referenced in: [show references]