In my android app I need to send Data between two apps. When the GameClient get initialized, it will start three Threads.
The first one will create a Socket and the ReceivingThread. I need to do it in a Thread, because Android won't let me do network connection on MainThread:
new Thread(new Runnable() {
#Override
public void run() {
try {
if (mSocket == null) {
setSocket(new Socket(mAddress, mPort));
Log.d(TAG, "Client-Socket set.");
} else {
Log.d(TAG, "Socket already set. Skip.");
}
mRecCardThread = new ReceivingCardThread();
mRecCardThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
mSendCardThread = new SendingCardThread();
mSendCardThread.start();
After that I create the third Thread for Sending data.
The problem occurs when I connect to another phone and immediately want to send some Data.
mConnection.connectToServer(d.getHost(), d.getPort());
mConnection.sendCard(new int[]{0, 0, 0});
My Sending Thread tries to Send data, but will often be called earlier than the setSocket method.
private synchronized void setSocket(Socket socket) {
Log.d(TAG, "setSocket called");
if (mSocket != null) {
if (mSocket.isConnected()) {
try {
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
mSocket = socket;
if (mClient != null)
mClient.mSendCardThread.notifyAll();
Log.d(TAG, "Notify all, that Socket is set");
}
private class SendingCardThread extends Thread {
BlockingQueue<int[]> mCardQueue = new ArrayBlockingQueue<>(10);
private ObjectOutputStream mOutput;
#Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
int[] nums = mCardQueue.take();
sendCard(nums);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private synchronized void sendCard(int[] nums) {
try {
if (mOutput == null) {
while (mSocket == null) {
Log.d(TAG, "Waiting for Socket to be set.");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d(TAG, "Not waiting anymore.");
}
mOutput = new ObjectOutputStream(mSocket.getOutputStream());
}
mOutput.writeObject(nums);
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "Client send:" + Arrays.toString(nums));
}
}
So I thought about to let the sending-Thread wait when the Socket is still null and when the Socket is created wakeing up the sending-Thread to be sure that the Socket is not null at the beginning.
But this will cause a IllegalMonitorStateException: object not locked by thread before notifyAll().
So it seems like it tries to notify but the Sending Thread isn't waiting. But I don't know how to solve this problem. I think also that I don't understand how to use wait() and notify() in praxice.
I hope one of you guys can help me to solve this stupid problem ;D
Related
So my problem is I have a client with a Runnable that readobjects in background from the socket. In the server i send multiple times objects like notifications updates etc by writeUnshared, but the client is only receiving them when I send a request back to server by writeUnshared.
\ClientThread.java\
public class ThreadClientInFromServer implements Runnable {
Socket socket;
ClientData clientData;
public ThreadClientInFromServer(Socket socket, ClientData clientData) {
this.socket = socket;
this.clientData = clientData;
}
#Override
public void run() {
ObjectInputStream in;
ObjectOutputStream out;
out = clientData.getOut();
in = clientData.getIn();
while (!socket.isClosed()) {
try {
Object object = in.readObject();
clientData.updateData(object);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
SendRequestClient.java Only when i send this request it refresh and come the updates,notifications,etc
public void sendRequest(Request request) {
try {
out.writeObject(request);
out.flush();
out.reset();
} catch (IOException e) {
System.out.println("[ERROR] ON SEND REQUEST!");
return;
}
}
On Server (KEEPALIVETCP.java) for example, he doesnt receive.
public class KeepAliveTCP implements Runnable {
ServerModel serverModel;
public KeepAliveTCP(ServerModel serverModel) {
this.serverModel = serverModel;
}
#Override
public void run() {
Request request = new Request(null, Constants.ACK);
while (!serverModel.getSocket().isClosed()) {
try {
for (SocketModel clients : serverModel.getModelClientes()) {
if (clients.getNome() != null) {
clients.getOut().writeUnshared(request);
clients.getOut().flush();
}
}
sleep(5000);
watchWhoFails();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks in Advance!!!
EDIT : So I was trying to find a solution and come up with my cliente blocking the thread(ThreadClientInFromServer) when he uses scanner.next() on the menus(that user uses to browse on the application). So I dont really know how to solve this problem, if you guys know some solution for this!
Thanks.
Edit: I know what Thread.interrupt() does.
while (!Thread.currentThread().isInterrupted()) does not exit when I interrupt the thread.
I also tried to catch an exception from url.openStream(); when the
thread is interrupted (desperation, maybe it was a blocking method,
which is not) and exit the loop, without any success.
The application creates a Thread that continuously reads a URL. After 3 seconds that Thread gets interrupted but unfortunately continues to execute.
How to stop the thread from executing?
Code (Main.java, MyRunnable.java):
public class Main {
public static void main(String[] args) {
MyRunnable runnable = new MyRunnable("http://ninjaflex.com/");
Thread thread = new Thread(runnable);
thread.start();
sleep(3000);
thread.interrupt();
System.out.println("Thread.interrupt() invoked.");
}
private static void sleep(long timeMilli) {
try {
Thread.sleep(timeMilli);
} catch (Exception e) {
}
}
}
public class MyRunnable implements Runnable {
private String website;
MyRunnable(String website) {
this.website = website;
}
#Override
public void run() {
URL url = createUrl();
if (url != null) {
while (!Thread.currentThread().isInterrupted()) {
sleepOneSec();
readFromUrl(url);
System.out.println("Read from " + website);
}
System.out.println("Script: Interrupted, exiting.");
}
}
private URL createUrl() {
URL url = null;
try {
url = new URL(website);
} catch (MalformedURLException e) {
System.out.println("Wrong URL?");
}
return url;
}
private void sleepOneSec() {
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println("Error sleeping");
}
}
private void readFromUrl(URL url) {
InputStream in = null;
try {
in = url.openStream();
} catch (Exception e) {
System.out.println("Exception while url.openStream().");
e.printStackTrace();
} finally {
closeInputStream(in);
}
}
private void closeInputStream(InputStream in) {
try {
in.close();
} catch (IOException e) {
System.out.println("Error while closing the input stream.");
}
}
}
Basically, your MyRunnable thread is interrupted during sleep. InterreuptedException is thrown but catched. By the way, it's a bad habit to catch Exception and you should not do that.
From the javadoc: "The interrupted status of the current thread is cleared when this exception is thrown".
Therefore, your while loop will never see the flag.
Replace the call to the sleepOneSec method with a simple Thread.sleep call. Catch InterruptedException outside your while loop. This will cause the loop to exit naturally:
try {
while (true) {
Thread.sleep(1000);
readFromUrl(url);
System.out.println("Read from " + website);
}
} catch (InterruptedException e) {
System.out.println("Script: Interrupted, exiting.");
}
I removed the MyRunnable.sleepOneSec and your code started to work.
Edited my question for clarification and code:
My goal is to pass my String data from my background thread, to my main application thread. Any help is appreciated.
Here is the code that creates the main background thread. This is located in my Server.java class
public class Server {
boolean isConnected = false;
Controller controller = new Controller();
public void startHost() {
Thread host = new Thread(() -> {
Controller controller = new Controller();
ServerSocket server = null;
try {
server = new ServerSocket(GeneralConstants.applicationPort);
} catch (BindException e2) {
System.out.println("Port Already in Use!");
} catch (IOException e) {
//do nothing
}
while (true) {
if (server == null) { break; }
try {
Socket client = server.accept();
System.out.println("Client Connected: " + isConnected);
if (!isConnected) {
controller.createClientHandler(client);
isConnected = true;
System.out.println("Client Connected: " + isConnected);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
host.setDaemon(true);
host.start();
}
Here is the code that is then called when a client is connected, located in my Controller.java class.
public synchronized void createClientHandler(Socket client) {
boolean alreadyConnected = false;
if (alreadyConnected) {
//do NOT assign multiple threads for each client
} else {
ClientHandler handleClients = new ClientHandler("client", client);
}
}
The program then creates two background threads for my client, one to manage receiving messages, and sending messages.
public ClientHandler(String name, Socket s) {
clientSocket = s;
clientName = name;
receiveThread = new Thread(this::receive);
sendThread = new Thread(this::send);
connected = clientSocket.isConnected();
receiveThread.start();
sendThread.start();
}
The thread then successfully creates the inputstream and passes the object to my controller. Which then process and grabs a string assigning it to a variable
public synchronized void handleReceivedPacket(String name, BufferedReader in) {
try {
data = in.readLine();
System.out.println("Successfully assigned data to: " + data);
} catch (IOException e) {
System.out.println("Unable to read result data");
}
}
How do I access my String data from the main thread without getting null?
Aka I can call (or something similar)
controller.returnData();
from my main application. From which it'll either return null (no data yet), or actually return my data. Right now, it's always null.
Edit, this is what's actually calling controller.returnData() {
I don't want to paste a massive amount of code for fear of reaching StackOverflow's code limit, so here's my application structure.
My JavaFX creates the scene, and creates a root gridpane, it then calls a method that creates sub gridpanes based the specified input. Aka, a user can press "Main Menu" that calls my method setScene() which removes the current "sub-root" gridpane and creates a "new" scene. Right now, I have a GameBoard.java class which on button press, calls controller.returnData()
PassOption.setOnAction(event -> {
System.out.println(controller.returnData());
});
There is no functional purpose for this besides testing. If I can receive the data, then I can expand on this using the data.
Start thinking about design. In network applications you typically have to manage the following responsibilites:
Connected clients and their state (connection state, heartbeats, ...)
Received messages from the clients
Messages to transmit to the clients
It makes sense to separate those responsibilities in order to keep the code clean, readable and maintainable.
Separation can mean both, thread-wise and class-wise.
For example, you could implement it as follows:
The class ClientAcceptor is responsible for opening the socket and accepting clients. As soon as a client has connected, it delegates the further work to a controller and then waits for other clients:
public class ClientAcceptor implements Runnable {
#Override
public void run() {
while (true) {
ServerSocket server;
try {
server = new ServerSocket(1992);
Socket client = server.accept();
if (client.isConnected()) {
controller.createClientHandler(client);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
The controller could then create a handler (if the controller decides to do so, e.g. it could also decline the client). The ClientHandler class could look as follows:
public class ClientHandler {
private Thread receiveThread;
private Thread sendThread;
private boolean connected;
private Socket clientSocket;
private String clientName;
private LinkedBlockingDeque<byte[]> sendQueue;
public ClientHandler(String name, Socket s) {
clientSocket = s;
clientName = name;
receiveThread = new Thread(() -> receive());
sendThread = new Thread(() -> send());
connected = clientSocket.isConnected();
receiveThread.start();
sendThread.start();
}
private void receive() {
BufferedInputStream in = null;
try {
in = new BufferedInputStream(clientSocket.getInputStream());
} catch (IOException e) {
connected = false;
}
while (connected) {
try {
byte[] bytes = in.readAllBytes();
if (bytes != null && bytes.length > 0) {
controller.handleReceivedPacket(clientName, bytes);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void send() {
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(clientSocket.getOutputStream());
} catch (IOException e) {
connected = false;
}
while (connected) {
byte[] toSend = sendQueue.getFirst();
if (toSend != null && toSend.length > 0) {
try {
out.write(toSend);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void send(byte[] packet) {
sendQueue.add(packet);
}
public void close() {
connected = false;
}
}
The ClientHandler is responsible for receiving and transmitting data. If a packet arrives it informes the controller, which parses the packet. The ClientHandler also provides a public API to send data (which is stored in a queue and handled by a thread) and close the connection.
The above code examples are neither tested, nor complete. Take it as a starting point.
Here is my programme, which have a two threads, one is listening user input, another is a socket:
bio = new BasicConsoleIO();
bio.assignObject(worker);
Thread b = new Thread(bio);
b.start();
Thread a = new Thread(worker);
a.start();
The worker is a socket, and the BasicConsoleIO is responsible for listening the user input
The BasicConsoleIO is something like that:
private Worker worker;
static BufferedReader reader;
#Override
public void run() {
//......Code Skip......//
if (inputString.equalsIgnoreCase("q")) {
this.applicationQuit();
}
}
public void applicationQuit(){
this.getWorker().stopWorking();
System.exit(0);
}
When it press 'q', the application will call the worker to close the socket, and quit the program, and the Worker works this way:
private ServerSocket providerSocket;
private Socket socket = null;
int port = 1234;
Worker() {
}
public void stopWorking() {
System.out.println("worker stop working");
try {
if (providerSocket != null) {
providerSocket.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
#Override
public void run() {
try {
providerSocket = new ServerSocket(this.port);
while (true) {
if (!providerSocket.isClosed()) {
socket = providerSocket.accept();
WorkTask wt = new WorkTask();
wt.setSocket(socket);
Thread a = new Thread(wt);
a.start();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
The worker will keep listening the request, and assign the new work task in a separate thread, the details of worktask like this:
Socket socket;
ObjectOutputStream out;
ObjectInputStream in;
Object receivedObj;
String message;
#Override
public void run() {
try {
do {
out.flush();
receivedObj = in.readObject();
//......Code Skip......//
} while (receivedObj != null
&& !receivedObj.equals(SharedConstant.SOCKET_EOF_STRING));
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
But When I launch the programme, and press 'q' to exit, it warns me with this error:
Please assign a port number 2333 Press 'q' to kill to programme
Waiting for connection: 2333 q worker stop working Run me anyway!
java.net.SocketException: Socket closed at
java.net.PlainSocketImpl.socketAccept(Native Method) at
java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408) at
java.net.ServerSocket.implAccept(ServerSocket.java:462) at
java.net.ServerSocket.accept(ServerSocket.java:430) at
com.mydefault.package.Worker.run(Worker.java:61) at
java.lang.Thread.run(Thread.java:680)
You can see the
socket = providerSocket.accept();
is throwing an exception because in
public void stopWorking() {
// Socket won't close unless the user make it to close
// 4: Closing connection
System.out.println("worker stop working");
try {
if (providerSocket != null) {
providerSocket.close();
you closed it.
If you want to avoid this error, I have a volatile boolean closed field which I set to true and check before I report an error. i.e. ignore errors when I am closing down.
I have a server in Java which is multithreading, and I've created a thread pool for it.
Now everything goes well and my server accepts and reads data from the clients that connect to it, but I don't know really how to clean up the sockets after the connections are closed.
So here is my code:
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ThreadPooledServer server = new ThreadPooledServer(queue,7001);
new Thread(server).start();
}
ThreadPooledServer class:
public class ThreadPooledServer implements Runnable {
protected ExecutorService threadPool = Executors.newFixedThreadPool(5);
public ThreadPooledServer(BlockingQueue queue,int port) {
this.serverPort = port;
this.queue=queue;
}
public void run() {
openServerSocket();
while (!isStopped()) {
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
clientconnection++;
} catch (IOException e) {
if (isStopped()) {
System.out.println("Server Stopped.");
return;
}
throw new RuntimeException("Error accepting client connection", e);
}
WorkerRunnable workerRunnable = new WorkerRunnable(queue,clientSocket);
this.threadPool.execute(workerRunnable);
}
this.threadPool.shutdown();
System.out.println("Server Stopped.");
}
private synchronized boolean isStopped() {
return this.isStopped;
}
public synchronized void stop() {
this.isStopped = true;
try {
this.serverSocket.close();
}
catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
Here's what I don't understand: My while() loop that accepts for clients works as loong as isStopped is false.
When isStopped is set to true, my loop ends and then I shut down my thread pool, which is correct.
isStopped is set to true in onstop(){..............}....
Where should I call onstop()...?
Because in this moment I'm not using this method ,I'm not calling it and that means that I'm not cleaning correctly my threads.
WorkerRunnable class:
public class WorkerRunnable implements Runnable {
public WorkerRunnable(BlockingQueue queue2, Socket clientSocket2) {
// TODO Auto-generated constructor stub
this.clientSocket2 = clientSocket2;
this.queue2 = queue2;
}
public void run() {
try {
is = new ObjectInputStream(this.clientSocket2.getInputStream());
try {
while (!stop) {
System.out.println("Suntem in interiorul buclei while:!");
v = (Coordinate) is.readObject();
queue2.put(v);
}
} catch (Exception e) {
e.printStackTrace();
is.close();
clientSocket2.close();
}
is.close();
clientSocket2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stop() {
this.stop = true;
}
}
}
Here I have the same issue. How should I clean and close up my sockets correctly?
Once you call shutdown() the thread pool will close off each thread once all the tasks are complete.
You should call onstop() from whatever code knows the pool should be shutdown. This depends on what the rest of your application does and why you would stop the pool before the application has finished.