write a networking program in which a Server can involve more than one clients.(File must be save as: MultiClientServer.java )
Server Part:-
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Server extends JFrame implements Runnable
{
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
private String counter = "";
Thread t;
public Server(Socket i,String c)
{
super( "Server" );
connection=i;
counter=c;
Container container = getContentPane();
enterField = new JTextField();
enterField.addActionListener(new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
sendData( event.getActionCommand() );
}
}
); // end call to addActionListener
container.add( enterField, BorderLayout.SOUTH );
displayArea = new JTextArea();
container.add( new JScrollPane( displayArea ),BorderLayout.CENTER );
setSize( 300, 150 );
setVisible( true );
}
public void run()
{
String message="";
try
{
getStreams();
processConnection();
closeConnection();
}
catch ( EOFException eofException )
{
System.out.println( "Client terminated connection" );
}
catch ( IOException ioException )
{
ioException.printStackTrace();
}
}
private void getStreams() throws IOException
{
output = new ObjectOutputStream(connection.getOutputStream() );
output.flush();
input = new ObjectInputStream(connection.getInputStream() );
displayArea.append( "\nGot I/O streams\n" );
}
private void processConnection() throws IOException
{
String message = "SERVER>>> Connection successful";
output.writeObject( message );
output.flush();
enterField.setEnabled( true );
do
{
try
{
message = ( String ) input.readObject();
displayArea.append( "\n" + message );
displayArea.setCaretPosition(
displayArea.getText().length() );
}
catch ( ClassNotFoundException classNotFoundException )
{
displayArea.append( "\nUnknown object type received" );
}
} while ( !message.equals( "CLIENT>>> TERMINATE" ) );
}
private void closeConnection() throws IOException
{
displayArea.append( "\nUser terminated connection" );
enterField.setEnabled( true );
output.close();
input.close();
connection.close();
}
private void sendData( String message )
{
try
{
output.writeObject( "SERVER>>> " + message );
output.flush();
displayArea.append( "\nSERVER>>>" + message );
}
catch ( IOException ioException )
{
displayArea.append( "\nError writing object" );
}
}
}
class MultiClientServer
{
public static void main(String[] args )
{
String st="";
Server a;
try
{
int i = 1;
ServerSocket s = new ServerSocket(5000,100);
for (;;)
{
st=st+i++;
Socket incoming = s.accept( );
a=new Server(incoming,st);
Thread t = new Thread(a);
t.start();
i++;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Client Par:-
Save as: MultiClient.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MultiClient extends JFrame
{
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String chatServer;
private Socket client;
public MultiClient( String host )
{
super( "Client" );
chatServer = host;
Container container = getContentPane();
enterField = new JTextField();
enterField.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
sendData( event.getActionCommand() );
}
} // end anonymous inner class
); // end call to addActionListener
container.add( enterField, BorderLayout.SOUTH );
displayArea = new JTextArea();
container.add( new JScrollPane( displayArea ),
BorderLayout.CENTER );
setSize( 300, 150 );
setVisible( true );
}
public void runClient()
{
try
{
connectToServer();
getStreams();
processConnection();
closeConnection();
}
catch ( EOFException eofException )
{
System.out.println( "Server terminated connection" );
}
catch ( IOException ioException )
{
ioException.printStackTrace();
}
}
private void getStreams() throws IOException
{
output = new ObjectOutputStream(client.getOutputStream() );
output.flush();
input = new ObjectInputStream(client.getInputStream() );
displayArea.append( "\nGot I/O streams\n" );
}
private void connectToServer() throws IOException
{
displayArea.setText( "Attempting connection\n" );
client = new Socket(InetAddress.getByName( chatServer ), 5000 );
displayArea.append( "Connected to: " +client.getInetAddress().getHostName() );
}
private void processConnection() throws IOException
{
enterField.setEnabled( true );
do
{
try
{
message = ( String ) input.readObject();
displayArea.append( "\n" + message );
displayArea.setCaretPosition(displayArea.getText().length() );
}
catch ( ClassNotFoundException classNotFoundException )
{
displayArea.append( "\nUnknown object type received" );
}
} while ( !message.equals( "SERVER>>> TERMINATE" ) );
} // end method process connection
private void closeConnection() throws IOException
{
displayArea.append( "\nClosing connection" );
output.close();
input.close();
client.close();
}
private void sendData( String message )
{
try
{
output.writeObject( "CLIENT>>> " + message );
output.flush();
displayArea.append( "\nCLIENT>>>" + message );
}
catch ( IOException ioException )
{
displayArea.append( "\nError writing object" );
}
}
public static void main( String args[] )
{
Client application;
if ( args.length == 0 ) application = new Client( "127.0.0.1" );
else
application = new Client( args[ 0 ] );
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
application.runClient();
}
} // end class Client