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)

// returns port number
static int installHelloMessage(final S helloString) ctex {
  final ServerSocket serverSocket = makeServerSocket();
  int port = serverSocket.getLocalPort();
  print("hello message on port: " + port);

  //serverSocket.setSoTimeout(1000);
  
  daemon { // todo: higher priority
    while (true) {
      try {
        final Socket s = serverSocket.accept();
        print("connect");
        thread {
          try {
            Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
            w.write(helloString + "\n");
            w.flush();

            final BufferedReader in = new BufferedReader(
                new InputStreamReader(s.getInputStream(), "UTF-8"));
                
            while (true) {
              String line = in.readLine();
              if (line == null) break;
              print("> incoming: " + line);
              if (line == "kill" || line == "kill!")
                if (s.getInetAddress().isLoopbackAddress())
                  killMyself();
                else
                  print("Ignoring kill command from non-local: " + s.getInetAddress());
            }
          } finally {
            print("disconnect");
          }
        }
      } catch (SocketTimeoutException e) {
      }
    }
  }
  
  return port;
}

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: 676 / 1007
Referenced in: [show references]