473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

java tcp/udp chat program with GUI

eyeofsoul
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?
Feb 24 '08 #1
1 24473
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.
Feb 24 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
3
by: A | last post by:
Hi all! I am currently working on a project where I need to create a web-based chat application that will be used for auctions. If possible, could I get some advice on how to create chat...
33
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
2
by: raski | last post by:
hello all! how is it possible to backdoor a java based chat room? www.iskon.hr/chat/chat.jsp
1
by: raski | last post by:
i was wondering if any of you guys would know any trick i could pull on some of my friends on a java based chat room. nothing harmful, only for playing around... www.iskon.hr/chat/chat.jsp ...
8
by: jason.m.ho | last post by:
>From the common user perspective (like my grandma), why would they care if its a java applet or an ajax application? Say I want to make a chat system on my website...If i'm doing really involved...
3
by: watusiboy | last post by:
i have created a chat application in java. the server listens to clients, receive messages from them, and send these message to other clients. i intend to embed the client program as an applet in a...
1
by: linksterman | last post by:
I am making this pretty basic chat project, and I found this echo server on the internet and decided to modify it my needs (much of the methods and fields are the same). Basically what my program...
0
sreekandank
by: sreekandank | last post by:
Hi..... I have done the program for simple chat application in java....as given below.... server.java import java.net.*; import java.io.*; public class server { public static void...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.