3 Replies - 12354 Views - Last Post: 02 December 2008 - 01:30 AM Rate Topic: -----

#1 kirru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 08

chat room code

Post icon  Posted 01 December 2008 - 02:02 PM

i want this code to be edited and get an output that looks completely diffferent from the current output.

this is the client side code [/color]

import java.io.*;
import java.net.*;

/* 
Client Side
 */
public class Client 
{

	public static void main(String args[]) throws Exception 
	{
//Client socket to localhost at port 15219
	  Socket ClientSocket = new Socket("localhost", 15219);
		
	  BufferedReader br = new BufferedReader(new InputStreamReader(System.in) );

	
	  PrintStream ToServer = new PrintStream(ClientSocket.getOutputStream());
	  BufferedReader FromServer = new BufferedReader(	new InputStreamReader(ClientSocket.getInputStream()));

	  System.out.println("My Chat Room Client. Version One");			
	
	  String msg = "";
			
		 
			   while( !msg.equals("logout") ) {
		
				msg = br.readLine();
//Reads only 4 messages login, newuser, send and logout				
			   if (msg.startsWith("login ")|| msg.startsWith("newuser ") || msg.startsWith("send ")|| msg.startsWith("logout")) 
			   {
					
					ToServer.println(msg + " ");
					System.out.println(FromServer.readLine());
				}
				
				else
				{
//Else print error message
					System.out.println("Please try one of the four valid commands: login, newuser, send or logout");
				}	  

			}
//Close ClientSocket			
				ClientSocket.close();
	
	}
}





