I am developing a WEB based GPS system and one of its functionalities is to be able to send messages and routes to GARMIN devices.
I have never been using sockets before, and for some (obvious) reason, I don't feel ready to submit my code before being reviewed by more experienced users.
If some of you have the time to check it, here it is:
//This is an inner class - that is why it is private
private class MessageReceiver implements Runnable
{
private Thread clientSocketListener = null;
private Socket clientSocket = null;
private Socket currentSocket = null;
private ServerSocket mReceiverSocket = null;
private DataInputStream in = null;
boolean shutDownServer = false;
boolean stopLoop = false;
public MessageReceiver()
{
try
{
if(mReceiverSocket == null) { mReceiverSocket = new ServerSocket(12346); }
System.out.println("mReceiverSocket initialized");
}
catch (IOException ex) {ex.printStackTrace();}
}
/**
* Listens for clients...
*/
private void initConnection()
{
clientSocketListener = new Thread(new Runnable()
{
private boolean loopStatus = true;
@Override
public void run()
{
while(loopStatus)
{
try {Thread.sleep(200);}
catch (InterruptedException ex) {ex.printStackTrace();}
try
{
clientSocket = mReceiverSocket.accept();
if(currentSocket != null) { currentSocket.close(); }
currentSocket = clientSocket;
System.out.println("new clientSocket accepted");
}
catch(SocketException e)
{
System.out.println("SocketException initConnection() ");
e.printStackTrace();
}
catch(IOException e)
{
System.out.println("Exception initConnection()");
e.printStackTrace();
}
finally
{
if(shutDownServer)
{
System.out.println("Breaking while looop for client listening");
loopStatus = false;
}
}
}
}
});
clientSocketListener.setPriority(3);
clientSocketListener.start();
}
@Override
public void run()
{
this.initConnection();
System.out.println("After calling initConncetion()");
byte currentByte = 0;
byte previousByte = 0;
boolean firstRun = true;
LinkedList<Byte> inputDataArray = new LinkedList<Byte>();
while(stopLoop == false)
{
try
{
if(currentSocket != null)
{
in = new DataInputStream(currentSocket.getInputStream());
System.out.println("After checking if currentSocket is null");
int nextByte;
while((nextByte = this.in.read()) != -1)
{
if(firstRun)
{
firstRun = false;
previousByte = (byte) nextByte; //in.readByte();
if(previousByte == (byte) 0xa5) inputDataArray.add(previousByte);
}
else
{
currentByte = (byte) nextByte; //in.readByte();
if(previousByte == 21 && currentByte == 22)
{
inputDataArray.clear();
System.out.println("Na4alo...");
}
else if(previousByte == 23 && currentByte == 24)
{
//Do something here...
if(type.equals(GarminMessage.MESSAGE_TYPE_GARMIN_TEXT))
{
//Do what is supposed to do...
}
else if(type.equals(GarminMessage.MESSAGE_TYPE_GARMIN_ACKNOWLEDGEMENT))
{
//Do what is supposed to do...
}
else if(type.equals(GarminMessage.MESSAGE_TYPE_SYNCHRONIZE_POINT_ACCNOWELDGEMENT))
{
//Do what is supposed to do...
}
else if(type.equals(GarminMessage.MESSAGE_TYPE_ETA_ACCNOWELDGEMENT))
{
//Do what is supposed to do...
}
inputDataArray.clear();
firstRun = true;
System.out.println("Krai!");
}
else
{
inputDataArray.add(currentByte);
previousByte = currentByte;
if(inputDataArray.size() == 5)
{
boolean flag = true;
for(int i=0; i<inputDataArray.size(); i++)
{
byte a = inputDataArray.get(i); //System.out.println("data: " + a);
if(a != (byte) 0xa5)
{
flag = false;
break;
}
}
if(flag == true)
{
//System.out.println("Handshake message received");
inputDataArray.clear();
firstRun = true;
byte handShakeMessage[] = new byte[]{(byte) 0xa6,(byte) 0xa6,(byte) 0xa6,(byte) 0xa6,(byte) 0xa6};
try
{
MessageService.this.messageSender.out.write(handShakeMessage);
MessageService.this.messageSender.out.flush();
}
catch (IOException ex)
{
System.out.println("IOException: Connection has been lost and the handshake message was not sent!");
if(MessageService.this.messageSender.out != null)
{
try {MessageService.this.messageSender.out.close();}
catch (IOException e) {}
finally{MessageService.this.messageSender.out = null;}
}
if(MessageService.this.messageSender.mSenderSocket != null)
{
try {MessageService.this.messageSender.mSenderSocket.close();}
catch (IOException e) {}
finally{MessageService.this.messageSender.mSenderSocket = null;}
}
MessageService.this.messageSender.initConnection();
}
catch(Exception ex)
{
System.out.println("Exception: Connection has been lost and the handshake message was not sent!");
if(MessageService.this.messageSender.out != null)
{
try {MessageService.this.messageSender.out.close();}
catch (IOException e) {}
finally{MessageService.this.messageSender.out = null;}
}
if(MessageService.this.messageSender.mSenderSocket != null)
{
try {MessageService.this.messageSender.mSenderSocket.close();}
catch (IOException e) {}
finally{MessageService.this.messageSender.mSenderSocket = null;}
}
MessageService.this.messageSender.initConnection();
}
}
}
}
}
}
}
}
catch (IOException ex)
{
ex.printStackTrace();
System.out.println("IOException inside while loop of run method of thread message receiver");
inputDataArray.clear();
this.closeInputStream();
this.closeSocket();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Exception");
inputDataArray.clear();
this.closeInputStream();
this.closeSocket();
}
finally
{
try {Thread.sleep(200);}
catch (InterruptedException ex)
{
Thread.currentThread().interrupt();
break;
}
}
} //end of while(true)
System.out.println("Outside while loop");
//shutDownServer = true;
}
}