In this tutorial we will see a Chat Application in Java, which is another module of remote procedure call, we will deal with sockets and its parameter to work out with our requirement. From generations, remote procedure call has been used to make message passing system in any environment, can be a distributed system, standalone or any client Server environment.
For detail and brief explanation on remote procedure call, you can read this article to understand in quite decent manner – Remote Procedure call.
Prerequisite
- Installed Java
- NetBeans SDK
Once NetBeans is installed, you have to make a Java Application, name it as – Chatapplication. This will create chat Application project inside your IDE. Also, there will be main.java file created, which you can delete, as it is not required.
Once you are done with this, right click on the project -- > New -- > Select JFrame Form. You have to make two Jframe form, one for Client and one for Server. Jframe is used to make a design of your Application; it is having simple configuration; which is similar to ASPX pages. Also, have decent toolbox with drag and drop functionality. Now, create two Jframe and name it as Client.java and Server.java. Afterwards, Design is given below.
Client.java
You just have to drag and drop these controls – TextArea, TextField, Button and Label. TextArea is used to view the incoming messages, TextField is used to write the message.
Server.java
Now the coding part is there. Open client.java and Server.java. Enter into the code part.
Client.java
- package chatapplication;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.net.Socket;
-
-
-
-
-
- public class client extends javax.swing.JFrame
- {
-
- static Socket sckt;
- static DataInputStream dtinpt;
- static DataOutputStream dtotpt;
-
- public client() {
- initComponents();
- }
-
- @SuppressWarnings("unchecked")
-
-
- private void initComponents() {
-
-
- }
-
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
- {
-
-
-
- try
- {
- String msgout = "";
- msgout = jTextField1.getText().trim();
- dtotpt.writeUTF(msgout);
- }
- catch (Exception e)
- {
- }
- }
-
- public static void main(String args[])
- {
- java.awt.EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- new client().setVisible(true);
- }
- });
-
- try
- {
- sckt = new Socket("127.0.0.1",1201);
- dtinpt = new DataInputStream(sckt.getInputStream());
- dtotpt = new DataOutputStream(sckt.getOutputStream());
- String msgin = "";
- while(!msgin.equals("Exit"))
- {
- msgin=dtinpt.readUTF();
- jTextArea1.setText(jTextArea1.getText().trim()+"\n Server:"+msgin);
- }
- }
- catch(Exception e)
- {
-
- }
- }
-
-
- private javax.swing.JButton jButton1;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JScrollPane jScrollPane1;
- private static javax.swing.JTextArea jTextArea1;
- private javax.swing.JTextField jTextField1;
-
-
- }
Server. Java
- package chatapplication;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
-
-
- public class Server extends javax.swing.JFrame
- {
-
- static ServerSocket ssckt;
- static Socket sckt;
- static DataInputStream dtinpt;
- static DataOutputStream dtotpt;
-
-
-
- public Server() {
- initComponents();
- }
-
- @SuppressWarnings("unchecked")
-
- private void initComponents() { }
-
- private void btnsendActionPerformed(java.awt.event.ActionEvent evt)
- {
-
- try
- {
- String msgout = "";
- msgout = txtbxfield.getText().trim();
- dtotpt.writeUTF(msgout);
-
- }
- catch (Exception e)
- {
- }
-
-
- }
-
- public static void main(String args[])
- {
- java.awt.EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- new Server().setVisible(true);
- }
- });
-
- String msgin = "";
- try
- {
- ssckt = new ServerSocket(1201);
- sckt = ssckt.accept();
- dtinpt = new DataInputStream(sckt.getInputStream());
- dtotpt = new DataOutputStream(sckt.getOutputStream());
- while(!msgin.equals("exit"))
- {
- msgin =dtinpt.readUTF();
- txtbxarea.setText(txtbxarea.getText().trim()+"\n Client:"+msgin);
- }
- }
- catch(Exception e)
- {
- }
- }
-
-
- private javax.swing.JButton btnsend;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JScrollPane jScrollPane1;
- private static javax.swing.JTextArea txtbxarea;
- private javax.swing.JTextField txtbxfield;
-
-
- }
Output
Hope, you like it. Thank you for for reading. Have a good day.