Here is most of the original code:
package Chat;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
// The ServerSocket we'll use for accepting new connections
private ServerSocket serverSocket;
private int num_active_chats;
// A mapping from sockets to DataOutputStreams. This will
// help us avoid having to create a DataOutputStream each time
// we want to write to a stream.
private Hashtable outputStreams = new Hashtable();
// Constructor and while-accept loop all in one.
public Server( int port ) throws IOException
{
// All we have to do is listen
num_active_chats = 0;
InetAddress IPAddress = InetAddress.getLocalHost();
System.out.println("server ip: " + IPAddress);
listen( port );
}
private void listen( int port ) throws IOException
{
// Create the ServerSocket
serverSocket = new ServerSocket( port );
// Tell the world we're ready to go
System.out.println( "Listening on "+serverSocket );
// Keep accepting connections forever
while (true)
{
// Grab the next incoming connection
Socket socket = serverSocket.accept();
// Tell the world we've got it
System.out.println( "Connection from "+socket );
// Create a DataOutputStream for writing data to the
// other side
DataOutputStream data_out = new DataOutputStream( socket.getOutputStream() );
// Save this stream so we don't need to make it again
outputStreams.put( socket, data_out );
// Create a new thread for this connection, and then forget
// about it
new ServerThread( this, socket );
//update the number of clients online
num_active_chats++;
}
}
// Get an enumeration of all the OutputStreams, one for each client
// connected to us
Enumeration getOutputStreams()
{
return outputStreams.elements();
}
//Filter then broadcast Message
public void Broadcast(String message)
{
/* if (filterMessage(message) == true)
{
System.out.println("Control message filtered " + message);
}
else*/ sendToAll(message);
}
//remove control messages and logs
private boolean filterMessage(String message)
{
return false;
}
// Send a message to all clients (utility routine)
void sendToAll( String message )
{
// We synchronize on this because another thread might be
// calling removeConnection() and this would screw us up
// as we tried to walk through the list
System.out.println("broadcasting: " + message);
synchronized( outputStreams )
{
// For each client ...
for (Enumeration e = getOutputStreams(); e.hasMoreElements(); )
{
// ... get the output stream ...
DataOutputStream sentence_out = (DataOutputStream)e.nextElement();
// ... and send the message
try {
sentence_out.writeBytes( message ); //this line does not reach the client app(s)
} catch( Exception ie ) { System.out.println( ie ); }
}
}
}
// Remove a socket, and it'socket corresponding output stream, from our
// list. This is usually called by a connection thread that has
// discovered that the connectin to the client is dead.
void removeConnection( Socket somesocket )
{
// Synchronize so we don't mess up sendToAll() while it walks
// down the list of all output streamsa
synchronized( outputStreams )
{
//update the number of clients online
num_active_chats--;
// Tell the world
System.out.println( "Removing connection to "+somesocket );
// Remove it from our hashtable/list
outputStreams.remove( somesocket );
// Make sure it'socket closed
try {
somesocket.close();
} catch( IOException ie )
{
System.out.println( "Error closing "+somesocket );
ie.printStackTrace();
}
}
}
// Main routine
// Usage: java Server <port>
static public void main( String args[] ) throws Exception
{
int port = 5555;
// Create a Server object, which will automatically begin
// accepting connections.
new Server( port );
}
private void programData(String message)
{
throw new UnsupportedOperationException("Not yet implemented");
}
}
Thread class:
package Chat;
import java.io.*;
import java.net.*;
public class ServerThread extends Thread
{
// The Server that spawned us
private Server server;
// The Socket connected to our client
private Socket socket;
// Constructor.
public ServerThread( Server server, Socket socket )
{
// Save the parameters
System.out.println("Creating client thread");
this.server = server;
this.socket = socket;
// Start up the thread
start();
}
// This runs in a separate thread when start() is called in the
// constructor.
@Override
public void run()
{
try {
// Create a DataInputStream for communication; the client
// is using a DataOutputStream to write to us
//System.out.println("run method in serverthread");
BufferedReader sentence_in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//DataInputStream din = new DataInputStream( socket.getInputStream() );
// Over and over, forever ...
while (true)
{
// ... read the next message ...
String message = sentence_in.readLine();
// ... tell the world ...
//System.out.println( "Sending "+message );
// ... and have the server send it to all clients
server.Broadcast( message );
}
} catch( EOFException ie )
{
//will not be reading from files
} catch( IOException ie )
{
System.out.println("input/output error");
ie.printStackTrace();
} finally
{
// The connection is closed for one reason or another,
// so have the server dealing with it
server.removeConnection( socket );
}
}
}


Sign In
Create Account


Back to top









