Dear All,
I have managed to created my code which can read data on a particular port from my gps devices. Below is my code. I would like to get feedback on whether this method is correct or not? Now I am confuse which method to use read or readline for the data. Another question is that I will be receiving data consistently and very frequently from many devices(>5000) at short intervals. So will this method be enough to handle a heavy traffic or the port will be saturated. So another thing once I read the data I want to insert into a database what is the best method open one connection or keep opening and closing connections? Thank you.
import java.io.;
import java.net.;
import java.util.;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.sql.;
public class t3 implements Runnable {
private Socket con = null;
private String port = null;
static Connection dbconn = null;
public static void main(String[] args) {
try {
final ServerSocket s3 = new ServerSocket(9911);
new Thread()
{
@Override
public void run()
{
while (true) {
try {
Socket c3 = null;
c3 = s3.accept();
t3 v3 = new t3(c3,"9911");
Thread t3 = new Thread(v3);
t3.start();
}catch (Exception e){System.out.println(e.toString());}
}
}
}.start();
} catch (Exception e) {
System.out.println(e.toString());
}
}
public t3(Socket c, String clientPort){
con = c;
port = clientPort;
}
@Override
public void run() {
try {
PrintStream out = System.out;
BufferedWriter fout = null;
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
BufferedReader r = new BufferedReader(new InputStreamReader(con.getInputStream()));
String deviceID,chksum;
int m = 0, count=0;
String line="",ipAdd="",ipPort="";
String n="";
while ((m=r.read()) != 0)
{
w.write("OK\n");
w.flush();
}
}
} catch (Exception e)
{
System.out.println(e.toString());
System.out.println("" + pollCount);
}
}
private static void printSocketInfo(Socket s) {
System.out.println("Remote address = "
+s.getInetAddress().toString());
System.out.println("Remote port = "
+s.getPort());
System.out.println("Local socket address = "
+s.getLocalSocketAddress().toString());
System.out.println("Local address = "
+s.getLocalAddress().toString());
System.out.println("Local port = "
+s.getLocalPort());
}
private static void printServerSocketInfo(ServerSocket s) {
System.out.println("Server socket address = "
+s.getInetAddress().toString());
System.out.println("Server socket port = "
+s.getLocalPort());
}
}