Dear All,
Below is my java codes. Currently the codes gets data from gps devices. The problem now if you notice is that I have to send twice the acknowledgement message that is w.write("$PA\n"); w.flush(); once in line 56 and 57 and once more in 85 and 86 then only I will receive all the 5 strings. Currently my method of receving is the one I have commented out that is from 62 to 128. I notice that if I dont know send the second acknowledgement I only receive the single string but it suppose to be 5 strings. So I am trying to revert to readline method at line 58 but unfortunately it always wait for \n rather than null. Any help.
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Date;
import java.text.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.IOException;
public class commServer {
public static void main(String[] args) {
try {
final ServerSocket serverSocketConn = new ServerSocket(9000);
[INDENT]while (true)
{
try
{
Socket socketConn1 = serverSocketConn.accept();
new Thread(new ConnectionHandler(socketConn1)).start();
}
catch(Exception e)
{
System.out.println("MyError:Socket Accepting has been caught in main loop."+e.toString());
e.printStackTrace(System.out);
}
} [/INDENT]
}
catch (Exception e)
{
System.out.println("MyError:Socket Conn has been caught in main loop."+e.toString());
e.printStackTrace(System.out);
//System.exit(0);
}
}
}
class ConnectionHandler implements Runnable {
private Socket receivedSocketConn1;
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy");
DateFormat inDf=new SimpleDateFormat("ddMMyyHHmmss");
DateFormat outDf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ConnectionHandler(Socket receivedSocketConn1) {
this.receivedSocketConn1=receivedSocketConn1;
}
//@Override
public void run() {
BufferedWriter w = null;
BufferedReader r = null;
try {
PrintStream out = System.out;
BufferedWriter fout = null;
w = new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputSt ream()));
r = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStre am()));
int m = 0, count=0;
String line="";
String n="";
w.write("$PA\n");
w.flush();
[INDENT]while ((line = r.readLine()) != null) {
// Print the content on the console
System.out.println (line);
} [/INDENT]/*
[INDENT]while ((m=r.read()) != -1)
{
Date dateIn = new Date();
n = n + (char) m;
int i = n.indexOf("GET");
if(i != -1) {
break;
}
if (m==35)
{
String ori = n;
String noCheckSum = n.substring(0,(n.length()-4));
int addExist = n.indexOf("@");
String[] slave = null;
if(addExist!=-1)
{
slave = noCheckSum.split("@");
n = slave[0];
} else
{
n = noCheckSum;
}
String[] result = n.split(",");
w.write("$PA\n");
w.flush();
int count1 = 0;
Date date = Calendar.getInstance().getTime();
String today = formatter.format(date);
String filename= "MyDataFile"+today+".txt";
boolean append = true;
FileWriter fw = null;
try
{
fw = new FileWriter(filename,append);
fw.write(ori+" "+dateFormat.format(dateIn)+"\n");//appends the string to the file
Date dateOut = new Date();
fw.write("$PA"+" "+dateFormat.format(dateOut)+"\n");//appends the string to the file
}
catch (IOException ex)
{
//ex.printStackTrace(new PrintWriter(sWriter));
System.out.println("MyError:IOException has been caught in in the file operation"+ex.toString());
ex.printStackTrace(System.out);
}
finally
{
try
{
if ( fw != null )
{
fw.close();
}
else
{
System.out.println("MyError:fw is null in finally close");
//logger.log(Level.SEVERE, "MyError:fw is null in finally close", "");
}
}
catch(IOException ex){
System.out.println("MyError:IOException has been caught in fw is null in finally close");
ex.printStackTrace(System.out);
}
}
n="";
}
}*/ [/INDENT]
}
catch (IOException ex)
{
System.out.println("MyError:IOException has been caught in in the main first try");
ex.printStackTrace(System.out);
}
finally
{
try
{
if ( w != null )
{
w.close();
}
else
{
System.out.println("MyError:w is null in finally close");
}
}
catch(IOException ex){
System.out.println("MyError:IOException has been caught in w in finally close");
ex.printStackTrace(System.out);
}
}
}
}