server side code is
[u][color=#FF0000]


import java.io.*;
import java.net.*;


//Server Side
public class Server 
{

	//Function for adding a new user in the file.. returns false if user is already present else true
	
	public boolean addUser(String userName, String password) throws Exception {

				FileReader fReader = new FileReader("user.txt");
			BufferedReader bReader = new BufferedReader(fReader);
			String line = null;
	
			while ((line = bReader.readLine()) != null) {

				if (userName.equals(line.substring(0, line.indexOf(' ')))) {
					return false;
				}
			}
	
				bReader.close();
				fReader.close();
				
				
			BufferedWriter fWriter = new BufferedWriter ( new FileWriter(new File("user.txt"), true) );
			
			fWriter.write( "\n" + userName + " " + password);


				fWriter.close();
				
		
	
		return true;

	}

		//Function to check if a user is present in the file or not . Returns true if user is found else false
	public boolean checkUser(String userName, String password) throws Exception {



		 FileReader freader =  new FileReader("user.txt");


		if (password == null)
			return false;

		 	BufferedReader bReader = new BufferedReader(freader);
			String line;
			
			while ((line = bReader.readLine()) != null) {
			
				if (userName.equals(line.substring(0, line.indexOf(' ')))) {
			
					if (password.equals(line.substring(line.indexOf(' ') + 1))) 
					{
						
					 freader.close();
					 bReader.close();
					 return true;
					} else 						return false;
				}
			}
		
				freader.close();
				bReader.close();
			
		
	
		return false;
	}

//Main method	
	public static void main(String args[]) throws Exception {

		
		boolean logoutFlag = false;
		String messg = null;				String clientmsg = null;		String userName = null;
		
		//Server object created..
		
		Server server1 = new Server();
		System.out.println("My Chat Room Server. Version One");

		
			//Serversocket listens at port 15219
			ServerSocket serverSocket = new ServerSocket(15219);
			//Infinite loop for waiting for next users if one logs off.
			while(true)
			{

				//Accepts user
				Socket socket = serverSocket.accept();

				
				BufferedReader readFromSocket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				PrintStream writeToSocket = new PrintStream(socket.getOutputStream());

				do{

				
					messg = readFromSocket.readLine();
					clientmsg = messg.substring(0, messg.indexOf(' '));	  

				//Nested if else for 4 commands
				//login
					
					if (clientmsg.equals("login")) 
					{

										
										String password = null;
				
										
										if (userName == null) {
				
										
											messg = messg.substring(messg.indexOf(' ') + 1);
											userName = messg.substring(0, messg.indexOf(' '));
										
											if (!(messg.indexOf(' ') == messg.lastIndexOf(' ')))
												password = messg.substring(messg.indexOf(' ') + 1,	(messg.length() - 1));
										
											if (server1.checkUser(userName, password)) {
				
										
												writeToSocket.println("Server :" + userName	+ " joins\n" + userName + " Login");
												System.out.println(userName + " login");
												
											}
										
											else {
										
													 writeToSocket.println("Invalid Login");
													 userName = null;
											}
										}
										
										else {
										
											writeToSocket.println("Denied. Already Logged in as "	+ userName+ " Please Logout first.");
												}

					}
					
			//newuser		
					else if (clientmsg.equals("newuser")) {

										
												String password = null;
												
												if (userName == null) {
													
													messg = messg.substring(messg.indexOf(' ') + 1);
													userName = messg.substring(0, messg.indexOf(' '));
													if (!(messg.indexOf(' ') == messg.lastIndexOf(' ')))
														password = messg.substring(messg.indexOf(' ') + 1,(messg.length() - 1));
										
																	if (userName.length() > 32 || password == null	|| password.length() < 4|| password.length() > 8) {
														writeToSocket.println("Server: Username is greater than 32 char  "+ "or password is < 4 char or > 8 characters.");
													}
													else if (server1.addUser(userName,password)) {
														
														writeToSocket.println("New User Created.Server :"+ userName + " joins");
														System.out.println(userName +  " Login.");
														
													}
													else {
														
														writeToSocket.println(" User already exists. Please try a different name.");
														userName = null;
													}
												}
												else {
													writeToSocket.println(userName+ " already logged in.");
													  }
												
						}
						
						
						//send
					else if (clientmsg.equals("send")) {
												
												if (userName == null) {
													writeToSocket.println("Denied. Please Login First");
												}
												else {
													messg = messg		.substring(messg.indexOf(' ') + 1);
													writeToSocket.println(userName + ":" + messg);
													System.out.println(userName + ":" + messg);
													
												}
					}
					
					
					//logout
					else if (clientmsg.equals("logout")) {
												
												if (userName == null) {
													writeToSocket.println("Denied. Please Login First");
												}
												else {
													writeToSocket.println("Server :" + userName	+ " left");
													System.out.println(userName + " left");
													
													logoutFlag = true;
												}
					}
					
				} while (!logoutFlag);
				userName = null;
				logoutFlag = false;
				socket.close();

			} 
	}
}







and i want to manipulate this code such that multiplle logins exist and list the people who are already in the chat room..

Is This A Good Question/Topic? 0
  • +

Replies To: chat room code

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: chat room code

Posted 01 December 2008 - 07:42 PM

Don't really understand your question
Was This Post Helpful? 0
  • +
  • -

#3 kirru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 08

Re: chat room code

Posted 01 December 2008 - 10:41 PM

I need to design a chat room that includes a client and a server.which should use login, userid, send a message, logout. i have the code for the chat room, but i need to implement multiple logins .i need help.
Was This Post Helpful? 0
  • +
  • -

#4 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 195
  • Joined: 30-June 08

Re: chat room code

Posted 02 December 2008 - 01:30 AM

View Postkirru, on 1 Dec, 2008 - 09:41 PM, said:

I need to design a chat room that includes a client and a server.which should use login, userid, send a message, logout. i have the code for the chat room, but i need to implement multiple logins .i need help.


Since you're already using a ServerSocket, why not create a separate thread that handles all requests for a single user? Once you start the thread your main loop is free to handle more requests.


while (true) {
  Socket s = serverSocket.accept();
  ClienThread thread = new ClientThread(s);
  thread.start();
}

// ClientThread.java

import java.net.Socket;

class ClientThread extends Thread {
  private Socket socket;

  public ClientThread(Socket socket) {
    this.socket = socket;
  }

  public void run() {
    // CLIENT HANDLER CODE HERE
  }
}


Was This Post Helpful? 1

Page 1 of 1