Prerequisite: Datagrams in Java
In this article, we will learn how to use Datagrams in Java to create an Asynchronous messaging application in java. Asynchronous in this context means that both the server and the client can send each other texts independently without waiting for any kind of response from the other party. We will be using the concept of multi-threading to implement Sending and receiving text messages between the processes with the help of DatagramPackets. Datagrams are bundles of information passed between applications. Once the datagram has been released to its intended target, it is independent and there is no assurance that it will arrive or even that some application will be there to receive it. Java implements datagrams on top of the UDP (User Datagram Protocol) protocol.
Below is the implementation of the above approach.
Output: Window 1 (UnSyncChatServer.java)
Running UnSyncChatServer.java Server is Up.... Client (5334): hey hi Server says: hi Waiting for client response... ssup? Server says: ssup? Waiting for client response... Client (5334): good Client (5334): u? good as well Server says: good as well Waiting for client response... Client (5334): bye Client connection closed. bye Server says: bye Server exiting...
Output: Window 2 (UnSyncChatClient.java)
Running UnSyncChatClient.java Client is Up.... hey Client says: hey Waiting for server response... Server: hi Server: ssup? good Client says: good Waiting for server response... u? Client says: u? Waiting for server response... Server: good as well bye Client says: bye client exiting... Server: bye Server Stopped...
- Use an offline IDE to run this program as an online IDE may timeout.
- Run program 1 first and the program 2.
- A Group chat application in Java
- Multi-threaded Chat Application in Java | Set 2 (Client Side Programming)
- Multi-threaded chat Application in Java | Set 1 (Server Side Programming)
- Creating an Server-Client Application using the DatagramPacket and DatagramSocket classes
- Asynchronous and Synchronous Callbacks in Java
- Creating Frames using Swings in Java
- How to run Java RMI Application
- Simple Chat Room using Python
- Creating Sequential Stream from an Iterator in Java
- Pinging an IP address in Java | Set 2 (By creating sub-process)
- Java Swing | Creating a Toast Message
- Java Swing | Creating Custom Message Dialogs
- Creating Sheets in Excel File in Java using Apache POI
- Creating a Cell at specific position in Excel file using Java
- Asynchronous serial data transfer
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.