How to implement a GUI for TCP Listener

Discussion in 'Java' started by jclark101@gmail.com, Aug 14, 2005.

  1. Guest

    Hello! I have built a TCP socket listener that reads ASCII data
    streams then inserts into a DB2 database. I am currently running this
    application via command line, but would like to implement a GUI
    interface to start and stop the app. I'm not quite sure where to
    begin! Do I start a new file that sets up the JTextArea, then pass the
    outputArea to the listener? Here is most of my listener code:


    import java.io.*; // for IOException, InputStreamReader, etc.
    import java.net.*; // for Socket, ServerSocket, etc.
    import java.util.Date; // for Date()
    import java.text.*; // for DateFormat & SimpleDateFormat
    import java.sql.*; // for DB2 connection

    // put network operation in its own thread
    public class Jserve_agent extends Thread {
    static String version = "1.0.1 8/9/05"; // CHANGE version HERE
    private ServerSocket listenPort; // server socket object
    static int portArg = 7100; // the port number to listen on
    static String clientHost; // the client connection
    static String logFileArg = "c:\\Jserve_agent.log"; // default log file
    static PrintWriter logFile = null; // log file object

    public static void main(String args[]) throws Exception {

    if (portArg > 0 && logFileArg == null)
    portArg = Integer.parseInt(args[0]);
    else if (portArg > 0 && logFileArg != null) {
    //portArg = Integer.parseInt(args[0]);
    //logFileArg = args[1]; // get log file arg when present

    logFile = new PrintWriter(new FileWriter(logFileArg, true), true);

    logFile.close(); // close the log file
    } else {
    System.out.println(
    "\n\t\t\tJserve_agent Version " + version + "\n\n" +
    "Usage: java [-DnoDataTO=90] Jserve_agent 7100
    C:/Data/Jserve_agent.log"
    );
    System.exit(1);
    }

    if (portArg < 1025 || portArg > 65535)
    throw new IllegalArgumentException("invalid port number");

    Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() { onTermination(); }
    });

    Jserve_agent myServer = new Jserve_agent();
    myServer.start();
    }


    public static void logIt(String message) {

    try {
    logFile = new PrintWriter(new FileWriter(logFileArg, true), true);

    } catch (IOException ioe) {

    }

    DateFormat myFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");

    if (clientHost != null) {
    System.out.println(myFormat.format(new Date()) + " - client: "
    + clientHost + ", port: " + portArg + " - " + message);

    logFile.println(myFormat.format(new Date()) + " - client: "
    + clientHost + ", port: " + portArg + " - " + message);
    } else {
    System.out.println(myFormat.format(new Date()) + " - Jserve_agent: "

    + message);

    logFile.println(myFormat.format(new Date()) + " - Jserve_agent: "
    + message);
    }
    logFile.close(); // close the log file
    }

    public Jserve_agent() {
    try {
    listenPort = new ServerSocket(portArg);
    logIt("listening on port: " + portArg);
    } catch (IOException ioe) {
    logIt("ERROR: " + ioe.getMessage());
    logIt("exiting...");
    System.exit(1);
    }
    }

    public void run() {
    Socket clientConn = null; // the client connection
    int timeout; // lack-of-data timeout (seconds)
    final String TIMEOUT = "40"; // default (seconds)

    while (true) {
    if (listenPort == null) {
    logIt("listenPort is null");
    return;
    }
    try {
    clientConn = listenPort.accept();

    clientHost = clientConn.getInetAddress().getHostName();
    logIt("client connection accepted");

    BufferedReader myRcv = new BufferedReader (
    new InputStreamReader(clientConn.getInputStream()));

    timeout = Integer.parseInt(
    System.getProperty("noDataTO", TIMEOUT));
    clientConn.setSoTimeout(timeout*1000);


    String line;
    while ( (line = myRcv.readLine() ) !=
    null )
    {
    if (! line.startsWith("=="))
    {
    Jserve_agent.j_Insertion(line);
    }
    }

    logIt("connection dropped by client");
    clientHost = null;
    logIt("listening on port: " + portArg);

    } catch (InterruptedIOException ioe) {
    logIt("IO ERROR: (cliento) " + ioe.getMessage());
    logIt("restarting...");

    } catch (IOException ioe) {
    logIt("IO ERROR: (client) " + ioe.getMessage());
    logIt("exiting...");
    System.exit(1);

    } finally {
    try {
    clientConn.close();
    } catch (IOException ioe) {
    logIt("ERROR: clientConn.close() failed - "
    + ioe.getMessage());
    logIt("exiting...");
    System.exit(1);
    }
    }
    }
    }

    public static void j_Insertion(String ob_Ag){

    //String jSQL;
    String vals = ob_Ag.replace('|',',');

    //value for agent
    int agent =
    Integer.parseInt(vals.substring(vals.lastIndexOf(',') + 1));

    //get current time in milliseconds
    Date now = new Date();
    long nowLong = now.getTime();


    try
    {

    Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();

    //instantiate a connection object
    Connection dcon = null;

    //data location
    String url = "jdbc:db2:test";
    String UID = "id";
    String PA = "word";
    dcon = DriverManager.getConnection(url, UID, PA);

    Statement chk_stmt = dcon.createStatement();
    ResultSet rs = chk_stmt.executeQuery("SELECT id,
    login, elap_time from tblagent_time where login =" + agent);

    Statement inject = dcon.createStatement();

    if (!rs.next())
    {

    inject.executeUpdate("INSERT INTO tblagent_time
    (login, elap_time, c_time ) VALUES " +
    "(" + agent + "," + nowLong + ",
    current_timestamp)");
    }
    else
    {
    //rs.first();
    long a = rs.getLong(3);
    int b = rs.getInt(1);

    //get the elapsed time
    long diff = nowLong - a;

    inject.executeUpdate("UPDATE tblagent_time SET
    elap_time = " + nowLong + " WHERE" +
    " id=" + rs.getInt(1));

    inject.executeUpdate("UPDATE tblagent_data_rt
    SET elap_time = " + diff + " WHERE" +
    " login=" + rs.getInt(2) + " AND
    elap_time is null");

    }


    inject.executeUpdate("INSERT INTO tblagent_data_rt
    (extension, agstate, agduration," +
    " destination, da_inqueue, split, onhold,
    acd, login, elap_time, c_time) VALUES (" +
    vals + ", null, current timestamp)");

    dcon.close();


    }
    catch( Exception e ) {
    System.out.println(e);
    }


    }
    public static void onTermination() {
    if(clientHost == null) {
    logIt("Jserve_agent exited, port: " + portArg);
    } else {
    logIt("Jserve_agent exited");
    }
    }
    }
     
    , Aug 14, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 14 Aug 2005 13:57:36 -0700, wrote or quoted :

    > I am currently running this
    >application via command line, but would like to implement a GUI
    >interface to start and stop the app.


    You would have some sort of Frame or JFrame that displayed what every
    data gathering fields you needed.

    When you hit GO your ActionListener spawns a thread that sets off the
    socket process.

    When you hit STOP your ActionListener taps the thread the on the
    shoulder and asks it to shut down at the earliest opportunity.
     
    Roedy Green, Aug 15, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danish Ahmed

    .net remoting and tcp listener socket

    Danish Ahmed, Apr 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    3,845
    Danish Ahmed
    Apr 21, 2005
  2. Sai

    implement soap listener

    Sai, Apr 24, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,767
  3. Jeremy Watts
    Replies:
    1
    Views:
    2,574
    John B. Matthews
    Feb 2, 2009
  4. Colin Robbins

    TCP Listener on Windows XP

    Colin Robbins, Oct 7, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    261
    Colin Robbins
    Oct 9, 2003
  5. kidalex

    Flash listener + JavaScript GUI

    kidalex, Sep 7, 2006, in forum: Javascript
    Replies:
    3
    Views:
    94
Loading...

Share This Page