Connecting Tech Pros Worldwide Help | Site Map

java tcp/udp chat program with GUI

eyeofsoul's Avatar
Newbie
 
Join Date: Sep 2007
Posts: 31
#1: Feb 24 '08
i have build the gui but i am having problem with implementing the tcp/udp. can somebody help me.
i have server and client. the server can chat with the client. when i tried to to use the udp of tcp the gui seems hangs..

this is the code for server
Expand|Select|Wrap|Line Numbers
  1. package ds;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.*;
  6. import java.net.*;
  7. import java.util.*;
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import javax.swing.border.TitledBorder;
  11.  
  12. public class Server extends JFrame implements ActionListener {
  13.   // Text area for displaying contents
  14.   private JTextField toclient = new JTextField();
  15.   private JTextArea display = new JTextArea();
  16.   private JButton send = new JButton("Send/Start Server");
  17.   public DatagramSocket aSocket;
  18.   public DatagramPacket request;
  19.   public DatagramPacket reply;
  20.  
  21.   public static void main(String[] args) {
  22.  
  23.     new Server();
  24.   }
  25.  
  26.     public void actionPerformed(ActionEvent e) {
  27.        if (e.getSource() == send){
  28.            try {
  29.  
  30.            String msg = toclient.getText();
  31.            byte [] xy = msg.getBytes();
  32.            reply = new DatagramPacket(xy,xy.length,
  33.            request.getAddress(), request.getPort());
  34.  
  35.            //sending a reply message to client
  36.                 aSocket.send(reply);
  37.             } catch (IOException ex) {
  38.                 ex.printStackTrace();
  39.             }
  40.        }
  41.     }
  42.  
  43.   public Server() {
  44.  
  45.     JPanel input = new JPanel();
  46.     input.setLayout(new BorderLayout());
  47.     input.setBorder(new TitledBorder("Enter Message"));
  48.     input.add(toclient, BorderLayout.CENTER);
  49.     input.add(send, BorderLayout.EAST);
  50.  
  51.     JPanel output = new JPanel();
  52.     output.setLayout(new BorderLayout());
  53.     output.setBorder(new TitledBorder("Conversation"));
  54.     output.add(display, BorderLayout.CENTER);
  55.  
  56.     JPanel gabung = new JPanel();
  57.     gabung.setLayout(new GridLayout(2, 1));
  58.     gabung.add(input);
  59.     gabung.add(output);
  60.  
  61.     this.getContentPane().add(gabung, BorderLayout.NORTH);
  62.     send.addActionListener(this);
  63.  
  64.     setTitle("Chat Server");
  65.     setSize(500, 300);
  66.     setVisible(true);
  67.  
  68.  
  69.  
  70.      }
  71.  
  72.  
  73. }

this is client
Expand|Select|Wrap|Line Numbers
  1. package ds;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.*;
  6. import java.net.*;
  7. import java.util.*;
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import javax.swing.border.TitledBorder;
  11.  
  12. public class Client extends JFrame implements ActionListener {
  13.   // Text area for displaying contents
  14.   private JTextField toclient = new JTextField();
  15.   private JTextArea display = new JTextArea();
  16.   private JButton send = new JButton("Send");
  17.  
  18.   public static void main(String[] args) {
  19.     new Client();
  20.   }
  21.  
  22.     public void actionPerformed(ActionEvent e) {
  23.     }
  24.  
  25.   public Client() {
  26.  
  27.     JPanel input = new JPanel();
  28.     input.setLayout(new BorderLayout());
  29.     input.setBorder(new TitledBorder("Enter Message"));
  30.     input.add(toclient, BorderLayout.CENTER);
  31.     input.add(send, BorderLayout.EAST);
  32.  
  33.     JPanel output = new JPanel();
  34.     output.setLayout(new BorderLayout());
  35.     output.setBorder(new TitledBorder("Conversation"));
  36.     output.add(display, BorderLayout.CENTER);
  37.  
  38.     JPanel gabung = new JPanel();
  39.     gabung.setLayout(new GridLayout(2, 1));
  40.     gabung.add(input);
  41.     gabung.add(output);
  42.  
  43.     this.getContentPane().add(gabung, BorderLayout.NORTH);
  44.  
  45.     setTitle("Chat Client");
  46.     setSize(500, 300);
  47.     setVisible(true);
  48.  
  49.  
  50.  
  51. }
  52.  
  53.  
  54.  
  55. }
where do i need to implement the udp or tcp?
Newbie
 
Join Date: Dec 2007
Posts: 6
#2: Feb 24 '08

re: java tcp/udp chat program with GUI


You can do it simply using tcp. Use "ServerSocket" on server side to accept the connection from client. Using "Socket" class on client side connect to the server. Then u can communicate by creating the input and output streams for the socket.
Reply