Basically what my program does is acts like a chat room. One computer runs the server which other java programs connect to and can communicate with one another. The problem is that this server I run is horribly inefficient.
I had to use 2 threads for each connection because one thread needs to continuously listen for incoming messages and the other has to send out messages for other computers. If i put them in the same thread the one that listens for messages will block the line for sending messages.
Expand|Select|Wrap|Line Numbers
- import java.net.*;
- import java.io.*;
- public class EchoServer
- {
- ServerSocket m_ServerSocket;
- private int counter=0;
- private String global_message = "first line";
- public EchoServer()
- {
- try
- {
- m_ServerSocket = new ServerSocket(8008);
- }
- catch(IOException ioe)
- {
- System.out.println("Could not create server socket at 8008. Quitting.");
- System.exit(-1);
- }
- System.out.println("Port Created on 8008");
- int id = 0;
- while(true)
- {
- try
- {
- Socket clientSocket = m_ServerSocket.accept();
- ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
- ClientListner lisThread = new ClientListner(clientSocket, id++);
- cliThread.start();
- lisThread.start();
- }
- catch(IOException ioe)
- {
- System.out.println("Exception encountered on accept. Ignoring. Stack Trace :");
- ioe.printStackTrace();
- }
- }
- }
- public static void main (String[] args)
- {
- new EchoServer();
- }
- class ClientListner extends Thread
- {
- Socket m_clientSocket;
- int m_clientID = -1;
- boolean m_bRunThread = true;
- ClientListner(Socket s, int clientID)
- {
- m_clientSocket = s;
- m_clientID = clientID;
- }
- public void run()
- {
- BufferedReader in = null;
- PrintWriter out = null;
- PrintWriter tempout = null;
- System.out.println("Accepted Client : ID - " + m_clientID + " : Address - " +
- m_clientSocket.getInetAddress().getHostName());
- try
- {
- in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream()));
- while(m_bRunThread)
- {
- String tempitoo = in.readLine();
- if (!(tempitoo==null))
- {
- global_message = tempitoo;
- }
- System.out.println("Client Says :" + global_message);
- if(global_message.equalsIgnoreCase("close"))
- {
- m_bRunThread = false;
- System.out.print("Stopping client thread for client : " + m_clientID);
- }
- else
- {}
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- // Clean up
- try
- {
- in.close();
- out.close();
- m_clientSocket.close();
- System.out.println("...Stopped");
- }
- catch(IOException ioe)
- {
- ioe.printStackTrace();
- }
- }
- }
- }
- class ClientServiceThread extends Thread
- {
- Socket m_clientSocket;
- int m_clientID = -1;
- boolean m_bRunThread = true;
- ClientServiceThread(Socket s, int clientID)
- {
- m_clientSocket = s;
- m_clientID = clientID;
- }
- public void run()
- {
- BufferedReader in = null;
- PrintWriter out = null;
- PrintWriter tempout = null;
- String clientCommand = "user: allo";
- try
- {
- out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream()));
- out.println("test");
- out.flush();
- while(true)
- {
- if (!clientCommand.equals(global_message))
- {
- clientCommand = global_message;
- out.println(global_message);
- out.flush();
- System.out.println("Message Sent");
- }
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- in.close();
- out.close();
- m_clientSocket.close();
- System.out.println("...Stopped");
- }
- catch(IOException ioe)
- {
- ioe.printStackTrace();
- }
- }
- }
- }
- }