Results 1 to 6 of 6
Thread: Chat Client
- 05-28-2007, 06:58 AM #1
Member
- Join Date
- May 2007
- Posts
- 3
- Rep Power
- 0
Chat Client
I need to do this for my class
But I am confuse can someone help me out
The GUI to your chat client will look like the following:
The File menu will have Send(Ctrl-S) and Exit(Ctrl-E) menu items. The Edit menu will have a Cut(Ctrl-X), Copy(Ctrl-C), and Paste(Ctrl-V) menu items. The Help menu will have an About(F10) menu item. You will need to add mnemonics to each menu item, as shown in parentheses above. In other words, if the user types Ctrl-S, the text in the text field should be sent to the server, as if the OK button were clicked or the File->Send menu item were clicked.
Hopefully each of those menu items is self-explanatory, but one note is that a user will not be able to cut or paste from the large text field, but only copy. The About box from the Help menu will display a dialog box that shows information such as your name, the name of the application, the version, and of course, your instructor’s name.
The main class for your chat client will be named ChatClient and be in the chatclient.gui package.
For the networking portion of the project, your code will need to communicate with the ChatServer program we wrote in class. The ChatClient will begin by sending a message with a username to the ChatServer. The ChatServer will store the username with each socket connection so that each subsequent message that is sent from the ChatClient will be prepended with the username by the ChatServer before being sent to the other ChatClients. The ChatServer will prepend the username and “->”to the message.
You will also need to read a configuration file that will specify the user’s name, the server’s name, and the port on which the ChatServer is listening. If you would like to add more information into the configuration file, you can, but make sure it at least has these three parameters. Here is a sample configuration file:
# username
username=jeff
# server hostname
hostname=127.0.01
# server port
port=6789
Any line that is blank or starts with a # should be ignored. The rest of the lines will contain key=value pairs. You will read the name of the configuration parameter along the command line in your ChatClient code.
The username is the value that will be prepended to each line sent. The hostname and port give the location of the ChatServer. The ChatServer and ChatClient could be running on the same computer or separate computers.
Further, you need to have a different type of message to allow for private chats. In class, we only implemented broadcasting, meaning that whenever the ChatServer received a message, it sent that message to all users. You will need to allow another type of message to be transmitted that specifies the name of a user followed by the message to send to that user. In that case, only that user will receive the message, but not any other user.
Ensure that no exceptions can be thrown. Even for badly formatted configuration files, there should be no possibility of throwing an exception. Also, you may have helper classes if necessary.
- 05-28-2007, 12:02 PM #2levent Guest
Copy/Pasting a homework text here and waiting for help does not work on any forum. You should try to solve your problem yourself and ask help when you are got stuck somewhere.
You can start your research from the following url because you will need to learn how to use Java sockets to solve this homework:
Lesson: All About Sockets (The Java™ Tutorials > Custom Networking)
- 05-29-2007, 03:20 AM #3
Member
- Join Date
- May 2007
- Posts
- 3
- Rep Power
- 0
I need help
I need to implement the action to cut, copy, paste and send.
Here in my code I have the actionListener already added but it does not do anything...... Also I need to implement my GUI as a client Server do I need to enter the socket before the main method
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class ChatGui extends JFrame{
//private JTextField TF;
//private JTextArea TA;
private JButton ok;
ChatGui cg;
static JTextField tf;
static boolean s= true;
public ChatGui(){
super("Natalie's Chat Client");
setSize(500,300);
setLocation(100,100);
setLayout(new BorderLayout());
JTextArea TA = new JTextArea();
add(TA,BorderLayout.CENTER);
ok=new JButton("ok");
JPanel TFPanel = new JPanel();
TFPanel.setLayout(new BorderLayout());
JTextField TF = new JTextField();
TFPanel.add(TF,BorderLayout.CENTER);
TFPanel.add(ok,BorderLayout.EAST);
add(TFPanel,BorderLayout.SOUTH);
//add(TF,BorderLayout.SOUTH);
TA.setEditable(false);
JMenuBar jmb=new JMenuBar();
JMenu FileMenu = new JMenu("File");
JMenuItem SendMenuItem = new JMenuItem("Send");
SendMenuItem.setMnemonic(KeyEvent.VK_S);
SendMenuItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_S,ActionEvent.CTRL_MASK));
//*******add actionevents, to send
FileMenu.add(SendMenuItem);
JMenuItem ExitMenuItem = new JMenuItem("Exit");
ExitMenuItem.setMnemonic(KeyEvent.VK_E);
ExitMenuItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_E,ActionEvent.CTRL_MASK));
ExitMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.exit(1);
}
});
FileMenu.add(ExitMenuItem);
jmb.add(FileMenu);
JMenu EditMenu = new JMenu("Edit");
JMenuItem CutMenuItem = new JMenuItem("Cut");
CutMenuItem.setMnemonic(KeyEvent.VK_X);
CutMenuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_X,ActionEvent.CTRL_MASK));
CutMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try{
cg.getTextField().cut();
cg.setSaved(false);
}catch(NullPointerException npe){}
}
});
EditMenu.add(CutMenuItem);
JMenuItem CopyMenuItem = new JMenuItem("Copy");
CopyMenuItem.setMnemonic(KeyEvent.VK_C);
CopyMenuItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_C,ActionEvent.CTRL_MASK));
CopyMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try{
cg.getTextField().copy();
}catch(NullPointerException npe){}
}
});
//***********setActionevents from edit menu items,later.
EditMenu.add(CopyMenuItem);
JMenuItem PasteMenuItem = new JMenuItem("Paste");
PasteMenuItem.setMnemonic(KeyEvent.VK_V);
PasteMenuItem.setAccelerator(KeyStroke.getKeyStrok e(KeyEvent.VK_V,ActionEvent.CTRL_MASK));
PasteMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try{
cg.getTextField().paste();
cg.setSaved(false);
}catch(NullPointerException npe){}
}
});
EditMenu.add(PasteMenuItem);
jmb.add(EditMenu);
JMenu HelpMenu = new JMenu("Help");
JMenuItem AboutMenuItem= new JMenuItem("About");
AboutMenuItem.setMnemonic(KeyEvent.VK_F10);
AboutMenuItem.setAccelerator(KeyStroke.getKeyStrok e(KeyEvent.VK_F10,ActionEvent.SHIFT_MASK));
//*********might be a problem with the mnemonic for about. I'm not sure what to use in place
//of the ALT_MASK.
AboutMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JOptionPane.showMessageDialog(ChatGui.this,"Natali e Catherine Martinez\n ChatClient, version one\n co-author: Dr. Jeff Miller");
}
});
HelpMenu.add(AboutMenuItem);
jmb.add(HelpMenu);
setJMenuBar(jmb);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(1);
}
});
setVisible(true);
}
public static void setTextField(JTextField jtf) {
tf = jtf;
}
public static JTextField getTextField() {
return tf;
}
public void setSaved(boolean isSaved) {
s = isSaved;
}
public boolean getSaved() {
return s;
}
public static void main(String[] args) {
ChatGui cg= new ChatGui();
}
}
- 05-29-2007, 03:22 AM #4
Member
- Join Date
- May 2007
- Posts
- 3
- Rep Power
- 0
Thank you for the advise I did read the socket tutorial, and it's easy but not when you need to implement a GUI Chat with that
- 05-31-2007, 12:37 PM #5levent Guest
Hi again,
I checked your source code. It is running fine. I fixed some minor compilation errors and i guess they were because fo copy/paste to the forum. But in case you need it, i am pasting it to here again:
Java Code:/* * Main.java * * Created on 31 May�s 2007 Per�embe, 12:30 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package javaapplication2; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.KeyStroke; public class ChatGui extends JFrame{ //private JTextField TF; //private JTextArea TA; private JButton ok; ChatGui cg; static JTextField tf; static boolean s= true; public ChatGui(){ super("Natalie's Chat Client"); setSize(500,300); setLocation(100,100); setLayout(new BorderLayout()); JTextArea TA = new JTextArea(); add(TA,BorderLayout.CENTER); ok=new JButton("ok"); JPanel TFPanel = new JPanel(); TFPanel.setLayout(new BorderLayout()); JTextField TF = new JTextField(); TFPanel.add(TF,BorderLayout.CENTER); TFPanel.add(ok,BorderLayout.EAST); add(TFPanel,BorderLayout.SOUTH); //add(TF,BorderLayout.SOUTH); TA.setEditable(false); JMenuBar jmb=new JMenuBar(); JMenu FileMenu = new JMenu("File"); JMenuItem SendMenuItem = new JMenuItem("Send"); SendMenuItem.setMnemonic(KeyEvent.VK_S); SendMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK)); //*******add actionevents, to send FileMenu.add(SendMenuItem); JMenuItem ExitMenuItem = new JMenuItem("Exit"); ExitMenuItem.setMnemonic(KeyEvent.VK_E); ExitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,ActionEvent.CTRL_MASK)); ExitMenuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ System.exit(1); } }); FileMenu.add(ExitMenuItem); jmb.add(FileMenu); JMenu EditMenu = new JMenu("Edit"); JMenuItem CutMenuItem = new JMenuItem("Cut"); CutMenuItem.setMnemonic(KeyEvent.VK_X); CutMenuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_X,ActionEvent.CTRL_MASK)); CutMenuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ try{ cg.getTextField().cut(); cg.setSaved(false); }catch(NullPointerException npe){} } }); EditMenu.add(CutMenuItem); JMenuItem CopyMenuItem = new JMenuItem("Copy"); CopyMenuItem.setMnemonic(KeyEvent.VK_C); CopyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK)); CopyMenuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ try{ cg.getTextField().copy(); }catch(NullPointerException npe){} } }); //***********setActionevents from edit menu items,later. EditMenu.add(CopyMenuItem); JMenuItem PasteMenuItem = new JMenuItem("Paste"); PasteMenuItem.setMnemonic(KeyEvent.VK_V); PasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK)); PasteMenuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ try{ cg.getTextField().paste(); cg.setSaved(false); }catch(NullPointerException npe){} } }); EditMenu.add(PasteMenuItem); jmb.add(EditMenu); JMenu HelpMenu = new JMenu("Help"); JMenuItem AboutMenuItem= new JMenuItem("About"); AboutMenuItem.setMnemonic(KeyEvent.VK_F10); AboutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F10,ActionEvent.SHIFT_MASK)); //*********might be a problem with the mnemonic for about. I'm not sure what to use in place //of the ALT_MASK. AboutMenuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ JOptionPane.showMessageDialog(ChatGui.this,"Natali e Catherine Martinez\n ChatClient, version one\n co-author: Dr. Jeff Miller"); } }); HelpMenu.add(AboutMenuItem); jmb.add(HelpMenu); setJMenuBar(jmb); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(1); } }); setVisible(true); } public static void setTextField(JTextField jtf) { tf = jtf; } public static JTextField getTextField() { return tf; } public void setSaved(boolean isSaved) { s = isSaved; } public boolean getSaved() { return s; } public static void main(String[] args) { ChatGui cg= new ChatGui(); } }
Let us know if you need further help..
- 12-09-2009, 04:49 PM #6
Member
- Join Date
- Dec 2009
- Location
- AddisAbaba
- Posts
- 10
- Rep Power
- 0
Hey guys i was trying to send file to a server from a client and the file will be sent but a serious of small rectangles which are equal to the array length i initialized are also displayed with the file i sent in the server side; what code should i use to avoid this, please can somebody help me out.
this are the code i used
Client side
public void sendFile(){
try{
bis = new BufferedInputStream(new FileInputStream(jFileChooser1.getSelectedFile()));
bos = new BufferedOutputStream(client.getOutputStream( ));
byteArray = new byte[(int)jFileChooser1.getSelectedFile().length()];
bis.read(byteArray,0, byteArray.length);
bos.write(byteArray,0, byteArray.length);
bis.close();
bos.close();
}
Server side
public void receiveFile(){
try{
byte receivedData[] = new byte[40];
bis = new BufferedInputStream(socket.getInputStream());
bis.read(receivedData);
String st=new String (receivedData,0,receivedData.length);
System.out.println(st);
bos = new BufferedOutputStream(new FileOutputStream("belete.txt"));
bos.write(receivedData);
bis.close();
bos.close();
}
Similar Threads
-
Chat Applet in HTML
By Flynazn in forum Java AppletsReplies: 3Last Post: 05-27-2008, 10:26 AM -
Simple serverless chat solution
By goodjonx in forum NetworkingReplies: 3Last Post: 01-07-2008, 04:25 PM -
Help in Chat application
By zocky in forum NetworkingReplies: 0Last Post: 11-08-2007, 08:19 PM -
Java Program chat
By susan in forum Advanced JavaReplies: 1Last Post: 07-25-2007, 10:05 PM -
123 Flash Chat 6.6
By levent in forum Java SoftwareReplies: 0Last Post: 06-05-2007, 01:07 PM
Bookmarks