Hello,
In the following programs, I am trying to add multiple clients. The Client program remains unchanged. For the Server program, I am using a thread (custom class implements the Runnable interface) to open a new socket and process every client request. My questions follow the programs.
ChatServer.java:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChatServer implements Runnable
{
private Socket socket = null;
private ServerSocket server = null;
private Thread thread;
private int ID = 1;
public ChatServer(int port)
{
try
{
System.out.println("Binding to port " + port + ", please wait ...");
server = new ServerSocket(port);
}
catch(IOException ioe)
{ System.out.println(ioe);
}
thread = new Thread (this);
thread.start();
}
public void run ()
{
System.out.println("Server started: " + server);
System.out.println("Waiting for a client ...");
boolean flag = true;
while (flag == true)
{
try
{
System.out.println ("I am here");
ServerThread st = new ServerThread (server.accept());
System.out.println("Client#" + ID + " accepted ");
st.open();
st.start();
ID++;
}
catch (IOException e)
{}
}
}
public static void main(String args[])
{
ChatServer server = new ChatServer(2000);
}
class ServerThread extends JFrame implements Runnable
{
private BufferedReader reader = null;
private PrintWriter writer = null;
private BufferedReader console = null;
private JTextField field;
private JTextArea area;
private JButton button;
private JScrollPane pane;
private Socket socket = null;
public ServerThread (Socket s)
{
super("Server");
socket = s;
setLayout (new FlowLayout());
field = new JTextField (20);
area = new JTextArea (20, 20);
button = new JButton ("Send to Client");
area.setLineWrap(true);
pane = new JScrollPane(area);
add(area);
add(field);
add (button);
addWindowListener(
new WindowAdapter ()
{
public void windowClosing (WindowEvent e)
{
System.exit(0);
}
}
);
button.addActionListener (
new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
writer.println (field.getText());
area.append ("SERVER : " + field.getText());
field.setText("");
}
}
);
setSize (500, 500);
setVisible (true);
}
public void run()
{
boolean flag = true;
while (flag == true)
{
try
{
String line = reader.readLine();
area.append ("CLIENT : " + line + "\n");
}
catch (Exception e)
{}
}
}
public void open() throws IOException
{
reader = new BufferedReader (new InputStreamReader (socket.getInputStream()));
console = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter (socket.getOutputStream(), true);
}
}
}
ChatClient.java
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChatClient extends JFrame implements Runnable
{
private Socket socket = null;
private BufferedReader console = null;
private BufferedReader reader = null;
private PrintWriter writer = null;
private Thread thread;
private JTextField field;
private JTextArea area;
private JButton button;
private JScrollPane pane;
public ChatClient(String serverName, int serverPort)
{
super("Client");
setLayout (new FlowLayout());
field = new JTextField (20);
area = new JTextArea (20, 20);
button = new JButton ("Send to Server");
area.setLineWrap(true);
pane = new JScrollPane(area);
add(area);
add(field);
add (button);
addWindowListener(
new WindowAdapter ()
{
public void windowClosing (WindowEvent e)
{
System.exit(0);
}
}
);
button.addActionListener (
new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
writer.println (field.getText());
area.append ("CLIENT : " + field.getText() +"\n");
field.setText("");
}
}
);
System.out.println("Establishing connection. Please wait ...");
try
{ socket = new Socket(serverName, serverPort);
System.out.println("Connected: " + socket);
start();
}
catch(UnknownHostException uhe)
{ System.out.println("Host unknown: ");
}
catch(IOException ioe)
{ System.out.println("Unexpected exception: ");
}
thread = new Thread (this);
thread.start();
setSize (500, 500);
setVisible (true);
}
public void run ()
{
boolean flag = true;
while (flag == true)
{
try
{
String line = reader.readLine();
area.append ("SERVER : " + line + "\n");
}
catch (Exception e)
{}
}
}
public void start() throws IOException
{
reader = new BufferedReader (new InputStreamReader (socket.getInputStream()));
console = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter (socket.getOutputStream(), true);
}
public static void main(String args[])
{
ChatClient client = new ChatClient("localhost", 2000);
}
}
-----------------------------------------------------------------
Questions:
a) In ChatServer.java, I get a syntax error when I try to start the thread that is implemented by the ServerThread class using the start method (to call run()):
while (flag == true)
{
try
{
System.out.println ("I am here");
ServerThread st = new ServerThread (server.accept());
System.out.println("Client#" + ID + " accepted ");
st.open();
st.start(); //error on this line
ID++;
}
catch (IOException e)
{}
}
What I am doing wrong here?
b) The major issue I am having is that when I start the second client, the console of the Server goes blank and it does not show the line, "Client#2 accepted". In this state, only communication between the Server and the first Client works. The second Client is not able to communicate at all. I would be grateful for any help/advise.
Thank you!!