|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hey guys i just started java at skool.........it has been challenging so far but i'm dealing with it anyway
i have an assignment to make a chat application ........... i have 2 files one is the server and the other is the client........here is my problem now the client can send messages to the server FINE......but the server cant reply to the client.......... HERE IS THE SERVER CODE //set up the gizzle server that will recieve packets from a client import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GizzleServer extends JFrame implements ActionListener { private JTextArea display; private JTextField sendMessage; private JButton sendButton; private JButton resetButton; private JLabel javaLabel; private Color colorValues[]= { Color.black,Color.blue,Color.red,Color.green}; private JRadioButtonMenuItem colorItems[],fonts[]; private JCheckBoxMenuItem styleItems[]; private DatagramPacket sendPacket,receivePacket; private DatagramSocket socket; public GizzleServer() { super("Gizzle Chatter Box Server"); Container c = getContentPane(); //creating buttons Icon send = new ImageIcon("sendButton.jpg"); Icon sendRollover = new ImageIcon("sendButtonRollover.jpg"); Icon reset = new ImageIcon("resetButton.jpg"); sendButton = new JButton("Send ",send); sendButton.setRolloverIcon(sendRollover); resetButton = new JButton("Reset",reset); c.add(resetButton,BorderLayout.SOUTH); c.add(sendButton,BorderLayout.WEST); sendMessage = new JTextField("Server User Type the Message Here"); sendMessage.addActionListener(this); getContentPane().add(sendMessage,BorderLayout.NORT H); display = new JTextArea(); getContentPane().add(new JScrollPane(display),BorderLayout.CENTER); setSize(500,400); show(); try { socket = new DatagramSocket(5000); } //end of try catch(SocketException se) { se.printStackTrace(); System.exit(1); } } //end of constructor public void waitForPackets() { while (true) { try { //set up packet byte data[] = new byte[100]; receivePacket = new DatagramPacket(data,data.length); //wait for packet socket.receive(receivePacket); //process packet display.append("\n" + "\nFrom Gizzle : " + receivePacket.getAddress() + "\nGizzle says>>>> \n\t" + new String(receivePacket.getData(),0,receivePacket.get Length() ) ); display.setCaretPosition(display.getText().length( ) ); //echo information from back to client display.append("\n\n Data to Client..."); sendPacket= new DatagramPacket(receivePacket.getData(),receivePack et.getLength(),receivePacket.getAddress(),receiveP acket.getPort() ); socket.send(sendPacket); display.append("Message Succesfull\n"); display.setCaretPosition(display.getText().length( ) ); }//end of try catch(IOException io) { display.append(io.toString() + "\n" ); io.printStackTrace(); } //end of catch } //end of while loop } //end of waitForPackets public void actionPerformed(ActionEvent e) { try { display.append("Message being sent" + e.getActionCommand() +"\n"); String s = e.getActionCommand(); byte data[] = s.getBytes(); sendPacket = new DatagramPacket(data,data.length,InetAddress.getLoc alHost(),5000); socket.send(sendPacket); display.append("Message Sent\n"); display.setCaretPosition(display.getText().length( ) ); } //end of try catch(IOException exception) { display.append(exception.toString() + "\n"); exception.printStackTrace(); } } public static void main(String args[]) { GizzleServer app = new GizzleServer(); app.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); app.waitForPackets(); }//end of p.s.v.m }//end of main class |
|
#2
|
|||
|
|||
|
this is the client
HERE IS THE CLIENT CODE
//setup for gizzle client import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GizzleClient extends JFrame implements ActionListener { private JTextField enter; private JTextArea display; private JButton sendButton; private JButton resetButton; private Color colorValues[]= { Color.black,Color.blue,Color.red,Color.green}; private JRadioButtonMenuItem colorItems[],fonts[]; private JCheckBoxMenuItem styleItems[]; private ButtonGroup fontGroup,colorGroup; private int style; private DatagramPacket sendPacket,receivePacket; private DatagramSocket socket; public GizzleClient() { super("Gizzle Chatter Box Messenger v1.0"); Container c = getContentPane(); //creating buttons Icon send = new ImageIcon("sendButton.jpg"); Icon sendRollover = new ImageIcon("sendButtonRollover.jpg"); Icon reset = new ImageIcon("resetButton.jpg"); sendButton = new JButton("Send ",send); sendButton.setRolloverIcon(sendRollover); resetButton = new JButton("Reset",reset); c.add(resetButton,BorderLayout.SOUTH); c.add(sendButton,BorderLayout.WEST); sendButton.addActionListener(this); JMenuBar bar = new JMenuBar(); //creating Menu Bar setJMenuBar(bar); //set the menubar for the JFrame //create File menu annd Exit menu item JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic('F'); JMenuItem aboutItem= new JMenuItem("About Gizzle Chatter..."); aboutItem.setMnemonic('A'); aboutItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null,"This is Gizzle's Chatter Box V1.0","About Chatter Box",JOptionPane.PLAIN_MESSAGE); } } //end of new actionListener ); //end of addActionlistener fileMenu.add(aboutItem); bar.add(fileMenu); //add File Menu JMenuItem exitItem = new JMenuItem("Exit"); exitItem.setMnemonic('x'); exitItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null,"GoodBye","Gizzle Chatter Box",JOptionPane.INFORMATION_MESSAGE); System.exit(0); } } ); fileMenu.add(exitItem); //create the Format menu JMenu formatMenu = new JMenu("Look & Feel"); formatMenu.setMnemonic('r'); //creating the COlor submenu String colors[]= { "Black","Blue","Red","Green"}; JMenu colorMenu = new JMenu("Background Color"); colorMenu.setMnemonic('C'); colorItems= new JRadioButtonMenuItem[colors.length]; colorGroup = new ButtonGroup(); ItemHandler itemHandler = new ItemHandler(); for (int i=0;i<colors.length;i++) { colorItems[i]= new JRadioButtonMenuItem(colors[i]); colorMenu.add(colorItems[i]); colorGroup.add(colorItems[i]); colorItems[i].addActionListener(itemHandler); } //end of for loop colorItems[0].setSelected(true); formatMenu.add(colorMenu); formatMenu.addSeparator(); // //formatMenu.add(fontMenu); bar.add(formatMenu); //Text Field box enter= new JTextField("User Type Your Message here"); enter.addActionListener(this); getContentPane().add(enter,BorderLayout.NORTH); display= new JTextArea(); display.setForeground(colorValues[0]); getContentPane().add(new JScrollPane(display),BorderLayout.CENTER); setSize(500,400); show(); try { socket = new DatagramSocket(); } catch(SocketException se) { se.printStackTrace(); System.exit(1); } //creating font submenu String fontNames[]= { "TimesRoman","Courier",Verdana"}; JMenu fontMenu = new JMenu("Font"); fontMenu.setMnemonic('n); fonts = new JRadioButtonMenuItem[fontNames.length]; fontGroup = new ButtonGroup(); for(int i =0;i<fonts.length;i++) { fonts[i]= new JRadioButtonMenuItem(fontNames[i]); fontMenu.add(fonts[i]); fontGroup.add(fonts[i]); fonts[i].addActionListener(itemHandler); } } //end of constructor public void waitForPackets() { while(true) { try { byte data[] = new byte[100]; receivePacket = new DatagramPacket(data,data.length); //wait for packet socket.receive(receivePacket); //process Packet display.append("\nMessage Received from server:--------------------->>> "+ "\nTo Server: " + receivePacket.getAddress() + "\nMessage Sent>>>>\n\n\n\n\t " + new String(receivePacket.getData(),0,receivePacket.get Length() ) ); display.setCaretPosition(display.getText().length( ) ); } //end of try catch(IOException exception) { display.append(exception.toString() + "\n"); exception.printStackTrace(); } //end of catch } //end of while loop } //end of waitForPackets() public void actionPerformed(ActionEvent e) { /* if (e.getSource()==enter) { enter="message here"); }*/ try { display.append("\nMessage being sent>>>>" + e.getActionCommand() + "\n"); String s = e.getActionCommand(); byte data[] = s.getBytes(); sendPacket = new DatagramPacket(data,data.length, InetAddress.getLocalHost(),5000); socket.send(sendPacket); display.append("Message sent\n"); display.setCaretPosition(display.getText().length( )); } //end of try catch(IOException exception) { display.append(exception.toString() + "\n"); exception.printStackTrace(); } //end of catch } //end of actionPerformed public static void main(String args[]) { GizzleClient app= new GizzleClient(); app.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); app.waitForPackets(); } class ItemHandler implements ActionListener { public void actionPerformed (ActionEvent e) { for(int i=0;i<colorItems.length;i++) if(colorItems[i].isSelected() ) { display.setBackground(colorValues[i]); break; } for(int i=0;i<fonts.length;i++) if(e.getSource() ==fonts[i]) { display.setFont(new Font(fonts[i].getText(),style,72)); break; } repaint(); } //actionPerformed (ActionEvent e) } //end of ItemHandler class SytleHandler implements ItemListener { public void itemStateChanged(ItemEvent e) { style=0; if(styleItems[0].isSelected()) style+=Font.BOLD; if(styleItems[1].isSelected()) style+=Font.ITALIC; display.setFont(new Font(display.getFont().getName(),style,72)); repaint(); } } }//end of main class |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > java chat application |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|