Hello all I am writing server socket inside my Android app. It should allow only one client. Interesting thing is that I want my server to send messages(strings) to client while they are available. I have queue of strings from which i want my server try taking strings on by one and send to the client, if queue empty do not send and as long as some string is pushed into queue send it.
Here is how I do that:
public class Resource {
public static Queue<String> q = new LinkedList<String>();
public static synchronized void addString(String commands) {//for reader
semaphore.add(commands);
}
public synchronized String getString() {
if (!semaphore.isEmpty()) {
return semaphore.remove();
} else return "0";
}
}
For that I have a queue q of string where I can add strings from another class. Here is my server code:
new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
// Listens for a connection to be made to this socket.
Socket socket = my_serverSocket.accept();
//send message to client
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("1");
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
String msg = in.readLine();
System.out.println("message is: " + msg);
// tidy up
//in.close();
//socket.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SecurityException se) {
se.printStackTrace();
}
}
}
}).start();
Here I want my serve check if queue is empty, if not get one string and send to the client, and repeat this process infinetely. It is something like producer consumer problem. Also I am interested do i need to use synchronized. Is it possible that i will end up with race condition.
You probably want to use a BlockingQueue.
This is the typical way to implement a producer-comsumer pattern: the producer will put() a string in the queue, the server/consumer will take() a string from the queue. If the queue is empty the server will block and wait, optionally with a timeout.
You probably don't need to bound the queue, so you could instantiate a LinkedBlockingQueue with its empty constructor.
All implementations of BlockingQueue are thread-safe, so you don't need additional synchronisation for their basic operations.1
1. If you need atomicity and/or other memory effects, you must implement your thread safety policies yourself as usual.
Related
I need to write a server for stock exchange in Java, the server will get many requests from couple of servers per second, my goal is to write this server and find out how many HTTP GET requests it can handle after executing couple of conditions and send a response in less then 100ms.
Each request contains a token that will be valid for 100ms.
My goal is to fetch data from cache (Redis/Memcached) using the request parameters, execute couple of O(1) conditions, send a response to the same server with the token and a boolean answer in max 100ms and save the request and response in a database (asynchronously).
This server will sit on AWS ec2 at the same region as the requesting servers.
Its needs to be written in Java, low level as it should be, btw, I come from Python/NodeJS world for the past 2 years.
I think its a classic producer-consumer design pattern.
Can anyone guide me to the technical parts ? like.. use "this' for handling requests (SocketInputStream), and use "that" for handling the queues, or maybe use "OpenMQ" framework ? monitor requests with "this" ? any reference for an implementation for similar problem ?
Update 1:
Thanks #mp911de, I saw LMAX (Disruptor), someone already invented the wheel but it feels like over-programming for now, I want to check how many 1k json object can be sent and response 100bytes back from simple ec2-server that run this java code I attached ? BTW, how many threads to put in Executors.newFixedThreadPool ?
public class JavaSimpleWebServer {
private static final int fNumberOfThreads = 100;
private static final Executor fThreadPool = Executors.newFixedThreadPool(fNumberOfThreads);
public static final int PORT = 81;
public static void main(String[] args) throws IOException {
ServerSocket socket = new ServerSocket(PORT);
while (true)
{
final Socket connection = socket.accept();
Runnable task = new Runnable() {
#Override
public void run() {
HandleRequest(connection);
}
};
fThreadPool.execute(task);
}
}
private static void HandleRequest(Socket s) {
BufferedReader in;
PrintWriter out;
String request;
try {
String webServerAddress = s.getInetAddress().toString();
System.out.println("New Connection:" + webServerAddress);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
request = in.readLine();
System.out.println("--- Client request: " + request);
out = new PrintWriter(s.getOutputStream(), true);
// do some calculation
out.println(jsonResponse);
out.flush();
out.close();
s.close();
}
catch (IOException e) {
System.out.println("Failed respond to client request: " + e.getMessage());
}
finally {
if (s != null)
{
try {
s.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
This method functions as intended, in that it only gets one object at a time from the server at a time, handles that object, and then gets another. However, it seems that I need to, seemingly, turn it inside out, probably with Producer-Consumer:
public void inputOutput() throws IOException, ClassNotFoundException {
Socket socket = new Socket(server, portNumber);
boolean eof = false;
Title title = null;
State state = State.undefined;
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream())) {
do {
try {
title = (Title) objectInputStream.readObject();
log.info(title.toString());
do {
state = State.undefined;
try {
c.printf("enter the state for record:");
state = State.valueOf(c.readLine());
} catch (java.lang.IllegalArgumentException iae) {
log.warning(Arrays.deepToString(State.values()));
}
} while (state == State.undefined);
title.setState(state);
title.setTitle("modified from client");
objectOutputStream.writeObject(title);
} catch (java.io.EOFException eofe) {
eof = true;
}
} while (!eof);
}
}
The "flow", of handling one object, and then sending it back, and then requesting another, is exactly what I want to replicate:
package net.bounceme.dur.client;
import java.util.concurrent.BlockingQueue;
public class Producer implements Runnable {
private final BlockingQueue<Message> queue;
public Producer(BlockingQueue<Message> q) {
this.queue = q;
}
#Override
public void run() {
//produce messages
for (int i = 0; i < 100; i++) {
Message msg = new Message("" + i);
try {
Thread.sleep(i);
queue.put(msg);
System.out.println("Produced " + msg.getMsg());
} catch (InterruptedException e) {
}
}
//adding exit message
Message msg = new Message("exit");
try {
queue.put(msg);
} catch (InterruptedException e) {
}
}
}
Would the Producer just handle getting objects from the socket connection via objectInputStream.readObject();, for example?
If so, how do I slow down the Producer (which, from a certain perspective, is also a consumer) so that it only "produces" one object at a time, waits to be notified, and then, only when notified, goes back to the stream for another object.
From within the context of the client, this is a producer, but, from a wider view, I suppose it's also a consumer.
How does the producer receive a semaphore, or other notification, to then "produce" another object?
Because Producer implements Runnable, I cannot pass parameters into run. Could, perhaps, the ProducerConsumerService driver pause the thread? That seems error-prone, at the least.
code borrowed from:
http://www.journaldev.com/1034/java-blockingqueue-example-implementing-producer-consumer-problem
Explicit answers not required -- I'm probably just misunderstanding how to use a BlockingQueue with Sockets. At present, I want to leave the server unchanged, so that its response/request "flow" of sending an object, and then waiting for a response, stays like that.
One solution is to use a size 1 ArrayBlockingQueue. The producer can only put one object at a time in there, and wait until the consumer removes it.
So the pseudocode would be something like...
// Producer thread (produces serverside data)
obj = readObjFromServer();
sharedQueue.put(obj); // Blocks if there is already an object waiting
// Consumer thread
obj = sharedQueue.take(); // Blocks until data available
handleData(obj);
I am implementing a Server/Client system using Java. the server listens for incoming connection from clients and after a client connects, the server creates a new socket and passes it to a new thread which will only be used for receiving data:
while (true){
clientSocket=serverSocket.accept();
new ClientReceiver(clientSocket,this.clientsManager).start();
}
the clientReceiver class is as follows:
public class ClientReceiver extends Thread {
private Socket clientSocket=null;
private Client client=null;
private ClientsManager clientsManager;
private ClientActionParser clientActionParser=new ClientActionParser();
ClientHandlerState clientHandlerState;
PrintWriter outputStream=null;
BufferedReader inputStream=null;
public ClientReceiver(Socket clientSocket, ClientsManager clientsManager){
this.clientSocket=clientSocket;
this.clientsManager=clientsManager;
this.setClientHandlerState(ClientHandlerState.Connected);
}
public void run(){
String actionString;
try{
//define output and input stream to client
outputStream =new PrintWriter(clientSocket.getOutputStream(),true);
inputStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//while client is connected read input to actionString
while((actionString=inputStream.readLine()) != null){
AbstractClientAction clientAction= this.clientActionParser.parse(actionString);
if(this.getClientHandlerState()==ClientHandlerState.Connected){
if(clientAction instanceof ClientLoginAction){
ClientLoginAction clientLoginAction=(ClientLoginAction) clientAction;
if(this.authenticate(clientLoginAction)){
}
else{
throw new AuthenticationException();
}
}
else{
throw new AuthenticationException();
}
}
}
if(this.getClientHandlerState()==ClientHandlerState.Authorized){
//receive other client actions: transfer barge ....
}
try {
Thread.sleep(400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch(IOException e){
}
catch (AuthenticationException e) {
// TODO: handle exception
}
//clean up the resources
try{
outputStream.close();
inputStream.close();
clientSocket.close();
}
catch(Exception e){
}
}
private boolean authenticate(ClientLoginAction clientLoginAction){
//perform authentication. If authentication successful:
this.client=this.clientsManager.authenticateClient(clientLoginAction.getUsername(), clientLoginAction.getPassword());
if(this.client==null){
return false;
}
else{
ClientSender clientSender=new ClientSender(this.outputStream, this.client);
this.clientsManager.addClientSender(clientSender);
this.setClientHandlerState(ClientHandlerState.Authorized);
clientSender.start();
return true;
}
}
public ClientHandlerState getClientHandlerState(){
return this.clientHandlerState;
}
public void setClientHandlerState(ClientHandlerState clientHandlerState){
this.clientHandlerState=clientHandlerState;
}
after successful authentication in the receiver thread, a new thread is created for sending data to client and socket's outputStream is passed to the new thread.
the clientSender class contains a queue as a buffer which contains the data that should be sent to the client. here is class clientSender:
public class ClientSender extends Thread {
private Client client=null;
private final Log logger = LogFactory.getLog(getClass());
PrintWriter outputStream=null;
private Queue<String> clientEventsQueue= new LinkedList<String>();
public ClientSender(PrintWriter outputStream, Client client){
this.outputStream=outputStream;
this.client=client;
}
public void run(){
//System.out.println("ClientSender run method called.");
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!this.clientEventsQueue.isEmpty()){
this.outputStream.println(this.clientEventsQueue.remove());
}
}
}
public Client getClient(){
return this.client;
}
public void insertClientEvent(String clientEvent){
this.clientEventsQueue.add(clientEvent);
}
whenever I want to send something to the client I use:
clientSender.insertClientEvent("some text");
the problem is that if I remove Thread.sleep(10) I will not receive anything in the client side. Since TCP sockets are blocking I think this should not happen. Is this normal or am I doing something wrong?
EDIT:
there is no "termination" for the sender thread. the server should send proper information to all clients whenever it receives an event from another system. so I think the best scenario is to stop the thread when there is no data to send and start it whenever there is. so I tried this in the clientSender class:
public void run(){
while(true){
if(this.clientEventsQueue.isEmpty()){
break;
}
else{
try {
this.outputStream.println(this.clientEventsQueue.take());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
but now the problem is when to start the thread? I tried starting it whenever I want to send data but as I expected it does not work properly and only sends the fist package:
clientSender.insertClientEvent(clientEvent.getEventString());
clientSender.start();
EDIT2
I came up with this idea. It is very simple and I think it consumes so much less CPU time.
while(true){
while(this.clientEventsQueue.isEmpty()){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
this.outputStream.println(this.clientEventsQueue.take());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
as much as I tested it, it worked just fine. what do you think about it?
I see that you are using a LinkedList as a queue accessed by multiple threads and that you are doing a busy wait on it in the ClientSender.
This implementation is not thread safe and may cause problems like clientEvents posted on it not being made visible to the ClientSender thread, CPU being wasted etc.
You could use a BlockingQueue instead and call take() on it to block on the queue until an event is posted.
I also see that you are relying on sleep(400) to wait for the communication. That will also cause issues. The thread that is using the socket resource can close it when it's done, instead of this.
EDIT:
There are number of techniques to deal with terminating the thread. I think a "poison pill" will work well in this case. Basically you do:
String stuff = queue.take();
if (stuff == null) break;
and post null on the queue when you want to terminate it (doesn't have to be null, can be anything e.g. "terminate" etc.
EDIT2:
Your way of terminating won't work, as it will terminate immediately before anyone can post an event on it. Theoretically you could be spawning and then immediately killing the thread over and over etc. The easiest way to cope with this is to use a special message (aka "poison pill") for the termination condition.
As for having a thread only when there is an event, at that point I'd recommend using a thread pool. You could encapsulate the event sending into a Runnable, and hold the sockets in a Map. This is however quite complicated to implement and requires good understanding of multithreading to get it right. Multithreading is hard and can introduce severe headache when done wrong. Tbh I wouldn't recommend to try doing this without studying more about multithreaded programming.
EDIT3:
#user2355734: Polling the queue with an interval as you did is done by many people, but is discouraged. The take() method will in effect "sleep" and only wake up if there is something on the queue, so in theory by removing the "sleep" loop you should get even lower CPU usage and shorter latency. In general, you should try to avoid using "sleep" altogether in multithreaded code. It's rare that you genuinely need it and it's frequently a sign of broken/suboptimal code. As for tests, although they are useful it's hard or even pretty much impossible to guarantee correctness of multithreaded code through tests. Your code may run fine in your tests yet fail in production, under high load, under different environment etc. Therefore it's important to review the code and make sure it's theoretically correct.
I'm trying to use server sockets to set up a connection between a client and a server. I'm also not using java.nio.
The problem is that I'm constantly sending a test message, and detecting whether if it is successful in sending the message (the client is still connected), if not, then the client is disconnected.
Shown here:
try
{
Scanner in = new Scanner(socket.getInputStream());
BufferedReader in_2 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(stopThread)
{
if(in_2.ready())
{
String message = in_2.readLine();
dt = new DateTime();
PrintStream out = new PrintStream(socket.getOutputStream());
server.detect(message, dataSets, out);
dataSets.add(message);
GUI.textArea_1.append(message + "\r\n");
GUI.textArea_1.setCaretPosition(GUI.textArea_1.getDocument().getLength());
}
else
{
PrintStream out = new PrintStream(socket.getOutputStream());
out.println("Testing Connection \r\n");
if(out.checkError())
{
try
{
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
stopThread = false;
GUI.textArea.append(userName + " disconnected \r\n");
GUI.textArea.setCaretPosition(GUI.textArea.getDocument().getLength());
server.inputDataForm(userName, dt, dataSets);
}
Thread.sleep(3000);
}
}
The problem is that the Thread.sleep(3000) is actually interfering with getting data, since after 3 seconds, I will get a huge amount of data (because I stopped the thread for 3 seconds).
Now, what I proposed is a anonymous class in the else statement.
class runThread implements runnable
{
void run()
{
//Put the else statement here
}
}
But the stopThread = false is not a constant, which I'm trying to control.
Other threads I've searched only puts variables inside main inside the anonymous class, but I need stopThread to stop the while loop if the client is disconnected.
Does anyone have an idea?
Thanks!
Consider setting a short timeout on your socket. This will allow you to control how long your thread will block while waiting for data from the socket.
If data are not quickly available, a very specific java.net.SocketTimeoutException will be raised. You can handle this exception by checking your stopThread flag. If it is set, you can return from the method. Otherwise, the socket is still valid and you can try another read operation with timeout.
If any other exception type is thrown, your socket is probably no longer valid.
socket.setSoTimeout(20); /* 1/50th of a second. */
BufferedReader in = new BufferedReader
(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
while (!stop) {
try {
String message = in.readLine();
if (message == null)
handleEOF();
else
handleMessage(message);
} catch(SocketTimeoutException ignore) {
/* Loop back to check "stop" flag. */
continue;
} catch(IOException ex) {
handleDisconnection();
break;
}
}
By the way, if you are using Swing, remember that you can only modify graphical components from Swing's Event Dispatch Thread, and you can't tie up the EDT in long-running operations like this socket handling. You should be passing tasks from this thread to Swing's invokeLater() utility.
Why don't you make a class that implements runnable but also has the method stop();
public class MyRunner implements Runnable(){
MutableBoolean stop = false;
public void run(){...}
public void stop(){
stop = true;
}
}
I am trying to keep a connection open for a multithreaded server program. When I hit a button, I want it to send a test message to all clients that are connected.
public void run() {
try {
Scanner in = new Scanner(socket.getInputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream());
readUpdate(out, in);
while(true){sendUpdate(out);}
} catch (Exception e) {
e.printStackTrace();
}
}
Uses way to much CPU.
This is my sendUpdate method.
private void sendUpdate(final PrintWriter out) {
new Thread(new Runnable() {
public void run() {
if(Server.send) {
try {
if (Server.command != "idle") {
System.out.println("Sending");
out.println("!msg#" + Server.command);
out.flush();
Server.send = false;
Thread.sleep(100);
}
} catch (Exception ex) {
}
}
}
}).start();
}
If somebody can help me keep the connection open, and ready to send data, I would appreciate it.
If your server can initiate messages and so can your client, you probably want a separate thread reading and writing. One thread makes sense for request-response style communication, where you can block on the next client request, do some server-side processing, respond to the client, and then block again.
But if you need to block on two separate conditions (receiving a message from the client and you clicking the button on the server) then you should have two separate threads. Otherwise, you will find yourself needing to repeatedly wake your thread up to check if either of the conditions are true.
So create two threads, and give one your Scanner (that does the readUpdate logic) and the other your PrintWriter. This is what your output handler could look like:
public class WriteHandler implements Runnable {
private final PrintWriter out;
private final BlockingQueue<String> messageQueue = new LinkedBlockingQueue<String>();
//initialize the above in a constructor;
public void run() {
while(true) {
String nextMessageToWrite = messageQueue.poll();
out.println(nextMessageToWrite);
}
}
public void send(String message) {
messageQueue.add(message);
}
}
This uses a blocking queue, which is a much better concurrency mechanism than a check-sleep loop. Then when the button is clicked, you can just have something like this:
public void actionPerformed() {
for ( WriteHandler handler : handlers ) {
handler.send("PING!");
}
}