How to implement a GUI for TCP Listener

J

jclark101

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");
}
}
}
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top