I had a quick question on how i would maintain a connection of 1000 sockets. Im using threads to initiate writing and reading of the streams.
However when I pass the socket to the new thread, the thread won't exit after excecution. I wanted to know if passing the socket would keep the thread on because it was still connected.
I wanted to know if passing the socket would keep the thread on because it was still connected.
No. It won't do that.
Your threads are failing to exit for some other reasons. Possibly they are blocked in some operation on their respective sockets. Or maybe they are exiting, but you are expecting the threads or sockets to be magically removed from some array or list. (They won't be.)
I'd need some code, etc to give you a better explanation of what might be happening.
Related
I have a Java program with multiple sockets that occasionally have data that need to be read and processed, but there is an indeterminate amount of time which there is no data to be read. I need a good way to constantly check if there is data in the sockets, and process the data. Assigning one thread per socket is not a good idea since there could be too many sockets and use too much memory.
Currently, I have a couple threads, each one assigned to service its own list of sockets. If there was nothing to read in any of the sockets, then sleep one second, then loop. If there was something to read in any of the sockets, just loop without waiting and iterate through the sockets again.
The reason I do this is because I don't want to use up too much resources if there is nothing to read, and the one second delay is not a problem. The only down side is that there is no flexibility for sockets to jump threads, so the worst case scenario is that a single thread is overloaded with work, while the other threads are doing nothing.
Another idea I've had: create a thread pool, and queue up all the sockets to be serviced, and re-add them when they are serviced, but there is no good way to know if none of the sockets need servicing and the threads can take a break to free up CPU cycles.
Is there a good way to assign threads tasks, but not overload computer resources if there is nothing to do?
Ideally an event is triggered each time there is data available in a socket, but as far as I know, there is no way to do this, and I must poll the sockets.
To reiterate, I do not want a one to one relationship between socket and thread.
there could be too many sockets and use too much memory.
You can achieve 1,000 to 10,000 this way. Memory is much cheaper than it was when NIO was introduced 12 years ago and threads are more efficient and scalable than they used to be.
I have a couple threads, each one assigned to service its own list of sockets. If there was nothing to read in any of the sockets, then sleep one second, then loop.
I use a pause which busy waits for a short period and yeilds and finally sleeps for an escalating period of time.
You can use Selectors, but these are not simple to use correctly. In this situation I would use a library like netty or at the very least read the code it uses.
The only down side is that there is no flexibility for sockets to jump threads, so the worst case scenario is that a single thread is overloaded with work, while the other threads are doing nothing.
This is where using a thread per socket is better.
I must poll the sockets.
You can use Selectors, but these are single threaded and switch sockets between selectors is not simple.
I would reconsider using more threads for simplicity.
I have some problems understanding how a socket should be handled. I get that server socket must runs in its own thread, because it must check if there are new connections. Now, i'm not sure if every socket opened by a new connection should runs in a thread.
What i have in mind is checking every x time the socket states. If it has something to be read, then read. If not, check the next socket. I see some examples where this process is done in a thread, but i dont want a socket to do stuff, just want to read if it has some data, and process them.
The answer is no, you don't need to listen in a separate thread. But, just realize that while you are "listening" your entire program will be waiting for that to complete before moving onward.
So unless you are fine with your entire program waiting, I would suggest a separate thread.
You can also have one thread which communicates with all sockets in a round-robin manner. It checks each socket if it has new data, and when it hasn't it checks the next.
Another alternative is to use NIO (New Input/Output).
The idea behind NIO is that you have a thread with one Selector which owns multiple Channels (a channel can be a network socket or any other IO interface). You then call selector.select() in a loop. This method blocks until one or more channels have data, and then returns a set of these channels. You can then process the data the channels delivered.
Here is a tutorial.
The problems with round-robin using available() are many.
It assumes that available() actually works, which isn't guaranteed.
It assumes that all clients need the same amount of service.
N-1 clients wait while one client is serviced.
A non-responsive client can block not only your application but all the other clients.
I'm sure there are more.
Don't do this. Use threads or NIO.
I want to create different thread for read and write method by using socket.
I wonder should I use different socket or same socket to share for both thread?
If you're reading and writing to and from the same device/object, i'd use the same thread. This way you don't have it trying to read something when you haven't written to it yet. Doing multiple threads might cause a multitude of errors if you're not careful, such as nulls.
You can use the same socket to both read and write. Of course one of the 2 endpoints must open a server socket. Then you can have 1 thread blocked waiting for input data while another thread could output data when needed
1. Create One socket object.
2. Use any nos of threads to access this object, but do keep enough care to make the
write and read operations Thread-Safe.
3. Try using the ServerChannel and ServerSocketChannel from java.util.nio package, which is Asynchronous .
Here is the situation. I'm creating an Android that utilises Bluetooth to update connected clients of each other's status. The idea is for the host of the Bluetooth connection to hold a multi-dimensional array holding all of these stats. Each time a client updates the host of their new status, the host then update the data in the array and then sends this to all the clients.
Of course, this all sounds like milk and cookies to me, but unfortunately it is not. I understand that I need to have a Bluetooth socket on each end and one of them needs to be a host socket. So, get one connection done seems pretty straight forward. But what if I then want to accept more connections? I've been reading around and apparently I have to create a new thread for each connection. I don't see how that would work, could someone please explain this?
The reason you need a thread for each connection is this:
Imagine you have two opened sockets, sock1 and sock2. To read from those sockets, you might call something like
InputStream in1 = sock1.getInputStream();
InputStream in2 = sock2.getInputStream();
Now, to read from sock1, you call
in1.read(buffer);
where "buffer" is a byte array, in which you store the bytes you read.
However, read() is a blocking call - in other words, it doesn't return, and you don't get to execute the next line, until there are some bytes to read(). So if you try to read sock1, you'll never get to read sock2, and vice versa, if they're in the same thread.
Thus if you have one thread per connection, each thread can call read(), and wait for input. If input comes while one of the other threads is executing, it waits until that thread's turn comes up, and then proceeds.
To actually implement this, all you need to do is stick the code to handle one connection into a class that extends Thread.
There are lots of details involved - I would suggest the Android BluetoothChat sample as a good tutorial.
I am developing a Net game, in general:
I have a server which launch a serverThread for each client that has connected to it.
Its purpose is to listen to messages from the specific client and to process it on the server.
Also for each client that is opened it launch a clientThread which is a listening thread to messages from the server.
The two threads are quite simple and similar threads which implements the Runnable Interface and therefore override the run method.
Each Run method is some kind of infinite loop that has on its start the command (blocking command):
int command = m_In.readInt();
and then do a process by switch cases structure over the received command.
after process was done, the loop cause the code to return to the blocking m_In.readInt()
To wait for another command to come.
My question is: My Net game has enough options which are using the communication over this m_In, so what happens if there are two messages or more coming almost together to the clientThread, how would the dataInputStream will act?
Will it begin to process first message and after its done will grab the second which is on some kind of a queue? or it could drop the second message and it will be lost?
Maybe that stream has buffer so it stores the second message in a queue or something?
Thanks
Streams by their nature expect data to come in a specified order. If you have two threads writing to the same stream at the same time, bad things will happen.
Now you can certainly synchronize access to the stream and have the two thread interleave their writing (so long as you build some sort of formatting on the stream that tells the receiver how to read data), but you don't get this by default.
Typically though, each client thread would have their own connection and thus their own stream to write into. A server can obviously read from multiple streams at the 'same time', and that is the normal server pattern.