Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2010, 06:27 PM
Member
 
Join Date: Feb 2010
Posts: 1
Rep Power: 0
newbie14 is on a distinguished road
Default 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());
}

}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-09-2010, 09:19 PM
FON FON is offline
Senior Member
 
Join Date: Dec 2009
Location: Belgrade, Serbia
Posts: 289
Rep Power: 1
FON is on a distinguished road
Default
Brother your code is a big mess

Just to get you going this is what you add to read some input


Code:
 ...
			while ((m=r.read()) != 0) 
			{
				//THIS IS OUTOUT
				System.out.println(m);
				
				w.write("OK\n");
				w.flush();
			}

...
...
TEST:
When you start socket server class
start Command Prompt and run this command :

Code:
telnet localhost 9911
When new windows pops out
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!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-10-2010, 06:40 AM
Senior Member
 
Join Date: Mar 2009
Location: USA
Posts: 117
Rep Power: 0
Aseem is on a distinguished road
Default
good suggestion FON.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with reading excel sheet data reading using poi libraries sandeepsai17 New To Java 5 08-21-2009 12:03 PM
reading data from a serial port? yam2 Advanced Java 1 10-17-2008 03:52 PM
Reading from Serial port! Dogge New To Java 0 02-10-2008 05:16 PM
reading from microphone port chintito Advanced Java 0 01-12-2008 05:13 AM
Heavy and light component mixing Bojevnik AWT / Swing 2 08-13-2007 12:23 PM


All times are GMT +2. The time now is 12:49 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org