Java: Spawning a thread which accepts sockets when needed? - java

Disclaimer: I'm fairly new to Java, so mabye it's a stupid question.
Here we go.
I am writing a program that listens on a port, accepts a connection, does its thing, and closes the port.
I want this to run in it's own thread which handles the complete communication. So, ServerSocket.accept() would be started in the thread.
But how can I only spawn a thread if I need it? I can not know if I receive data if I didn't use ServerSocket.accept() yet, but if I do this in the main method, I will not be able to accept a new one while this one is open.
Some sample code omitting imports for clarity:
public class communicator implements Runnable{
private ServerSocket server;
private Socket socket;
Thread t;
communicator(ServerSocket server){
this.server = server;
t = new Thread(this,"MyThread");
t.start();
}
public void run() {
try {
socket = server.accept();
//do stuff
socket.close();
} catch (IOException ex) {
Logger.getLogger(communicator.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
and the main class:
public class test {
private static ServerSocket server;
private static Socket socket;
public static void main(String[] args) throws IOException{
server = new ServerSocket(123);
communicator comm = new communicator(server);
}
}
Or maybe I can use something like, "if one thread is active, provide a new one"?
Thx.

You can use Threadpools and an ExecturonerService something like this.
public class Listener implements Runnable {
private ServerSocket serverSocket;
ExecutorService executorService = Executors.newCachedThreadPool();
public Listener(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
#Override
public void run() {
try {
while (true) {
Socket accept = serverSocket.accept();
executorService.execute(new Worker(accept));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Listener waits until a connection comes, creates a new Worker object and gives it the executioner service to run it. This service will handle the number of threads that are needed for you und clean everything up when the task is done.
public class Worker implements Runnable{
private Socket socket;
public Worker(Socket socket) {
this.socket = socket;
}
#Override
public void run() {
//do stuff
}
}

Related

How to only wait for a new Socket with ServerSocket without usage of an accept()?

I have a basic server thread in which I accept sockets with the accept () function. For a certain period of time, I would need to avoid connecting new sockets. I want to do this from another thread by telling the server thread to not make new connections.
public class ServerThread extends Thread {
private boolean running = false;
private final ConnectionManager connectionManager;
private final AtomicBoolean acceptNewConnections;
ServerThread(ConnectionManager connectionManager, int port) {
super("ServerThread");
this.connectionManager = connectionManager;
this.port = port;
this.acceptNewConnections = new AtomicBoolean(false);
}
// This is called from other threads
public void setAcceptNewConnections(boolean value) {
acceptNewConnections.set(value);
}
#Override
public void shutdown() {
acceptNewConnections.set(false);
running = false;
try {
join();
} catch (InterruptedException ignored) {}
}
#Override
public void run() {
running = true;
connectionManager.serverThreadStart();
try (ServerSocket serverSocket = new ServerSocket(port)) {
acceptNewConnections.set(true);
while (running) {
try {
if (acceptNewConnections.get()) {
final Socket socket = serverSocket.accept();
if (acceptNewConnections.get()) connectionManager.addClient(socket);
else socket.close();
} else {
try {Thread.sleep(10);} catch(InterruptedException e){}
}
} catch (SocketTimeoutException ignored) {
//
}
}
} catch (IOException e) {
//
}
connectionManager.serverThreadStop();
}
}
My questions are:
Is there a way to interrupt waiting for a new socket while waiting?
Is there a way to wait for a new socket, but would accept it as needed after waiting for it, not wait and accept it in one command?
How can I pause the server thread while blocking new connections without using Thread.sleep() and useless overusage of cpu?
Thank you so much for help.

why java tcp server is accepting closed socket?

I have a class A that accepts TCP connection and send this new socket to Thread B where data from that socket is received and sent.
Class A{
Class b = new B();
public void run()
{
b.start();
while(true){
Socket socket = serverSocket.accept();
if(socket==null || socket.isClosed())continue;
b.setSocket(socket);
}
}
}
Class B extends Thread{
Socket socket;
public void setSocket(Socket p_socket) throws IOException
{
if(socket!=null && !socket.isClosed())
{
try{
socket.close();
socket = null;
Thread.sleep(5);
}
catch(Exception ex)
{}
}
socket = p_socket;
inputStream = socket.getInputStream(); // Here I am getting socket.closed() exception very much.
}
This worked fairly good in the past but now recently I am very very frequently getting the following error.
java.io.IOException: Socket Closed
at java.net.AbstractPlainSocketImpl.getInputStream(AbstractPlainSocketImpl.java:421)
at java.net.Socket$2.run(Socket.java:914)
at java.net.Socket$2.run(Socket.java:912)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.Socket.getInputStream(Socket.java:911)
I don't understand why this is happening now after years of working fine. Is this due to the network problem or Thread related something?
Updated:
Can this be the server related problem? Because the same application is running on other server but they are not having this problem.
The whole setup concept looks a bit broken. You should not "change" resources from the outside, while maybe there is still some work going on in that thread. A way better concept is to encapsulate the Socket into a new worker thread:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class WrapHandler {
static public class Server {
private final ServerSocket mSocket;
private final ArrayList<Handler> mRunningHandlers = new ArrayList<>();
public Server(final int pPort) throws IOException {
mSocket = new ServerSocket(pPort);
new Thread(() -> mainLoop()).start();
}
private void mainLoop() {
while (true) {
try {
#SuppressWarnings("resource") final Socket socket = mSocket.accept(); // do not close, do not handle with resource, socket will be closed by handler!
final Handler h = new Handler(socket, this);
handlerStarted(h);
} catch (final IOException e) {
e.printStackTrace(); // do something useful
}
}
}
synchronized void handlerStarted(final Handler pHandler) {
mRunningHandlers.add(pHandler);
}
synchronized void handlerEnded(final Handler pHandler) {
mRunningHandlers.remove(pHandler);
}
void handleException(final Handler pHandler, final Throwable pException) {
/* again do something useful */
}
}
static public class Handler {
private final Socket mSocket;
private final Server mServer;
public Handler(final Socket pSocket, final Server pServer) {
mSocket = pSocket;
mServer = pServer;
new Thread(() -> handleSocket()).start();
}
private void handleSocket() {
try {
handleData();
} catch (final IOException e) {
mServer.handleException(this, e);
} finally {
mServer.handlerEnded(this);
stop();
}
}
private void handleData() throws IOException {
mSocket.getInputStream().read();
/* data handling code here */
}
void stop() {
try {
mSocket.close();
} catch (final IOException e) { /* ignore or handle as you will */ }
}
}
}

Java ServerSocket & Sockets no wait for multi-threaded server

So I have a multithreaded server, and data sends back and forth correctly, but my write operations stalls on the slower connections. I've noticed that it goes by connection time. The first client to connect always receives data first from the server. The next one has to wait until the first one is done receiving and so on so forth. What I'm looking for is a server that sends data to many clients without waiting for a client to finish receiving. I've read up about NIO (non-blocking), but I'd really prefer keeping my current method, which is to use a separate thread for each client.
Here's the code.
Server:
public class Server implements Runnable {
private Thread thread;
private ServerSocket serverSocket;
private ArrayList<ClientThread> clients;
public Server(int port) throws IOException {
thread = new Thread(this);
clients = new ArrayList<ClientThread>();
serverSocket = new ServerSocket(port);
thread.start();
}
#Override
public void run() {
while (true) {
try {
//Listens to clients connecting.
ClientThread client = new ClientThread(serverSocket.accept());
clients.add(client);
ServerWindow.addText("-- Someone connected!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void broadcast(String data) {
broadcast(data, null);
}
public void broadcast(String data, ClientThread exclude) {
int amount = clients.size();
for (int i = 0; i < amount; i++) {
if (!clients.get(i).equals(exclude)) { //Don't send it to that client.
try {
clients.get(i).broadcast(data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
Client thread object:
public class ClientThread implements Runnable {
private Thread thread;
private Socket socket;
private Scanner input;
private PrintWriter output;
public ClientThread(Socket s) throws IOException {
thread = new Thread(this);
socket = s;
socket.setTcpNoDelay(true);
//socket.setSoTimeout(10); //Send little chunk for 10 milliseconds.
input = new Scanner(socket.getInputStream());
output = new PrintWriter(socket.getOutputStream());
thread.start();
}
public void run() {
while (true) {
if (input.hasNext()) {
reciever(input.nextLine());
}
}
}
private void reciever(String data) {
ServerWindow.addText(data);
ServerWindow.server.broadcast(data, this);
}
public void broadcast(String data) throws IOException {
output.println(data);
output.flush();
}
}
It seems you are calling the broadcast method from the same thread.
This is a common pitfall for users new to multithreading in Java.
The fact that the broadcast method is in a subclass of Thread does not mean it will be executed on that Thread
In fact it will be executed on the thread that called it. The only method that will be executed on your created ClientThread is run() and anything that run() calls while it is executing. If you want said thread to not only read data from your connection but also write to it, you have to modify the run method to listen to external commands to start writing.

Client / Server model Java

I have a server class which connects a client on a specific server socket port and starts off a thread with a service class. Specifically, I have 3 service classes so I would like to have 3 different ports. This however, is not working as I had expected it to. This is my code for the server:
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
public class WebsiteServer {
public static void main(String args[]) throws IOException {
ServerSocket serversocket = new ServerSocket(22401);
ServerSocket serversocket2 = new ServerSocket(22402);
Thread thread;
Thread thread2;
Socket socket;
Socket socket2;
NewUserService newuserservice;
ExistingUserService existinguserservice;
System.out.println("Waiting for clients to connect.");
while (true) {
socket = serversocket.accept();
socket2 = serversocket2.accept();
if(socket.isConnected()) {
System.out.println("NewUserClient has connected.");
newuserservice = new NewUserService(socket);
thread = new Thread(newuserservice);
thread.start();
}
if(socket2.isConnected()) {
System.out.println("ExistingUserClient has connected.");
existinguserservice = new ExistingUserService(socket2);
thread2 = new Thread(existinguserservice);
thread2.start();
}
}
}
}
It works fine if I only use one port for example:
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
public class WebsiteServer {
public static void main(String args[]) throws IOException {
ServerSocket serversocket = new ServerSocket(22401);
ServerSocket serversocket2 = new ServerSocket(22402);
Thread thread;
Thread thread2;
Socket socket;
Socket socket2;
NewUserService newuserservice;
ExistingUserService existinguserservice;
System.out.println("Waiting for clients to connect.");
while (true) {
socket = serversocket.accept();
//socket2 = serversocket2.accept();
if(socket.isConnected()) {
System.out.println("NewUserClient has connected.");
newuserservice = new NewUserService(socket);
thread = new Thread(newuserservice);
thread.start();
}
// if(socket2.isConnected()) {
//
// System.out.println("ExistingUserClient has connected.");
// existinguserservice = new ExistingUserService(socket2);
// thread2 = new Thread(existinguserservice);
// thread2.start();
// }
}
}
}
Any help would be appreciated.
The accept method blocks until a connection is made. So, you are sitting blocked one one .accept(), even though the other server may have activity. One simple solution would be to make one listening thread per server.
accept()is a blocking method. In other words, the second accept() is waiting for another connection. Untik a second connection is accepted, your code will be blocked
You need to call the accept() method in a separate thread. accept() blocks the thread until someone joins the server. This could be 1 ms or 1 year.
I suggest you create an AcceptListener to be called when someone joins
public interface AcceptListener{
public void socketAccepted(Socket s);
}
And a handler class
public class MyServerSocket extends Thread{
private AcceptListener l;
private boolean run = true;
private ServerSocket s;
public MyServerSocket(AcceptListener l, int port){
this.l = l;
this.s = new ServerSocket(port);
this.start();
}
public void run(){
while(run){
l.socketAccepted(s.accept());
}
}
}
You'll have to handle errors and whatnot and make the overrides in you subclass(es) of AcceptListener.

Interrupting thread which is blocked waiting on IO from socket?

I have an app that listens to incoming connections on a specified hostname and port. The listening is invoked with the method listen() (see below), which waits constantly for an incoming connection using ServerSocket.accept(), creating a new Thread to handle the input stream.
private ServerSocket serverSocket;
private Thread listenerThread;
public void listen() throws IOException {
this.listenerThread = new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
Socket socket = TheServerClass.this.serverSocket.accept();
// Create new thread to handle the incoming connection
}
catch (IOException exc) { }
}
}
});
this.listenerThread.start();
}
Now I want to stop the running of listenerThread. But when I call this.listenerThread.interrupt(), this doesn't work.
I thought you can stop a thread by interrupting it, so why isn't that working?
(Notice: A possible solution is to close the ServerSocket using this.serverSocket.close(), but can it be accomplished with interrupt() or something?)
Call serverSocket.close(),
I guess since you are not doing IO yet - you can not interrupt it, and since the accept() doesn't throw InterruptedException you won't be able to interrupt it. The thread is interrupted, but that flag you have to check for yourself Thread.isInterrupted().
See How can I interrupt a ServerSocket accept() method?.
The answer is in the question. You need to close the socket. It's done using serverSocket.close(). Thread.interrupt() doesn't care about sockets.
Use this:
public class MyThread extends Thread {
private boolean stop;
private ServerSocket serverSocket;
public MyThread(ServerSocket ss) {
this.serverSocket = ss;
this.stop = false;
}
public void setStop() {
this.stop = true;
if (this.ss != null) {
this.ss.close();
}
}
public void run() {
while (!stop) {
try {
Socket socket = serverSocket.accept();
// Create new thread to handle the incoming connection
}
catch (IOException exc) { }
}
}
}
and from the listen() method just call setStop() method of the thread.

Categories

Resources