Skip to content

Instantly share code, notes, and snippets.

@nkalra0123
Created April 22, 2023 09:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkalra0123/cffcf7d3363082b18ac8d5fc038128b9 to your computer and use it in GitHub Desktop.
Save nkalra0123/cffcf7d3363082b18ac8d5fc038128b9 to your computer and use it in GitHub Desktop.
TCP server in java
@Override
public void run() {
try {
httpd.getMyServerSocket().bind(httpd.hostname != null ? new InetSocketAddress(httpd.hostname, httpd.myPort) : new InetSocketAddress(httpd.myPort));
hasBinded = true;
} catch (IOException e) {
this.bindException = e;
return;
}
do {
try {
final Socket finalAccept = httpd.getMyServerSocket().accept();
if (this.timeout > 0) {
finalAccept.setSoTimeout(this.timeout);
}
final InputStream inputStream = finalAccept.getInputStream();
httpd.asyncRunner.exec(httpd.createClientHandler(finalAccept, inputStream));
} catch (IOException e) {
NanoHTTPD.LOG.log(Level.FINE, "Communication with the client broken", e);
}
} while (!httpd.getMyServerSocket().isClosed());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment