Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Client Server Chat application

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    My Mood
    Tolerant
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Client Server Chat application

    Hi
    I haven't been here in a while and hadn't looked at java for 18 months, up until 3 weeks ago. I have two semesters (16 weeks) of Java behind me which i struggled with. That said I have been handed a java project out of the blue in college to complete(even though we are not doing Java this year).
    For all intensive purposes this is a chat application. I have done the majority of the work on the program and can get it to work opening different instances to communicate with each other on different machines. That’s Great.
    Effectively i have the client and server running on the same application.
    I open the application and select start server to listen for traffic on a specific port (hard coded in for the moment) associated to the machines IP address. At this point it freezes But wait.
    I can then kick of a second instance of the program and type in the the message i want to send. Hit the send button and it works, kind of(the issue here i understand and can fix). I have a very good idea of what I want to do from here to get it completed.
    My problem is this! I want to be able to use the single instance of the program to start the server and sent messages to itself or, to a different Machine running the same application. This I feel I can do providing I can prevent it freezing when I start up the server. I have tried all sorts of Throwables but still no joy. At this point I have spend the last week just doing trial and error and learning nothing, which is not good.

    I have attached the code below, any help would be greatly appreciated.

    package chat.itb.college;

    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;

    public class Chattie extends JFrame{


    private static final long serialVersionUID = 1L;
    protected static final String Double = null;
    private JPanel contentPane;
    private JTextField ipaddress;
    private JTextField portnumber;
    private JTextField conversationtext;
    private JTextField senttext;

    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    Chattie frame = new Chattie();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    public Chattie() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 590, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    final JButton startserver = new JButton("Start Server");
    startserver.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    DatagramSocket udpServerSocket = null;

    try {
    udpServerSocket = new DatagramSocket(7777);
    } catch (SocketException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    }

    while(true)
    {
    byte[] receiveData = new byte[1024];
    byte[] sendData = new byte[1024];
    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    try {
    udpServerSocket.receive(receivePacket);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String clientMessage = (new String(receivePacket.getData())).trim();
    conversationtext.setText("FROM CLIENT:" + clientMessage);
    InetAddress IPAddress = receivePacket.getAddress();
    int port = receivePacket.getPort();
    sendData = ("Received: \""+clientMessage+"\" from "+IPAddress.getHostName()).getBytes();
    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
    try {
    udpServerSocket.send(sendPacket);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }


    }
    }

    });
    startserver.setBounds(12, 39, 110, 25);
    contentPane.add(startserver);

    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    JButton stopbutton = new JButton("Stop Server");
    stopbutton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

    System.exit(0);

    }
    });
    stopbutton.setBounds(12, 77, 110, 25);
    contentPane.add(stopbutton);

    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    JButton send = new JButton("Send");
    send.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

    DatagramSocket udpClientSocket = null;
    try {
    udpClientSocket = new DatagramSocket();
    } catch (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    InetAddress IPAddress = null;
    try {
    IPAddress = InetAddress.getByName (ipaddress.getText());
    } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    byte[] sendData = new byte[1024];
    byte[] receiveData = new byte[1024];

    String clientRequest = senttext.getText();
    sendData = clientRequest.getBytes();
    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 7777);

    try {
    udpClientSocket.send(sendPacket);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    try {
    udpClientSocket.receive(receivePacket);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    String serverReply = new String(receivePacket.getData());
    conversationtext.setText("FROM SERVER:" + serverReply.trim());
    udpClientSocket.close();
    }
    });
    send.setBounds(12, 195, 110, 45);
    contentPane.add(send);

    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    JLabel ipLabel = new JLabel("Server Address");
    ipLabel.setBounds(134, 4, 97, 22);
    contentPane.add(ipLabel);
    getContentPane().add (ipLabel);
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    ipaddress = new JTextField();
    ipaddress.setText("Enter Address");
    ipaddress.setBounds(237, 4, 102, 22);
    contentPane.add(ipaddress);
    ipaddress.setColumns(10);
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    JLabel portLabel = new JLabel("Port Number");
    portLabel.setBounds(351, 4, 80, 22);
    contentPane.add(portLabel);
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    portnumber = new JTextField();
    portnumber.setText("Enter Port no.");
    portnumber.setColumns(10);
    portnumber.setBounds(443, 4, 104, 22);
    contentPane.add(portnumber);
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    conversationtext = new JTextField();
    conversationtext.setHorizontalAlignment(SwingConst ants.TRAILING);
    conversationtext.setBounds(134, 39, 413, 147);
    contentPane.add(conversationtext);
    conversationtext.setColumns(10);
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++
    senttext = new JTextField();
    senttext.setColumns(10);
    senttext.setBounds(134, 195, 413, 45);
    contentPane.add(senttext);
    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Client Server Chat application

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    How can the code be tested?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. server/client application fails when client closes
    By billykid in forum Java Networking
    Replies: 4
    Last Post: January 26th, 2012, 01:54 AM
  2. Basic client/server application
    By TheRedFox in forum Java Networking
    Replies: 1
    Last Post: November 25th, 2011, 09:40 AM
  3. Simple client-server chat program
    By Saloni Patil in forum Java Networking
    Replies: 3
    Last Post: October 22nd, 2011, 09:29 AM
  4. Server-Client Chat application using UDP
    By weakprogrammer in forum Java Networking
    Replies: 3
    Last Post: July 1st, 2011, 02:35 PM
  5. Replies: 1
    Last Post: April 20th, 2009, 11:17 AM