Results 1 to 3 of 3
- 02-09-2010, 05:27 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 19
- Rep Power
- 0
Reading heavy data on a particular port
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());
}
}
- 02-09-2010, 08:19 PM #2
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
Brother your code is a big mess :p
Just to get you going this is what you add to read some input
TEST:Java Code:... while ((m=r.read()) != 0) { //THIS IS OUTOUT [B]System.out.println(m);[/B] w.write("OK\n"); w.flush(); } ... ...
When you start socket server class
start Command Prompt and run this command :
When new windows pops outJava Code:[B]telnet localhost 9911[/B]
use keyboard to write something
You will see in your console in your IDE result
Take your time with this and work on your code style.
Find some good book on java networking and run some basic
socket examples (single first, multithread later), then do go on with this stuff
good luck!
- 02-10-2010, 05:40 AM #3
Senior Member
- Join Date
- Mar 2009
- Location
- USA
- Posts
- 127
- Rep Power
- 0
Similar Threads
-
reading data from a serial port?
By yam2 in forum Advanced JavaReplies: 2Last Post: 01-26-2011, 06:34 AM -
Reading from Serial port!
By Dogge in forum New To JavaReplies: 2Last Post: 12-01-2010, 03:21 AM -
problem with reading excel sheet data reading using poi libraries
By sandeepsai17 in forum New To JavaReplies: 5Last Post: 08-21-2009, 11:03 AM -
reading from microphone port
By chintito in forum Advanced JavaReplies: 0Last Post: 01-12-2008, 04:13 AM -
Heavy and light component mixing
By Bojevnik in forum AWT / SwingReplies: 2Last Post: 08-13-2007, 11:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks