java tcp/udp chat program with GUI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eyeofsoul
    New Member
    • Sep 2007
    • 31

    java tcp/udp chat program with GUI

    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
    Code:
    package ds;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;
    
    public class Server extends JFrame implements ActionListener {
      // Text area for displaying contents
      private JTextField toclient = new JTextField();
      private JTextArea display = new JTextArea();
      private JButton send = new JButton("Send/Start Server");
      public DatagramSocket aSocket;
      public DatagramPacket request;
      public DatagramPacket reply;
    
      public static void main(String[] args) {
          
        new Server();
      }
    
        public void actionPerformed(ActionEvent e) {
           if (e.getSource() == send){
               try {
               
               String msg = toclient.getText();
               byte [] xy = msg.getBytes();
               reply = new DatagramPacket(xy,xy.length,
               request.getAddress(), request.getPort());
                
               //sending a reply message to client
                    aSocket.send(reply);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
           }
        }
    
      public Server() {
        
        JPanel input = new JPanel();
        input.setLayout(new BorderLayout());
        input.setBorder(new TitledBorder("Enter Message"));
        input.add(toclient, BorderLayout.CENTER);
        input.add(send, BorderLayout.EAST);
        
        JPanel output = new JPanel();
        output.setLayout(new BorderLayout());
        output.setBorder(new TitledBorder("Conversation"));
        output.add(display, BorderLayout.CENTER);
        
        JPanel gabung = new JPanel();
        gabung.setLayout(new GridLayout(2, 1));
        gabung.add(input);
        gabung.add(output);
        
        this.getContentPane().add(gabung, BorderLayout.NORTH);
        send.addActionListener(this);
        
        setTitle("Chat Server");
        setSize(500, 300);
        setVisible(true);
        
        
        
         }
      
       
    }

    this is client
    Code:
    package ds;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;
    
    public class Client extends JFrame implements ActionListener {
      // Text area for displaying contents
      private JTextField toclient = new JTextField();
      private JTextArea display = new JTextArea();
      private JButton send = new JButton("Send");
    
      public static void main(String[] args) {
        new Client();
      }
    
        public void actionPerformed(ActionEvent e) {
        }
    
      public Client() {
        
        JPanel input = new JPanel();
        input.setLayout(new BorderLayout());
        input.setBorder(new TitledBorder("Enter Message"));
        input.add(toclient, BorderLayout.CENTER);
        input.add(send, BorderLayout.EAST);
        
        JPanel output = new JPanel();
        output.setLayout(new BorderLayout());
        output.setBorder(new TitledBorder("Conversation"));
        output.add(display, BorderLayout.CENTER);
        
        JPanel gabung = new JPanel();
        gabung.setLayout(new GridLayout(2, 1));
        gabung.add(input);
        gabung.add(output);
        
        this.getContentPane().add(gabung, BorderLayout.NORTH);
        
        setTitle("Chat Client");
        setSize(500, 300);
        setVisible(true);
        
        
        
    }
    
     
      
    }
    where do i need to implement the udp or tcp?
  • swatibksh
    New Member
    • Dec 2007
    • 6

    #2
    You can do it simply using tcp. Use "ServerSock et" 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.
    Last edited by r035198x; Feb 25 '08, 07:53 AM. Reason: code tags bug had struck again

    Comment

    Working...