🔨
Java Utility Package
🔨
Java Utility Package
  • Java Utility Package (Open Source)
  • Examples
    • 📖Logging
    • 🔧Utility Class
    • 🔑Password Vault
    • 🖥️HTTP / Socket Server
    • ⚒️HTTP Client
    • 🖥️Socket Client
    • 🗃️JDBC Database
    • ✉️SMTP Mailer
    • ⚔️Java Thread
    • 🗄️File Tools
    • ⌚Timer
    • 💡Tips / FAQ
  • Downloads
    • ⬇️Package ch.k43.util
    • ⬇️Sample Code
    • 📖JavaDoc API
    • 👨‍💻GitHub
Powered by GitBook
  1. Examples

HTTP / Socket Server

Create transaction server in minutes.

Last updated 19 days ago

KHTTPServerThread / KSocketThread Class Overview

  • Socket and HTTP Supports raw TCP/IP socket applications or HTTP server applications (for web or REST services).

  • TLS and Non-TLS Client Connection Support Enables both secure (TLS) and non-secure client connections to server applications, providing flexibility for various networking scenarios.

  • Thread Management Loads, starts, and manages user classes to dynamically handle socket or HTTP clients.

  • Client and Basic Authentication Allows server applications to differentiate between authenticated and non-authenticated clients. Use the getAuthenticatedClient() method to verify a valid client name..

  • Flexible Data Read/Write Methods Includes efficient methods for reading and writing data in various formats, such as byte[], char[], String, and line-based input.

HTTP Server Example: Simple Webserver
try (KSocketServer httpServer = new KSocketServer(8080, KHTTPServerThreadSample.class)) {

   if (!httpServer.isActive()) {
      System.out.println("The HTTP server could not be started: " + httpServer.getLastError());
      System.exit(1);
   }
			
   System.out.println("HTTP server ready on port 8080 - Will terminate after 3 minutes");
   K.waitMinutes(3);
   System.out.println("HTTP server terminating");
}
public class KHTTPServerThreadSample extends KHTTPServerThread {

   public KHTTPServerThreadSample(Socket argSocket) {
      super(argSocket);
   }
	
   @Override
   public void get(String argURL) {
      sendFile(argURL);
   }
}
Socket Server Example: Echo Server
// Start echo server on port 8080
try (KServerSocket server = New KServerSocket(8080, KSocketServerThreadSample.class)) {
   KLog.abort(!server.isActive(), "Unable to start server: " + server.getLastError());

   // Let client connect (e.g. "nc localhost 8080" or "telnet localhost 8080")
   K.waitMinutes(5);
}
public class KSocketServerThreadSample extends KSocketServerThread {

   public KSocketServerThreadSample(Socket argSocket) {
      super(argSocket);
   }

   public void run() {
      KLog.info("Upper-Case-Echo-Server started");
      
      String newLine    = K.getLineSeparator();
      String clientData = null;
      
      write((!isSecuredConnection() ? "Non-" : "") + "TLS Upper-Case-Echo-Server ready" + newLine);
      
      while ((clientData = readLine()) != null) {
         write(clientData.toUpperCase() + newLine);
      }
      close();
      
      KLog.info("Upper-Case-Echo-Server terminated");
   }
}

Architecture Overview

🖥️
Page cover image