Skip to content

Instantly share code, notes, and snippets.

@ericfialkowski
Last active July 22, 2019 02:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericfialkowski/3b849f01f83c7f97b734 to your computer and use it in GitHub Desktop.
Save ericfialkowski/3b849f01f83c7f97b734 to your computer and use it in GitHub Desktop.
Java 8 simple socket server
package com.ericski.socketreceiver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server
{
private final int port;
private boolean running;
private final ExecutorService threadPool;
private Thread serverThread;
public Server(int port)
{
this.port = port;
threadPool = Executors.newFixedThreadPool(10,new ThreadFactory()
{
private final AtomicInteger instanceCount = new AtomicInteger();
@Override
public Thread newThread(Runnable r)
{
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
t.setName("HANDLER_" + instanceCount.getAndIncrement());
return t;
}
});
}
public void start()
{
running = true;
System.out.println("------------- Starting Server Up -------------");
serverThread = new Thread(() ->
{
try
{
ServerSocket server = new ServerSocket(port);
while ( running)
{
final Socket client = server.accept();
threadPool.submit(() ->
{
try
{
try (BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())))
{
String line;
while((line = in.readLine()) != null)
{
System.out.println(Thread.currentThread().getName() + ": " + line);
}
}
client.close();
}
catch (IOException ex)
{
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
});
}
}
catch (IOException ex)
{
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
});
serverThread.setName("LISTENER");
serverThread.start();
}
public void stop()
{
running = false;
if ( serverThread != null)
{
serverThread.interrupt();
}
threadPool.shutdown();
serverThread = null;
}
public static void main(String[] args)
{
Server server = new Server(2003);
server.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment