servlet Socket.accept() method executes multiple times for a single connection
servlet Socket.accept() method executes multiple times for a single
connection
I am currently in the process of developing a servlet/applet
interactive website in JAVA JDK 1.1.5. At the present time I am trying
to understand how the ObjectInput/ObjectOutput streams work and that
is what the code below is built around but that is not the problem
that I am dealing with. For sometime I have had a problem with the
accept() method generating several threads for a single socket
connection. In the server portion of the code that's shown below, you
can see that I have the accept() method within a infinite while loop.
As requests come in from the outside world, the socket.accept() is
suppose to here the request, proceed down through the loop where it
generates a new thread of a ServerSide object. It's then suppose to
loop back around and wait at the socket.accept() for the next
request. Unfortunately, it goes through that line and proceeds down
through the loop where it generates yet another thread for the same
request. It does this from two to three times on any given test run
before it will pause for a NEW incoming socket request. Can somebody
please give me an explanation as to why this is happening and what if
anything, I can do to stop it?
Thank you.
P.S. I have listed the code for both the servlet and the applet below
and the output from the JAVA console.
This test was performed on a single PC running windows XP. Both the
servlet and the applet were running on the same machine. A webserver
was not used.
APPLET SIDE
/* Class Name: TestApplet.java
Purpose: This program provides an example from the Applet side
for transmitting data between a servlet and an
applet
using the ObjectStream methods.
*/
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.JPanel.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.lang.*;
public class TestApplet extends JApplet implements Serializable
{
private static final long serialVersionUID = 1;
private String ServerName_;
private Socket s;
private boolean debug_Flag;
private String host;
// CONSTRUCTOR
public TestApplet()
{}// End of CONSTRUCTOR.
public void init()
{
ServerName_ = getCodeBase().getHost();
String debug_ = getParameter("debug");
//System.out.println("Debug is turned on y/n? " +
debug_);
System.out.println(ServerName_);
System.out.println("Check point 1");
if(createSocket(ServerName_))
{
System.getSecurityManager();
System.out.println(s.getInetAddress());
System.out.println("Check point 2");
serverIO();
}
}// End of init.
// Establish a socket connection with the server.
private boolean createSocket(String pServerName)
{
boolean SocketConnectSuccessful = false;
try
{
s = new Socket(pServerName, 554);
SocketConnectSuccessful = true;
}
catch(Exception e)
{
e.printStackTrace(System.err);
System.out.println(e);
SocketConnectSuccessful = false;
}//End of catch block.
return SocketConnectSuccessful;
}//End of method createSocket.
//Perform Input/Output operations with the server.
private void serverIO()
{
Try
{
//Read data back from server - Input Stream.
ObjectInputStream inObj = new
objectInputStream(s.getInputStream());
Employee empIn = (Employee)inObj.readObject();
System.out.println(empIn.first_name);
System.out.println(empIn.last_name);
System.out.println(empIn.mi);
System.out.println(empIn.job_title);
System.out.println(empIn.phone_number);
// Thread.currentThread().sleep(10000); s.close();
}
catch(Exception e)
{
System.out.println("Exception: " + e);
}//End of try/catch block
}//End of method ServerIO
}//End of class TestApplet.
class Employee implements Serializable
{
private static final long serialVersionUID = 1;
String first_name = "";
String last_name = "";
String mi = ""; String job_title = "";
String phone_number = "";
}//End of class Employee.
SERVER SIDE
/* Class name: ServerSide
Purpose: This program provides an example from the java
servlet side for transmitting data between a
servlet and an applet using the ObjectStream
input/output methods. A counterpart will also be
provided from the Applet side.
*/
import java.io.*;
import java.util.*;
import java.net.*;
public class ServerSide extends Thread implements Serializable
{
//INSTANCE VARIABLES
private DataInputStream d;
private Socket s;
private String sTestField_;
private static final long serialVersionUID = 1;
public static void main(String[] Args)
{
try
{
int spawnCnt = 1;
ServerSocket ss = new ServerSocket(554);
while(true)
{
/*
Listen for incoming connection requests from client
programs, establish a connection, and return a Socket
object that represents this connection.
*/
System.out.println("Starting point");
Socket s = ss.accept();
System.out.println ("Accepting Connection");
System.out.println("Spawning " + spawnCnt++);
System.out.println("check point " + spawnCnt);
Thread servSide = new Thread(new ServerSide(s),
"ServerSide");
servSide.start();
for(int loopCnt = 0; loopCnt < 100000 ;++loopCnt);
System.out.println("Checkpoint after loopcnt");
}//End of while loop
}catch(IOException e)
{
e.printStackTrace(System.err);
}
}//End of main.
//Constructor.
ServerSide(Socket s_)
{
s = s_;
}//End of Constructor.
public void run()
{
//Create an object.
Employee emp = new Employee();
emp.first_name = "John";
emp.last_name = "Stevens";
emp.mi = "R";
emp.job_title = "Information Systems Tech";
emp.phone_number = "867-5309";
try
{
// Setup output Stream.
ObjectOutputStream objOut = new
ObjectOutputStream(s.getOutputStream());
objOut.flush();
objOut.writeObject(emp);
objOut.flush();
objOut.close();
s.close();
}
catch(IOException e)
{
System.out.println("Check point 1: Excep Error!!!");
e.printStackTrace(System.err);
}
}//End of run method
}//End of ServerSide class...
class Employee implements Serializable
{
private static final long serialVersionUID = 1;
String first_name = "";
String last_name = "";
String mi = "";
String job_title = "";
String phone_number = "";
}//End of class Employee.
JAVA CONSOLE OUTPUT
Java Plug-in 1.5.0_11
Using JRE version 1.5.0_11 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\sruliso
network: Loading user-defined proxy configuration ...
network: Done.
network: Loading proxy configuration from Internet Explorer ...
network: Auto config URL: http://webt1resv.ilptab.il.us:8080/array.dll?Get.Routing.Script
network: Done.
network: Loading auto proxy configuration ...
network: Downloading auto proxy file from
http://webt1resv.ilptab.il.us:8080/array.dll?Get.Routing.Script
network: Done.
network: Proxy Configuration: Automatic Proxy Configuration
URL: http://webt1resv.ilptab.il.us:8080/array.dll?Get.Routing.Script
basic: Cache is enabled
basic: Location: C:\Documents and Settings\sruliso\Application Data\Sun
\Java\Deployment\cache\javapi\v1.0
basic: Maximum size: unlimited
basic: Compression level: 0
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
p: reload proxy configuration
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
basic: Registered modality listener
liveconnect: Invoking JS method: document
liveconnect: Invoking JS method: URL
basic: Referencing classloader: sun.plugin.ClassLoaderInfo@1807ca8,
refcount=1
basic: Added progress listener: sun.plugin.util.GrayBoxPainter@1a80a69
basic: Loading applet ...
basic: Initializing applet ...
basic: Starting applet ...
basic: Registered modality listener
liveconnect: Invoking JS method: document
liveconnect: Invoking JS method: URL
basic: Stopped loading ...
basic: Stopping applet ...
basic: Removed progress listener:
sun.plugin.util.GrayBoxPainter@1a80a69
basic: Finding information ...
basic: Releasing classloader: sun.plugin.ClassLoaderInfo@1807ca8,
refcount=0
basic: Caching classloader: sun.plugin.ClassLoaderInfo@1807ca8
basic: Current classloader cache size: 1
basic: Done ...
basic: Joining applet thread ...
basic: Referencing classloader: sun.plugin.ClassLoaderInfo@1807ca8,
refcount=1
basic: Added progress listener: sun.plugin.util.GrayBoxPainter@1484a05
basic: Loading applet ...
basic: Initializing applet ...
basic: Starting applet ...
basic: Destroying applet ...
basic: Disposing applet ...
java.lang.InterruptedException
at javax.swing.JPanel.updateUI(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JRootPane.createGlassPane(Unknown Source)
at javax.swing.JRootPane.<init>(Unknown Source)
at javax.swing.JApplet.createRootPane(Unknown Source)
at javax.swing.JApplet.<init>(Unknown Source)
at TestApplet.<init>(TestApplet.java:43)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown
Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
basic: Exception: java.lang.InterruptedException
Check point 1
network: Connecting socket://localhost:554 with proxy=DIRECT
localhost/127.0.0.1
Check point 2
John
Stevens
R
Information Systems Analyst II
217-755-4141
basic: Joined applet thread ...
basic: Unregistered modality listener
basic: Stopping applet ...
basic: Removed progress listener:
sun.plugin.util.GrayBoxPainter@1484a05
basic: Finding information ...
basic: Releasing classloader: sun.plugin.ClassLoaderInfo@1807ca8,
refcount=0
basic: Caching classloader: sun.plugin.ClassLoaderInfo@1807ca8
basic: Current classloader cache size: 1
basic: Done ...
basic: Joining applet thread ...
basic: Quiting applet ...
basic: Destroying applet ...
basic: Disposing applet ...
basic: Joined applet thread ...
basic: Unregistered modality listener
basic: Quiting applet ...
basic: Registered modality listener
liveconnect: Invoking JS method: document
liveconnect: Invoking JS method: URL
basic: Referencing classloader: sun.plugin.ClassLoaderInfo@1807ca8,
refcount=1
basic: Added progress listener: sun.plugin.util.GrayBoxPainter@14e8cee
basic: Loading applet ...
basic: Initializing applet ...
basic: Starting applet ...
Check point 1
network: Connecting socket://localhost:554 with proxy=DIRECT
localhost/127.0.0.1
Check point 2
John
Stevens
R
Information Systems Analyst II
217-755-4141