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); 
} 
} 
} 
}

Could you repost your code with proper formatting and without the blockquote stuff?

Here is my codes edited version and indented. I suspect can it be due to the location where I send my acknowledgement to the device is it? Any idea really lost on this problem?

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); 
      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); 
       } 
      }
    }
    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; 
} 

public void run() 
{ 
  BufferedWriter w = null; 
  BufferedReader r = null; 
  try 
  {
    PrintStream out = System.out; 
    BufferedWriter fout = null; 
    w = new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream  ())); 
    r = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream())); 
    int m = 0, count=0; 
    String n="";
    w.write("$PA\n");
    w.flush();
    while ((line = r.readLine()) != null) 
    { 
     // Print the content on the console 
     System.out.println (line); 
    } 
    /*
    while ((m=r.read()) != -1) 
    {
     String n=""; 
     w.write("$PA\n"); 
     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=""; 
   } 
  }*/
    
 } 
 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);
    }
  }
 }
}

Not sure how to solve this.
One way that comes to mind is to write a client that connects on socket 9000 so you can see both sides of the communication.

What kind of device is the GPS that connects to socket 9000?
I wrote some code to talk to several types of GPSs using the serial port.

1/ that not good idea to wrap another try - catch - finally block into another one

Dear NormR1,
I guess is due to some acknowledgment sending problem what is your idea? Yes I have tried via a local telnet connection and it successfully receive the acknowledgement. This is a custom made gps device.

My idea for a client was for a program to connect to your commServer program. I've seen problems with some of the Java classes for I/O and have seen that some work better than others. Perhaps try a lower level class vs a Buffered class.

Dear NormR1,
So you idea is build a simple client program to connect to this server is it? What are the problem have you seen so far? Do you see problem when server sending back the acknowledgement do you find those in my codes? I do know what is lower level class? Any suggestion?

I don't remember what the problem was. By lower level class I mean OutputStreamWriter or OutputStream.
With a test program as client you could see if there is any difference when you use each one of these three classes (the Buffered one you have now and these two).

Dear NormR1,
So just replace the buffered write with OutputStreamWriter or both the buffer read and write?

Try as many different combinations as you can and see what happens.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.