aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Connection is failing at ObjectInputStream for Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Connection is failing at ObjectInputStream for Server" Watch "Connection is failing at ObjectInputStream for Server" New topic

Connection is failing at ObjectInputStream for Server

Matthew Seibel

Joined: Jan 12, 2006
Posts: 4
This server fails to get passed line 181 (ObjectInputStream ois = new ObjectInputStream(client.getInputStream()) within my Connection class thread. I am not seeing why the creation of this ObjectInputStream is causing me problems.

import java.util.*;

public class Server2 {

Map services; //Used to hold Multiple Listeners
Set connections; //Used to hold multiple connections
int maxConnections; //# Max Connections
//Used to set mass prirority on all Listener threads. If the server is to listen on more than one port then all the Listener's threads will be in this group.
ThreadGroup threadGroup;

/*This constructor for the server will create a thread group for listener threads, set the max number of connections, create the map queue for Listener threads, and create the queue for the connections*/
public Server2(int maxConnections) {
threadGroup = new ThreadGroup(Server2.class.getName());
this.maxConnections = maxConnections;
services = new HashMap( );
connections = new HashSet(maxConnections);

//This function creates a listener to bind to the port provided
public synchronized void addService(int port) throws IOException {
Integer key = new Integer(port); // the hashtable key
// Check whether a service is already on that port
if (services.get(key) != null)
throw new IllegalArgumentException("Port " + port + " already in use.");
// Create a Listener object to listen for connections on the port
Listener listener = new Listener(threadGroup, port); /*The Server is created to listen on the provided port*/
// Store it in the hashtable
services.put(key, listener);
// Start the listener running.
listener.start( );

* This is the method that Listener objects call when they accept a
* connection from a client. It either creates a Connection object
* for the connection and adds it to the list of current connections,
* or, if the limit on connections has been reached, it closes the
* connection.
protected synchronized void addConnection(Socket s) {
// If the connection limit has been reached
if (connections.size( ) >= maxConnections) {
try {
// Then tell the client it is being rejected.
PrintWriter out = new PrintWriter(s.getOutputStream( ));
out.print("Connection refused; " + "the server is busy; please try again later.\r\n");
out.flush( );
// And close the connection to the rejected client.
s.close( );
} catch (IOException e) {}
else {
// Otherwise, if the limit has not been reached
// Create a Connection thread to handle this connection
Connection c = new Connection(s);
// Add it to the list of current connections
// And start the Connection thread to provide the service
c.start( );

* A Connection thread calls this method just before it exits. It removes
* the specified Connection from the set of connections.
protected synchronized void endConnection(Connection c) {
connections.remove(c);//Remove connection object from connection queue
System.out.println("Connection to " + c.client.getInetAddress( ).getHostAddress( ) + ":" + c.client.getPort( ) + " closed.");

public static void main(String[] args) {

//if port is specified then start the listener
if(args.length > 0) {
try {
int port = Integer.parseInt(args[0]);
Server2 server = new Server2(Integer.parseInt(args[0]));
server.addService(port); //Calling function to create Listener
catch(IOException e) {
else {
System.out.println("java Server2 <port number>");

public class Listener extends Thread {
ServerSocket listen_socket; // The socket to listen for connections
int port; // The port we're listening on
volatile boolean stop = false; // Whether we've been asked to stop

* The Listener constructor creates a thread for itself in the
* threadgroup. It creates a ServerSocket to listen for connections
* on the specified port. It arranges for the ServerSocket to be
* interruptible, so that services can be removed from the server.
public Listener(ThreadGroup group, int port) throws IOException {
super(group, "Listener:" + port);
listen_socket = new ServerSocket(port);
// give it a non-zero timeout so accept( ) can be interrupted
this.port = port;
System.out.println("Server " + listen_socket);

* This is the polite way to get a Listener to stop accepting
* connections
public void pleaseStop( ) {
this.stop = true; // Set the stop flag
this.interrupt( ); // Stop blocking in accept( )
try { listen_socket.close( ); } // Stop listening.
catch(IOException e) { }

* A Listener is a Thread, and this is its body.
* Wait for connection requests, accept them, and pass the socket on
* to the addConnection method of the server.
public void run( ) {
while(!stop) { // loop until we're asked to stop.
try {
Socket client = listen_socket.accept( );
catch (InterruptedIOException e) {}
catch (IOException e) {}

* This class is a subclass of Thread that handles an individual
* connection between a client and a Service provided by this server.
* Because each such connection has a thread of its own, each Service can
* have multiple connections pending at once. Despite all the other
* threads in use, this is the key feature that makes this a
* multi-threaded server implementation.
public class Connection extends Thread {
Socket client;

* This constructor just saves some state and calls the superclass
* constructor to create a thread to handle the connection. Connection
* objects are created by Listener threads. These threads are part of
* the server's ThreadGroup, so all Connection threads are part of that
* group, too.
public Connection(Socket client) {
super("Server.Connection:" + client.getInetAddress( ).getHostAddress( ) + ":" + client.getPort( ));
this.client = client;


public void run( ) {
try {
System.out.println("Client " + client);
ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
System.out.println("Object Input Stream Created");
catch (IOException e) {
finally {endConnection(this);}



Help would be much appericated.

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24187

Are you going to show us the printouts and the stack trace from the exception?

[Jess in Action][AskingGoodQuestions]
Matthew Seibel

Joined: Jan 12, 2006
Posts: 4
Problem was resolved. The client did not have a ObjectOutputStream created for its socket. ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream);
I agree. Here's the link:
subject: Connection is failing at ObjectInputStream for Server