Skip to content

Instantly share code, notes, and snippets.

View nkalra0123's full-sized avatar

Nitin Kalra nkalra0123

View GitHub Profile
@nkalra0123
nkalra0123 / tcpserver.java
Created April 22, 2023 09:05
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 {
else if (frame.getOpCode() == OpCode.Ping) {
sendFrame(new WebSocketFrame(OpCode.Pong, true, frame.getBinaryPayload()));
}
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);
/**
* Maximum time to wait on Socket.getInputStream().read() (in milliseconds)
* This is required as the Keep-Alive HTTP connections would otherwise block
* the socket reading thread forever (or as long the browser is open).
*/
public static final int SOCKET_READ_TIMEOUT = 5000;
protected boolean isWebsocketRequested(IHTTPSession session) {
Map<String, String> headers = session.getHeaders();
String upgrade = headers.get(NanoWSD.HEADER_UPGRADE);
boolean isCorrectConnection = isWebSocketConnectionHeader(headers);
boolean isUpgrade = NanoWSD.HEADER_UPGRADE_VALUE.equalsIgnoreCase(upgrade);
return isUpgrade && isCorrectConnection;
}
public Response handleWebSocket(final IHTTPSession session) {
Map<String, String> headers = session.getHeaders();
if (isWebsocketRequested(session)) {
if (!NanoWSD.HEADER_WEBSOCKET_VERSION_VALUE.
equalsIgnoreCase(headers.get(NanoWSD.HEADER_WEBSOCKET_VERSION))) {
return Response.
newFixedLengthResponse(Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT,
"Invalid Websocket-Version " + headers.get(NanoWSD.HEADER_WEBSOCKET_VERSION));
}
// BinaryOperator
System.out.println("binaryOpeartor = " + Stream.of(1,2,3,4).
reduce(new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer x, Integer y) {
return x + y;
}
}).get());
@nkalra0123
nkalra0123 / FunctionalInterfaces.java
Created January 19, 2020 06:27
Functional Interfaces, Lamda and Method References
import java.util.Optional;
import java.util.function.*;
import java.util.stream.Stream;
public class FunctionalInterfaces {
@FunctionalInterface
public interface foo
{
abstract public void food();
@nkalra0123
nkalra0123 / FunctionalInterfaces.java
Created January 19, 2020 06:27
Supplier Functional Interfaces
//Supplier
Optional<Integer> value = Stream.of(1,2,3,4).reduce(new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer x, Integer y) {
return x + y;
}
});
value.orElseGet(new Supplier<Integer>() {
@nkalra0123
nkalra0123 / FunctionalInterfaces.java
Created January 19, 2020 06:26
Function, Consumer Functional Interfaces
// Function, Consumer
Stream.of(strings).filter(s -> s.matches("\\d+")).map(new Function<String, Integer>() {
/**
* Applies this function to the given argument.
*
* @param s the function argument
* @return the function result
*/
@Override