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

42
LINES

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

JavaX fragment (include)

// returns port number
static int installHelloMessage(final S helloString) ctex {
  final ServerSocket serverSocket = new ServerSocket(0);
  int port = serverSocket.getLocalPort();
  
  //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 (io.isLocalConnection())
                  killMyself();
                else
                  print("Ignoring kill command from non-local: " + io.getSocket().getInetAddress());
            }
          } finally {
            print("disconnect");
          }
        }
      } catch (SocketTimeoutException e) {
      }
    }
  }
  
  return port;
}

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: 766 / 797
Referenced in: #1000803 - Program with a hello message on server port
#1000983 - installHelloMessage - new port finding method
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)