Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to implement a GUI for TCP Listener

Reply
Thread Tools

How to implement a GUI for TCP Listener

 
 
jclark101@gmail.com
Guest
Posts: n/a
 
      08-14-2005
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").ne wInstance();

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

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-15-2005
On 14 Aug 2005 13:57:36 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a 'key press listener' to a small swing GUI Jeremy Watts Java 1 02-02-2009 03:31 PM
Flash listener + JavaScript GUI kidalex Javascript 3 09-09-2006 10:30 PM
implement soap listener Sai ASP .Net 4 04-25-2006 02:28 PM
.net remoting and tcp listener socket Danish Ahmed ASP .Net 2 04-21-2005 11:02 AM
TCP Listener on Windows XP Colin Robbins Perl Misc 4 10-09-2003 05:34 PM



Advertisments