-
-
Save TuyenGa/0528c93b58fc4ca4dd0b to your computer and use it in GitHub Desktop.
A JAVA chat client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.awt.FlowLayout; | |
import java.awt.Font; | |
import java.awt.GridLayout; | |
import java.awt.Image; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.KeyAdapter; | |
import java.awt.event.KeyEvent; | |
import java.awt.image.BufferedImage; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.awt.Color; | |
import java.awt.FlowLayout; | |
import java.awt.Font; | |
import java.awt.GridLayout; | |
import java.awt.Image; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.AdjustmentEvent; | |
import java.awt.event.AdjustmentListener; | |
import java.awt.event.KeyAdapter; | |
import java.awt.event.KeyEvent; | |
import java.awt.image.BufferedImage; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.MalformedURLException; | |
import java.net.Socket; | |
import java.net.SocketException; | |
import java.net.URL; | |
import java.net.UnknownHostException; | |
import javax.imageio.ImageIO; | |
import javax.swing.BorderFactory; | |
import javax.swing.Icon; | |
import javax.swing.ImageIcon; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JSplitPane; | |
import javax.swing.JTextArea; | |
import javax.swing.JTextField; | |
import javax.swing.JTextPane; | |
import javax.swing.border.Border; | |
import javax.swing.border.TitledBorder; | |
import javax.swing.text.BadLocationException; | |
import javax.swing.text.SimpleAttributeSet; | |
import javax.swing.text.Style; | |
import javax.swing.text.StyleConstants; | |
import javax.swing.text.StyledDocument; | |
/** a chat client class that includes graphics | |
* this class communicates with the user through a graphical display | |
* upon running the program the user is prompted to enter the | |
* IP address of the server | |
* once the connection is established, the user enters a screen name | |
* after the submission of the screen name, the client is able to send | |
* messages to all other clients that are logged in at the moment | |
*/ | |
public class ChatClient { | |
BufferedReader in; | |
PrintWriter out; | |
JFrame frame = new JFrame("Chatter"); | |
JTextField textField; | |
JTextPane messageArea; | |
JTextArea panel; | |
StyledDocument doc; | |
/** | |
* Constructor for the client | |
* also sets up the initial GUI | |
*/ | |
public ChatClient() { | |
JPanel mainPanel = (JPanel) frame.getContentPane(); | |
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, | |
getChat(), getPictures()); | |
splitPane.setDividerLocation(400); | |
splitPane.setResizeWeight(0.5); | |
mainPanel.add(splitPane); | |
frame.setSize(600,500); | |
frame.setTitle("Group chat"); | |
frame.setVisible(true); | |
/** | |
* Add action listeners to detect the entry of the clients | |
*/ | |
textField.addActionListener(new ActionListener() { | |
/** | |
* takes in the entry in the textfield and clears it for the next entry | |
*/ | |
public void actionPerformed(ActionEvent e) { | |
out.println(textField.getText()); | |
textField.setText(""); | |
} | |
}); | |
} | |
/** | |
* Method to get the address of the server from the user | |
*/ | |
private String getServerAddress() { | |
return JOptionPane.showInputDialog( | |
frame, | |
"Enter IP Address of the Server:", | |
"Welcome to the Chatter", | |
JOptionPane.QUESTION_MESSAGE); | |
} | |
/** | |
* Method to get the screen name of the user | |
*/ | |
private String getName() { | |
return JOptionPane.showInputDialog( | |
frame, | |
"Choose a screen name:", | |
"Screen name selection", | |
JOptionPane.PLAIN_MESSAGE); | |
} | |
/** | |
* makes the connection to the server | |
* enters the loop that executes the communication | |
* | |
*/ | |
public void run() throws IOException, BadLocationException, UnknownHostException, SocketException { | |
String serverAddress = getServerAddress(); | |
Socket socket = new Socket(serverAddress, 9001); | |
in = new BufferedReader(new InputStreamReader( | |
socket.getInputStream())); | |
out = new PrintWriter(socket.getOutputStream(), true); | |
/** | |
* infinite loop that processes all the incoming input from the server | |
*/ | |
while (true) { | |
String line = in.readLine(); | |
doc = messageArea.getStyledDocument(); | |
if (line.startsWith("SUBMITNAME")) { | |
out.println(getName()); | |
} | |
else if (line.startsWith("NAMEACCEPTED")) { | |
textField.setEditable(true); | |
} else if (line.startsWith("MESSAGE")) { | |
doc.insertString(doc.getLength(), line.substring(8)+ "\n", new SimpleAttributeSet()); | |
} | |
else if(line.startsWith("NAMELIST")){ | |
panel.setText("Members are: \n" + line.substring(8) + "\n"); | |
} | |
else if(line.startsWith("JOIN")){ | |
doc.insertString(doc.getLength(), "\n" +line.substring(5)+ "\n", new SimpleAttributeSet()); | |
StyledDocument doc = (StyledDocument)messageArea.getDocument(); | |
Style style = doc.addStyle("StyleName", null); | |
StyleConstants.setIcon(style, getJoinImage()); | |
doc.insertString(doc.getLength(), "ignored text", style); | |
doc.insertString(doc.getLength(), "\n", new SimpleAttributeSet()); | |
} | |
else if(line.startsWith("LEFT")){ | |
doc.insertString(doc.getLength(), "\n" +line.substring(5)+ "\n", new SimpleAttributeSet()); | |
StyledDocument doc = (StyledDocument)messageArea.getDocument(); | |
Style style = doc.addStyle("StyleName", null); | |
StyleConstants.setIcon(style, getLeaveImage()); | |
doc.insertString(doc.getLength(), "ignored text", style); | |
doc.insertString(doc.getLength(), "\n", new SimpleAttributeSet()); | |
} | |
} | |
} | |
/** | |
* Method to set the image when a client leaves the chat room | |
* @return | |
*/ | |
private ImageIcon getLeaveImage(){ | |
URL url; | |
ImageIcon waiting = null; | |
try | |
{ | |
url = new URL("https://si0.twimg.com/profile_images/1769643466/258844_104131489680984_104118713015595_32268_721285_o__1_.jpeg"); | |
waiting = new ImageIcon(url); | |
} | |
catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return waiting; | |
} | |
/** | |
* Method to set the image when a new client joins the chat room | |
* @return | |
*/ | |
private ImageIcon getJoinImage(){ | |
URL url; | |
ImageIcon waiting = null; | |
try | |
{ | |
url = new URL("http://im.tech2.in.com/gallery/2011/aug/anonymous_640x360_110936481249_640x360.jpg"); | |
waiting = new ImageIcon(url); | |
} | |
catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return waiting; | |
} | |
/** | |
* method to set up the GUI that will enable the chat interaction | |
* @return | |
*/ | |
private JSplitPane getChat(){ | |
messageArea = new JTextPane(); | |
messageArea.setEditable(false); | |
JScrollPane scrollableMessages = new JScrollPane(messageArea); | |
scrollableMessages.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){ | |
public void adjustmentValueChanged(AdjustmentEvent e){ | |
messageArea.select(messageArea.getHeight()+1000,0); | |
}}); | |
Border etchedBorder = BorderFactory.createEtchedBorder(); | |
Border border = BorderFactory.createTitledBorder(etchedBorder, "Messages", | |
TitledBorder.DEFAULT_JUSTIFICATION, | |
TitledBorder.DEFAULT_POSITION, | |
new Font("Serif", Font.BOLD, 20), Color.PINK); | |
messageArea.setBorder(border); | |
textField = new JTextField(); | |
JScrollPane typeScrollableMessage = new JScrollPane(textField); | |
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, | |
scrollableMessages,typeScrollableMessage); | |
splitPane.setDividerLocation(400); | |
return splitPane; | |
} | |
/** | |
* Method to get the pictures to set up the GUI | |
* @return | |
*/ | |
private JSplitPane getPictures(){ | |
panel = new JTextArea(); | |
panel.setBackground(Color.LIGHT_GRAY); | |
panel.setFont(new Font("Serif", Font.BOLD, 20)); | |
panel.setForeground(Color.BLUE); | |
panel.setLayout(new FlowLayout()); | |
panel.setText("Members of Chat: \n \n"); | |
JScrollPane scrollable = new JScrollPane(panel); | |
JPanel gifPanel = new JPanel(); | |
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, | |
scrollable,gifPanel); | |
splitPane.setDividerLocation(300); | |
addGIF(gifPanel); | |
return splitPane; | |
} | |
/** | |
* Method that will set up part of the GUI for the chat panel | |
* @param gifPanel | |
*/ | |
private void addGIF(JPanel gifPanel) { | |
URL url; | |
try { | |
url = new URL("http://assets.nydailynews.com/polopoly_fs/1.135224.1313980496!/img/httpImage/image.jpg_gen/derivatives/landscape_635/alg-anonymous-jpg.jpg"); | |
ImageIcon waiting = new ImageIcon(url); | |
Image img = waiting.getImage(); | |
Image newimg = img.getScaledInstance(80, 80, java.awt.Image.SCALE_SMOOTH); | |
JLabel waitingLabel = new JLabel(waiting); | |
gifPanel.setLayout(new GridLayout(0,1)); | |
gifPanel.add(waitingLabel); | |
} | |
catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.net.SocketException; | |
import java.net.UnknownHostException; | |
import javax.lang.model.element.UnknownElementException; | |
import javax.swing.JFrame; | |
import javax.swing.text.BadLocationException; | |
/** | |
* main class for handling the client-side of the communication | |
* @author kaushiktewari | |
* | |
*/ | |
public class ChatClientMain { | |
public static void main(String[] args) throws UnknownElementException, UnknownHostException, IOException, BadLocationException { | |
/** | |
* create a new client and call the run method of the client class | |
*/ | |
try | |
{ | |
ChatClient client = new ChatClient(); | |
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
client.frame.setVisible(true); | |
client.run(); | |
} | |
catch(UnknownElementException e) | |
{ | |
System.out.println("the connection to the server could not be established \n please enter a valid server location"); | |
} | |
catch(SocketException e) | |
{ | |
System.out.println("the connection to the server could not be established \n please enter a valid server location"); | |
} | |
catch(Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Graphics; | |
import java.awt.image.BufferedImage; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.HashSet; | |
import javax.imageio.ImageIO; | |
import javax.swing.ImageIcon; | |
/** | |
* Main class for handling the server-side of the chat program | |
* allows clients to establish connections | |
* first asks for a screen name and keeps on asking until | |
* it receives a unique screen name | |
* waits for connections on port number 9001 which is kept constant | |
*/ | |
public class ChatServer { | |
/**set the port number | |
* | |
*/ | |
private static final int PORT = 9001; | |
/** | |
* the main method keeps on waiting for connections | |
* once a connection is established, a thread is started to handle | |
* the communication | |
*/ | |
public static void main(String[] args) throws Exception { | |
ServerSocket listener = null; | |
try{ | |
listener = new ServerSocket(PORT); | |
} | |
catch(Exception e){ | |
System.out.println("ERROR! Try again"); | |
System.exit(0); | |
} | |
System.out.println("The chat server is running."); | |
try{ | |
while (true) { | |
new ChatServerHandler(listener.accept()).start(); | |
} | |
} | |
catch(Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
finally { | |
listener.close(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.Socket; | |
import java.util.HashSet; | |
import javax.swing.ImageIcon; | |
public class ChatServerHandler extends Thread { | |
private String name; | |
private Socket socket; | |
private BufferedReader in; | |
private PrintWriter out; | |
private String namesList; | |
private static HashSet<String> names = new HashSet<String>(); | |
private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>(); | |
/**the set of all the names of the clients in the chat room | |
// helps to keep screen names being used | |
//the set of all the writers of the clients | |
// this enables us to broadcast messages to each client | |
/** | |
* gets the socket from the server | |
*/ | |
public ChatServerHandler(Socket socket) { | |
this.socket = socket; | |
namesList =""; | |
} | |
/** | |
* run method for the thread | |
* handels the main part of the communication | |
*/ | |
public void run() { | |
try { | |
in = new BufferedReader(new InputStreamReader( | |
socket.getInputStream())); | |
out = new PrintWriter(socket.getOutputStream(), true); | |
/** gets the name from the client | |
* | |
*/ | |
while (true) { | |
out.println("SUBMITNAME"); | |
name = in.readLine(); | |
if (name == null) { | |
return; | |
} | |
synchronized (names) { | |
if (!names.contains(name)) { | |
names.add(name); | |
for(String name:names){ | |
namesList = namesList + name +", "; | |
} | |
break; | |
} | |
} | |
} | |
/**once a name has been accepted, the communication can proceed | |
* | |
*/ | |
out.println("NAMEACCEPTED"); | |
writers.add(out); | |
for (PrintWriter writer : writers) { | |
writer.println("JOIN YAY! " + name + " has joined the chat! " ); | |
writer.println("NAMELIST" + namesList); | |
} | |
while (true) { | |
String input = in.readLine(); | |
if (input == null) { | |
return; | |
} | |
for (PrintWriter writer : writers) { | |
writer.println("MESSAGE " + name + ": " + input); | |
} | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
/** | |
* when the client exits, remove its name and close its socket | |
*/ | |
if (name != null) { | |
names.remove(name); | |
namesList = ""; | |
for(String name:names){ | |
namesList = namesList + name +", "; | |
} | |
} | |
if (out != null) { | |
writers.remove(out); | |
} | |
for (PrintWriter writer : writers) { | |
writer.println("LEFT NOOOOO!" + name + " has left the chat!" ); | |
writer.println("NAMELIST" + namesList); | |
} | |
try { | |
socket.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment