473,748 Members | 10,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

java tcp/udp chat program with GUI

eyeofsoul
31 New Member
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 24522
swatibksh
6 New Member
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.
Feb 24 '08 #2

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

Similar topics

73
8062
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 as a standard portable part of the core language, the LISP package, and the java.lang package, respectively. Both have big integers, although only LISP has rationals as far as I can tell. Because CL supports keyword arguments, it has a wider range...
3
2596
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 applications and which part of the .NET framework should I delve into? I would really appreciate the help! Cheers,
33
9945
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 the following calculation: z = x! / (x- y)! The following code is my attempt and i was hoping for a point in the right direction as too where i am going wrong.. cheers
2
2633
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
1744
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 that's the link. i would be thankful if you could mail me some... Thanx in advance
8
2104
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 Comet push-style data communication, and rendering everything using DHTML, why would users prefer that over a java applet? Moreover, say I use a java applet to transfer data through a socket connection, then use DHTML to display the data, so...
3
1846
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 website that i'm developing. i am building this website using apache-php-mysql is there a way to trigger the server program (which is a java class) through php. what i want is that as apache starts, the server program is also executed. is this...
1
1743
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 does is acts like a chat room. One computer runs the server which other java programs connect to and can communicate with one another. The problem is that this server I run is horribly inefficient. I had to use 2 threads for each connection...
0
1550
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 main(String args)throws Exception
0
8984
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.