J
Jason Teagle
If a client Socket makes a connection to a ServerSocket, and then
disconnects from it, how can code from the ServerSocket side detect that
disconnection?
Similarly, if the ServerSocket closed down the client's connection, how can
code from the client side detect that disconnection?
I had some code that was failing, so I generated a new crude test
application. In the server side, I call isClosed() on the server socket
every 10th of a second, to see if it is still open. Sure enough, when I
disconnect the client side deliberately, the server side continues to claim
that it is connected.
Just in case it was my code that was naff even in the test app, I used
breakpoints to make sure it was getting to the check and never to the
disconnect detection.
--
Jason Teagle
(e-mail address removed)
The demo is reproduced below, if anyone is interested (click "Start server",
then "Connect client", then "Disconnect client" - the console should say "It
was closed." if it detects the disconnection):
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Timer;
import java.util.TimerTask;
/**
* @author Jason Teagle
*
*
*/
public class JServerSocket extends ServerSocket
{
private Timer m_timer ;
private TimerTask m_task ;
public JServerSocket(int portNumber) throws IOException
{
super(portNumber, 5);
try
{
m_task = new TimerTask()
{
public void run()
{
checkStatus();
}
};
m_timer = new Timer();
m_timer.schedule(m_task, 100, 100);
}
catch(Exception e)
{
System.out.println("Couldn't create server socket.");
}
}
private void checkStatus()
{
if (isClosed() )
System.out.println("It was closed.");
}
}
-----------------------------------------------------------
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
/**
* @author Jason Teagle
*
*
*/
public class JClientSocket extends Socket
{
public JClientSocket(int portNumber)
{
super();
SocketAddress addr ;
addr = new InetSocketAddress("127.0.0.1", portNumber);
try
{
connect(addr, 1000);
}
catch(Exception e)
{
System.out.println("Failed to connect.");
}
}
public void disconnect()
{
try
{
close();
}
catch(Exception e)
{
}
}
}
-----------------------------------------------------------
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* @author Jason Teagle
*
*
*/
public class JMainFrame extends JFrame
{
public static void main(String[] args)
{
JMainFrame mainFrame = new JMainFrame();
mainFrame.setVisible(true);
mainFrame.pack();
}
private JServerSocket m_server ;
private JClientSocket m_client ;
public JMainFrame()
{
super("TestSocketDisconnect");
Container contentPane ;
JButton button ;
m_server = null ;
m_client = null ;
contentPane = getContentPane();
contentPane.setLayout(new GridLayout(3, 1) );
button = new JButton("Start server");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
startServer();
}
}
);
contentPane.add(button);
button = new JButton("Connect client");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
connectClient();
}
}
);
contentPane.add(button);
button = new JButton("Dicsonnect client");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
disconnectClient();
}
}
);
contentPane.add(button);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
private void startServer()
{
if (m_server == null)
{
try
{
m_server = new JServerSocket(1234);
}
catch(Exception e)
{
System.out.println("Unable to construct server socket.");
}
}
}
private void connectClient()
{
if (m_client == null)
m_client = new JClientSocket(1234);
}
private void disconnectClient()
{
if (m_client != null)
{
m_client.disconnect();
m_client = null ;
}
}
}
disconnects from it, how can code from the ServerSocket side detect that
disconnection?
Similarly, if the ServerSocket closed down the client's connection, how can
code from the client side detect that disconnection?
I had some code that was failing, so I generated a new crude test
application. In the server side, I call isClosed() on the server socket
every 10th of a second, to see if it is still open. Sure enough, when I
disconnect the client side deliberately, the server side continues to claim
that it is connected.
Just in case it was my code that was naff even in the test app, I used
breakpoints to make sure it was getting to the check and never to the
disconnect detection.
--
Jason Teagle
(e-mail address removed)
The demo is reproduced below, if anyone is interested (click "Start server",
then "Connect client", then "Disconnect client" - the console should say "It
was closed." if it detects the disconnection):
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Timer;
import java.util.TimerTask;
/**
* @author Jason Teagle
*
*
*/
public class JServerSocket extends ServerSocket
{
private Timer m_timer ;
private TimerTask m_task ;
public JServerSocket(int portNumber) throws IOException
{
super(portNumber, 5);
try
{
m_task = new TimerTask()
{
public void run()
{
checkStatus();
}
};
m_timer = new Timer();
m_timer.schedule(m_task, 100, 100);
}
catch(Exception e)
{
System.out.println("Couldn't create server socket.");
}
}
private void checkStatus()
{
if (isClosed() )
System.out.println("It was closed.");
}
}
-----------------------------------------------------------
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
/**
* @author Jason Teagle
*
*
*/
public class JClientSocket extends Socket
{
public JClientSocket(int portNumber)
{
super();
SocketAddress addr ;
addr = new InetSocketAddress("127.0.0.1", portNumber);
try
{
connect(addr, 1000);
}
catch(Exception e)
{
System.out.println("Failed to connect.");
}
}
public void disconnect()
{
try
{
close();
}
catch(Exception e)
{
}
}
}
-----------------------------------------------------------
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* @author Jason Teagle
*
*
*/
public class JMainFrame extends JFrame
{
public static void main(String[] args)
{
JMainFrame mainFrame = new JMainFrame();
mainFrame.setVisible(true);
mainFrame.pack();
}
private JServerSocket m_server ;
private JClientSocket m_client ;
public JMainFrame()
{
super("TestSocketDisconnect");
Container contentPane ;
JButton button ;
m_server = null ;
m_client = null ;
contentPane = getContentPane();
contentPane.setLayout(new GridLayout(3, 1) );
button = new JButton("Start server");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
startServer();
}
}
);
contentPane.add(button);
button = new JButton("Connect client");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
connectClient();
}
}
);
contentPane.add(button);
button = new JButton("Dicsonnect client");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
disconnectClient();
}
}
);
contentPane.add(button);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
private void startServer()
{
if (m_server == null)
{
try
{
m_server = new JServerSocket(1234);
}
catch(Exception e)
{
System.out.println("Unable to construct server socket.");
}
}
}
private void connectClient()
{
if (m_client == null)
m_client = new JClientSocket(1234);
}
private void disconnectClient()
{
if (m_client != null)
{
m_client.disconnect();
m_client = null ;
}
}
}