Java Chatroom menu

User Tag List

Results 1 to 1 of 1
  1. #1
    Aimladen's Avatar Active Member
    Reputation
    16
    Join Date
    Sep 2008
    Posts
    84
    Thanks G/R
    1/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Java Chatroom

    This is an old Java Chatroom script I made about a year ago, just trying to share some stuff I've done.

    --
    One person must run the ChatServer application on a given port, and then the other people will type in the host's ip address when it asks for host, and the port that is chosen by the host.

    ChatServer(Only one person needs to run it):
    Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.net.*;
    import java.util.*;
    import java.io.*;
    
    public class ChatServer
    {
    	public static ChatServer server;
    	public static int port = 2444;
    	public static ServerSocket serverSocket;
    	public static Socket temp;
    	public static ArrayList<User> users = new ArrayList<User>();
    	public ChatServer(int ePort)
    	{
    		port = ePort;
    		try{
    			serverSocket = new ServerSocket(port);
    			System.out.println("Server opened on port: " + port);
    		}catch(Exception e){
    			System.out.println("Server Port in use. Please choose another port.");
    			System.exit(1);
    		}
    	}
    	public static void main(String args[])
    	{
    		server = new ChatServer(Integer.parseInt(JOptionPane.showInputDialog("Enter the port #")));
    		Thread mainLoop = new Thread(new Runnable()
    		{
    			public void run()
    			{
    				while(true)
    				{
    					try{
    						temp = serverSocket.accept();
    					} catch(Exception e){}
    					User user = new User("nameless",temp);
    					users.add(user);
    					System.out.println("A new user has connected to the server.");
    					Thread t = new Thread(new Runnable()
    					{
    						public void run()
    						{
    							Socket temp2 = temp;
    							boolean stop = false;
    							while(!stop)
    							{
    								String msg = "poop";
    								try {
    									msg = users.get(findUser(temp2)).getIn().readLine();
    								} catch(SocketException e) { 
    									System.out.println(users.get(findUser(temp2)).getName() + " has disconnected from the server.");
    									sendAll(users.get(findUser(temp2)).getName() + " has disconnected from the server.");
    									users.remove(findUser(temp2));
    									stop = true;
    									break;
    								}
    								catch(IOException e){}
    								if(msg.startsWith("/"))
    								{
    									if(msg.startsWith("/nick"))
    									{
    										String[] MSG = msg.split(" ");
    										boolean found = false;
    										for(int x = 0; x < users.size(); x++)
    										{
    											if(users.get(x).getName().equalsIgnoreCase(MSG[1]))
    											{
    												users.get(findUser(temp2)).getOut().println("Nickname is already in use.");
    												found = true;
    											}
    										}
    										if(!found)
    										{
    											sendAll(users.get(findUser(temp2)).getName() + " is now known as " + MSG[1]);
    											users.get(findUser(temp2)).changeNick(MSG[1]);
    										}
    									}
    								}
    								else
    								{
    									sendAll(msg,users.get(findUser(temp2)).getName());
    								}
    							}
    						}
    					});
    					t.start();
    				}
    			}
    		});
    		mainLoop.start();
    	}
    
    	public static int findUser(Socket temp)
    	{
    		for(int x = 0; x < users.size(); x++)
    		{
    			if(users.get(x).getSocket() == temp)
    			{
    				return x;
    			}
    		}
    		return -1;
    	}
    
    
    	public static int findUserByName(String name)
    	{
    		for(int x = 0; x < users.size(); x++)
    		{
    			if(users.get(x).getName().equalsIgnoreCase(name))
    			{
    				return x;
    			}
    		}
    		return -1;
    	}
    
    	public static void sendAll(String msg, String name)
    	{
    		for(int x = 0; x < users.size(); x++)
    		{
    			users.get(x).getOut().println(name + ":" + msg);
    		}
    	}
    
    	public static void sendAll(String msg)
    	{
    		for(int x = 0; x < users.size(); x++)
    		{
    			users.get(x).getOut().println(msg);
    		}
    	}
    }
    
    class User
    {
    	public String username;
    	public PrintWriter out;
    	public BufferedReader in;
    	public Socket userSocket;
    	public User(String Eusername, Socket EuserSocket)
    	{
    		username = Eusername;
    		userSocket = EuserSocket;
    		try {
    			out = new PrintWriter(userSocket.getOutputStream(), true);
    			in = new BufferedReader(new InputStreamReader(userSocket.getInputStream()));
    		} catch(Exception e){}
    	}
    
    	public PrintWriter getOut()
    	{
    		return out;
    	}
    
    	public BufferedReader getIn()
    	{
    		return in;
    	}
    
    	public String getName()
    	{
    		return username;
    	}
    
    	public Socket getSocket()
    	{
    		return userSocket;
    	}
    
    	public void changeNick(String newstring)
    	{
    		username = newstring;
    	}
    }
    ChatClient(Needed to connect to the server):
    Code:
    import javax.swing.*;
    import java.net.*;
    import java.util.*;
    import java.io.*;
    import java.lang.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ChatClient implements ActionListener
    {
    	public int port;
    	public String host;
    	public String msg;
    	public PrintWriter out;
    	public BufferedReader in;
    	public JTextArea messages;
    	public JPanel displayPanel, sendPanel;
    	public JFrame main;
    	public JScrollPane messagePane;
    	public JTextField sendM;
    	public JButton send;
    
    	public static void main(String args[]) { ChatClient k = new ChatClient(); }
    	public ChatClient()
    	{
    		try {
    			host = JOptionPane.showInputDialog("Enter the host name of the server.");
    			port = Integer.parseInt(JOptionPane.showInputDialog("Enter the port #"));
    			Socket connection = new Socket(host, port);
    			System.out.println("Now connected to " + host + ".");
    			out = new PrintWriter(connection.getOutputStream(), true);
    			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    			Thread t = new Thread(new Runnable()
    			{
    				public void run()
    				{
    					try{
    						displayMsg("You are now connected to host: " + host);
    						chooseNick();
    						String msg2 = in.readLine();
    						while(msg2 != null)
    						{
    							displayMsg(msg2);
    							msg2 = in.readLine();
    						}
    					} catch(Exception e) { e.printStackTrace(); }
    				}
    			});
    			t.start();
    		} catch(Exception e) { e.printStackTrace(); }
    
    		main = new JFrame();
    		main.setLayout(new BorderLayout());
    		main.setSize(400,450);
    		main.setResizable(false);
    
    		displayPanel = new JPanel();
    		messages = new JTextArea(20,35);
    		messages.setEditable(false);
    		messagePane = new JScrollPane(messages);
    		displayPanel.add(messagePane);
    
    		sendM = new JTextField(18);
    		sendM.addKeyListener(new KeyListener()
    		{
    			public void keyPressed(KeyEvent e)
    			{
    				if(e.getKeyCode() == e.VK_ENTER) { send.doClick(); }
    			}
    			public void keyReleased(KeyEvent e){}
    			public void keyTyped(KeyEvent e){}
    		});
    
    		send = new JButton("Send");
    		send.addActionListener(this);
    
    		sendPanel = new JPanel();
    		sendPanel.add(sendM);
    		sendPanel.add(send);
    
    		main.getContentPane().add(displayPanel,BorderLayout.NORTH);
    		main.getContentPane().add(sendPanel,BorderLayout.SOUTH);
    
    		main.setVisible(true);
    		sendM.requestFocusInWindow();
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource() == send)
    		{
    			out.println(sendM.getText());
    			sendM.setText("");
    		}
    		sendM.requestFocusInWindow();
    	}
    
    	public void displayMsg(String themsg)
    	{
    		messages.append("\n" +themsg);
    	}
    
    	public void chooseNick()
    	{
    		String k = JOptionPane.showInputDialog("Choose a nickname.");
    		out.println("/nick " + k);
    	}
    }

    Java Chatroom

Similar Threads

  1. [Release] Java MMowned RealmChanger
    By Pragma in forum WoW EMU Programs
    Replies: 9
    Last Post: 03-02-2008, 01:25 PM
  2. World Editor, In Java!!
    By Chuck Norris in forum WoW ME Questions and Requests
    Replies: 2
    Last Post: 02-29-2008, 02:39 PM
  3. Java Developers
    By tharo in forum World of Warcraft Model Editing
    Replies: 36
    Last Post: 02-02-2008, 05:02 PM
  4. Java Developers
    By tharo in forum World of Warcraft Model Editing
    Replies: 25
    Last Post: 11-20-2007, 11:30 PM
All times are GMT -5. The time now is 10:01 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search