I have a socket listener thread in the client side which receives data from the server. Currently I'm just getting the data input stream from socket and reading data from the stream. I want to modify my listener thread in such way that processing of that thread is blocked until data is received in the socket instead of checking my input stream always.
My socket listener thread is given below
public void run() {
listenFlag = true;
try {
//instream is a global variable
inStream = new DataInputStream(clientSocket.getInputStream());
while (listenFlag) {
messageType = -1;
if (readHeader()) {// Check for correct header
Application.getLogger().log(Level.SEVERE, "<<< passed readHeader() ----");
if (readType()) {// Check for valid Type
Application.getLogger().log(Level.SEVERE, "<<< passed readType ----");
readAndProcessMessage();// Read and process the correct message
Application.getLogger().log(Level.SEVERE, "<<< passed readAndProcessMessage ----");
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
} catch (InterruptedException e) {
Application.getLogger().log(Level.SEVERE, "Interrupted socket communication - closing socket" + e.getMessage());
} catch (InterruptedIOException e) {
Application.getLogger().log(Level.SEVERE, "Interrupted IOException socket communication - closing socket" + e.getMessage());
} catch (IOException ioe) {
Application.getLogger().log(Level.SEVERE, "Socket read error : " + ioe.getMessage());
} catch (Exception e) {
Application.getLogger().log(Level.SEVERE, "Unknown exception on SocketListenThread:"+ e.getMessage());
} finally {
Application.getLogger().log(Level.SEVERE, "SocketListenThread: listenFlag : "+ listenFlag);
Application.getLogger().log(Level.SEVERE, "Quiting SocketListnerThread Thread");
DRISMCMStatus.socketListenerStatus = DRISMCMStatus.NOT_ALIVE;
closeInputStream();
}
}
//Read header called from above.
private boolean readHeader() throws Exception {
Application.getLogger().log(Level.INFO, "Reading header..");
DRISMCMStatus.socketListenerStatus = DRISMCMStatus.WAITING;
int val = inStream.read();
DRISMCMStatus.socketListenerStatus = DRISMCMStatus.RUNNING;
Application.getLogger().log(Level.INFO, "[READ]: " + val);
if (val == -1) {
throw new IOException();
}
if ( val == SYNCCHAR) {
return true;
}
return false;
}
Related
I want to transfer objects (AssignmentListener) from one Java Server to 5 Java Clients.
Therefore I wrote a method to send out the message:
private void sendMessage(AssignmentListener listener, int[] subpartitionIndices){
boolean success = false;
int failCount = 0;
// retry for the case of failure
while(!success && failCount < 10) {
try {
// get the stored socket & stream if stored
if(listener.getSocket() == null) {
if (localMode) {
listener.setSocket(new Socket("localhost", listener.getPort()));
} else {
listener.setSocket(new Socket(listener.getIp(), listener.getPort()));
}
listener.setOutputStream(new ObjectOutputStream(listener.getSocket().getOutputStream()));
}
AssignmentListenerMessage assignmentListenerMessage = new AssignmentListenerMessage(subpartitionIndices);
System.out.println("Sending " + assignmentListenerMessage);
listener.getOutputStream().writeObject(assignmentListenerMessage);
listener.getOutputStream().flush();
success = true;
} catch (IOException se) {
se.printStackTrace();
System.err.println("Failed to forward " + Arrays.toString(subpartitionIndices) + " to " + listener);
failCount++;
}
}
}
On the client side, I have the following:
public void run() {
String mode = "remote";
if(localMode) mode = "local";
// we need to register this listener at at the OverpartitioningManager
if(register(isLocalRequest)) System.out.println("Registered AssignmentListenerServer for index "+subpartitionIndex+" at ForwardingServer - "+mode);
running = true;
while (running) {
try {
socket = serverSocket.accept();
// Pass the socket to the RequestHandler thread for processing
RequestHandler requestHandler = new RequestHandler( socket );
requestHandler.start();
} catch (SocketException se) {
se.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class RequestHandler extends Thread {
private Socket socket;
RequestHandler(Socket socket) {
this.socket = socket;
}
#Override
public void run() {
try {
System.out.println("Received a connection");
// Get input and output streams
inStream = new ObjectInputStream(socket.getInputStream());
//outStream = new DataOutputStream(socket.getOutputStream());
AssignmentListenerMessage incomingMessage = null;
while(socket.isBound()) {
try {
incomingMessage = (AssignmentListenerMessage) inStream.readObject();
}catch (StreamCorruptedException sce){
System.out.println("Failed to read AssignmentMessage from Stream, but will try again... (no ack)");
sce.printStackTrace();
continue;
}
// do stuff with the message
}
// Close our connection
inStream.close();
socket.close();
System.out.println("Connection closed");
} catch (Exception e) {
e.printStackTrace();
}
}
}
This works multiple times, but at one point I get the following exception:
java.io.StreamCorruptedException: invalid type code: 00
Does anyone have an idea or any other performance improvement for what I'm doing?
Thanks.
I have a classic server-multi-clients program. Tthe server listens to ServerSocket and for each incoming socket it builds a new Runnable class and executes it in ExecuteService.
In the run method of the Runnable class, I open try-with-resources block and in the try I have a while loop that reads from inputstream and writes to outputstream until it receives FIN command from the clients. Everything works fine and the clients disconnect successfully. The run reaches the finally block and prints some stuff for testing, but it doesn't exit the try block so it does not exit the run method and I am stuck in the run somewhere, maybe the read method of the inputstream.
I can post the code if anyone interested.
How can I force close everything in the finally and exit the run method?
The code:
Server.java:
public static void main(String[] args) {
playersReady = new ArrayList<Integer>();
ServerSocket server = null;
try {
server = new ServerSocket(Consts.PORT);
ExecutorService service = Executors.newFixedThreadPool(characters.size());
while(playersReady.size()<characters.size()){
RequestHandler handler = new RequestHandler(server.accept());
service.execute(handler);
}
service.shutdownNow();
service.shutdown();
while(!service.isTerminated()){}
System.out.println("finished");
RequestHandler.java
public final class RequestHandler implements Runnable {
.....
public void run() {
//DataOutputStream output = null;
//DataInputStream input = null;
try (DataOutputStream output = new DataOutputStream(socket.getOutputStream());
DataInputStream input = new DataInputStream(socket.getInputStream())){
// socket.setSoTimeout(500);
handleReady(input.readUTF().split(" "), output);
while (/*!shutdown && !socket.isClosed() && */socket.isConnected()) {
System.out.println("check before read " + character.getId());
String request = input.readUTF();
System.out.println("check after read " + character.getId());
System.out.println("-----------------------------------" + request);
if (shutdown) {
socket.shutdownInput();
socket.getOutputStream().flush();
socket.shutdownOutput();
break;
}
String[] requestParser = request.split(" ");
if (requestParser[1].equals("DMG")) {
// handle damage request
handleDamage(requestParser, output);
} else if (requestParser[1].equals("BND")) {
// handle bandage request
handleBandage(requestParser, output);
} else if (requestParser[1].equals("FIN")) {
// handle finish request
handleFin();
if (!socket.isClosed())
socket.shutdownInput();
if (!socket.isClosed()) {
socket.getOutputStream().flush();
socket.shutdownOutput();
}
shutdown = true;
break;
} else {
break;
}
}
} catch (SocketTimeoutException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
shutdown = true;
break;
} catch (Throwable e) {
e.printStackTrace();
} finally {
try {
System.out.println("finished");
if (!socket.isClosed())
socket.shutdownInput();
if (!socket.isClosed()) {
socket.getOutputStream().flush();
socket.shutdownOutput();
socket.close();
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Done run");
}
....
The System.out.println("finished") in the finally is printed,
but the System.out.println("Done run") in the end of the run method does not!!
Why?
It stuck in the run method, I think in the readUTF call, but I closed all the resources!
You return before that line, that's why it is not run. The finally block is run anyway, because it is a finally block. Finally blocks are always run, there is only one exception from this rule: System.exit(), but this is not the case.
I have one client file clientRPC.java and server file serverRPC.java. Both communicate using TCP protocol and use objectinput and output stream to transfer data.
my client file:
public class clientRPC {
public static void main(String args[]) {
Socket s = null;
try {
int serverPort = 8888;
s = new Socket("localhost", serverPort);// server name is local host
//initializing input and output streams object and referencing them to get input and output
ObjectInputStream in = null;
ObjectOutputStream out = null;
out = new ObjectOutputStream(s.getOutputStream());
in = new ObjectInputStream(s.getInputStream());
MathsTutor mt = new MathsTutor();
out.writeObject(mt);
out.flush();
System.out.println("Welcome to Maths Tutor Service. The available maths exercises are:\n"
+ "Addition: Enter 'A' or 'a'\n"
+ "Subtraction: Enter 'S' or 's'\n"
+ "Multiplication: Enter 'M' or 'm'\n"
+ "Division: Enter 'D' or 'd'\n"
+ "Enter 'Q' or 'q' to quit");
//System.out.println();
MathsTutor mt1 = (MathsTutor) in.readObject();
String response = in.readUTF();
System.out.println(response);
} catch (UnknownHostException e) {
System.out.println("Socket:" + e.getMessage());
} catch (EOFException e) {
System.out.println("EOF:" + e.getMessage());
} catch (IOException e) {
System.out.println("readline:" + e.getMessage());
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
System.out.println("close:" + e.getMessage());
}
}
}
}
}
and my server file :
public class serverRPC extends Thread {
String request;
String response;
public static void main(String args[]) {
try {
int serverPort = 8888;
ServerSocket listen_socket = new ServerSocket(serverPort);
while (true) {
Socket clientSocket = listen_socket.accept();
Connection c = new Connection(clientSocket);
}
} catch (IOException e) {
System.out.println("Listen socket:" + e.getMessage());
}
public serverRPC(String s) {
request = s;
}
}
class Connection extends Thread {
ObjectInputStream in;
ObjectOutputStream out;
Socket clientSocket;
public Connection(Socket aClientSocket) {
try {
clientSocket = aClientSocket;
in = new ObjectInputStream(clientSocket.getInputStream());
out = new ObjectOutputStream(clientSocket.getOutputStream());
this.start();
} catch (IOException e) {
System.out.println("Connection:" + e.getMessage());
}
}
public void run() {
try {
MathsTutor mt = (MathsTutor) in.readObject();
InetAddress ip = clientSocket.getInetAddress();
System.out.println("The Received Message from Client at address:/" + ip.getHostAddress());
System.out.println("====================================");
MathsTutor mt1 = new MathsTutor();
out.writeObject(mt1);
while(true) {
// Read from input
String command = in.readUTF();
System.out.println(command);
}
//System.out.println();
} catch (EOFException e) {
System.out.println("EOF:" + e.getMessage());
} catch (IOException e) {
System.out.println("readline:" + e.getMessage());
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {/*close failed*/
}
}
}
}
The problem is when I run server and then client on cmd, the client side displays the welcome msg and puts cursor on another line for user input but, I can't type anything, the cursor just blinks... I know this might be simple but it has taken already 3 hours for me and I'm stuck in the same thing.
The cursor marked with red keeps blinking but doesn't let me type anything.
You're writing an object with writeObject() and trying to read it with readUTF(). Illogical.
objects written with writeObject() must be read with readObject().
strings written with writeUTF() must be read with readUTF().
primitives written with writeXXX() must be read with readXXX(), for most values of X.
I have a server that accept more then one Client. Every Client is stored inside an ArrayList of Sockets. If a Client, for some reason, disconnects from my server, this should understand which client has been disconnected, close the client and delete it from the List.
Reading other question here I have understood that the only way to understand which client is disconnected is this: try to send data through all socket connected and the first one which throws Exception has to be closed.
The problem is that if the client is a simple Java-Application it works perfectly. But when the Client is an Android-Application, the data is sent through the (supposed) disconnected Socket. In this way my Server's algorithm doesn't throw Exceptions and it keeps sending data to all Sockets Causing a disaster. The code of clients (java and android) is Exactly the same but the results are different:
Server Code:
List<Socket> sList = new ArrayList<>();
Socket s;
int i = 0;
int whichSocket;
try
{
ServerSocket ss = new ServerSocket(7000);
while (true)
{
System.out.println("Server is Listening");
s = ss.accept();
sList.add(s);
System.out.println("Server Accepted Client --- " +s.toString());
Thread t2 = new Thread(new Runnable() {
public void run() {
try
{
DataInputStream dis = new DataInputStream(s.getInputStream());
while (true)
{
// For every message received from one client, it iterate through list sending that dat to ALL CLIENTS in the list of Socket
String test = dis.readUTF();
System.out.println("Message Sent By -- " + s.toString());
System.out.println(test);
while(i < sList.size()){
DataOutputStream dos = new DataOutputStream(sList.get(i).getOutputStream());
dos.writeUTF(test);
System.out.println("Messaggio Sent To -- " + sList.get(i).toString());
dos.flush();
++i;
}
i=0;
}
} catch (IOException e)
{
System.out.println("First Exception");
e.printStackTrace();
} finally
{
try
{
// An exception has been thrown. This means that one client is disconnected.Which One? Let's send data to all Clients in the list.
whichSocket = -1;
for(Socket temp : sList)
{
System.out.println("PENEEEE inviato a -- " + temp.toString());
whichSocket ++;
DataOutputStream dosser = new DataOutputStream(temp.getOutputStream());
dosser.write(1);
System.out.println("Message Sent To -- " + temp.toString());
dosser.flush();
}
}
catch (IOException e)
{
System.out.println("Second Exception");
e.printStackTrace();
}finally
{
try
{
sList.get(whichSocket).close();
System.out.println("Socket Closed --- " + sList.get(whichSocket).toString());
sList.remove(whichSocket);
} catch (IOException e) {
System.out.println("Third Exception");
e.printStackTrace();
}
}
}
}
});
t2.start();
}
} catch (IOException e) {
System.out.println("General Exception");
e.printStackTrace();
}
Client Code:
while(flag) {
if(!isConnected) {
try {
s = new Socket("192.168.1.69", 7000);
isConnected = true;
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
while (flag) {
String result = dis.readUTF();
Log.d("InputStreammmm", result);
}
} catch (IOException e) {
Log.d("THIS IS", "THE EXCEPTIONN");
e.printStackTrace();
} finally {
isConnected = false;
}
}
}
We are creating a java listener to read multiple device data those are configured on particular server ip and port.Device following below rule.
device send a login packet.
server will return ack packet in response.
after receive ack device will send information packet.
server reads that data.
on last step we stuck, we are sending the ack but cant get the information packet back from device(though we check the generated ack through opensource sofware).For ref we are attaching code.(if we remove while(true) than get login packet but after that socket connection will close and again device will send login packet but if we keep it then we dont get any packet)
//--------------Main class------------------------------------------
public class Main {
public static void main(String[] args) {
Server server = new Server(listen_port, pool_size, pm);
new Thread(server).start();
logger.info("Server Started .....");
}
}
//--------------------------------------------------------------
public class Server implements Runnable {
private ServerSocket serverSocket = null;
public void run()
{
this.m_stop = false;
while (!this.m_stop)
try {
this.m_pool.execute(new Handler(this.serverSocket.accept()));
} catch (IOException e) {
LOGGER.debug("Unable to accept connection ", e);
}
}
}
//--------------------------------------------------------------
public class Handler implements Runnable {
private Socket m_clientSocket;
private String imei;
public Handler(Socket socket) {
this.m_clientSocket = socket;
}
public void run() {
DataOutputStream clientDataOS = null;
DataInputStream clientDataIS = null;
try {
logger.info("data is coming");
m_clientSocket.setSoTimeout(300000);
clientDataIS = new DataInputStream(this.m_clientSocket.getInputStream());
clientDataOS = new DataOutputStream(this.m_clientSocket.getOutputStream());
while (true) {
String pkt = "";
logger.info("Waiting for input strem");
byte[] byte_pkt = IOUtils.toByteArray(clientDataIS);
logger.info("Got input stream");
for (byte b : byte_pkt) {
pkt += String.format("%02X ", b);
}
logger.info(pkt);
if (byte_pkt.length > 0) {
logger.info("");
if (Byte.valueOf(byte_pkt[3]) == 1) {
imei = "xyz";
logger.info("login packet");
byte[] rep_pkt = Utils.getReceptionPacket(byte_pkt);//send back to device
clientDataOS.write(rep_pkt);
clientDataOS.flush();
} else if (Byte.valueOf(byte_pkt[3]) == 34) {
logger.info("information packet");
Utils.processPackets(byte_pkt);
} else {
logger.info("Unkown packet format");
}
logger.info(imei);
} else {
logger.info("InputStream is empty.");
}
}
} catch (SocketException se) {
logger.error("Failure on reading data", se);
} catch (IOException e) {
logger.error("Failure on reading data", e);
} catch (Exception e) {
logger.error("Error while processing data", e);
} finally {
try {
IOUtils.closeQuietly(clientDataOS);
IOUtils.closeQuietly(clientDataIS);
this.m_clientSocket.close();
} catch (IOException e) {
logger.debug("Error when sending out response ::", e);
}
}
}
}