Results 1 to 3 of 3
  1. #1
    newbie14 is offline Member
    Join Date
    Feb 2010
    Posts
    63
    Rep Power
    0

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

    }

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Brother your code is a big mess :p

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


    Java Code:
     ...
    			while ((m=r.read()) != 0) 
    			{
    				//THIS IS OUTOUT
    				[B]System.out.println(m);[/B]
    				
    				w.write("OK\n");
    				w.flush();
    			}
    
    ...
    ...
    TEST:
    When you start socket server class
    start Command Prompt and run this command :

    Java Code:
    [B]telnet localhost 9911[/B]
    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!

  3. #3
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

Similar Threads

  1. reading data from a serial port?
    By yam2 in forum Advanced Java
    Replies: 2
    Last Post: 01-26-2011, 06:34 AM
  2. Reading from Serial port!
    By Dogge in forum New To Java
    Replies: 2
    Last Post: 12-01-2010, 03:21 AM
  3. Replies: 5
    Last Post: 08-21-2009, 11:03 AM
  4. reading from microphone port
    By chintito in forum Advanced Java
    Replies: 0
    Last Post: 01-12-2008, 04:13 AM
  5. Heavy and light component mixing
    By Bojevnik in forum AWT / Swing
    Replies: 2
    Last Post: 08-13-2007, 11:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •