How To Create A Chat Server In Java
- Updated date Sep 30, 2019
- 175.4k
- 8
In this article, you will learn how to make a chat Application , using Sockets in Java.
Introduction
In this tutorial, we will see a Chat Application in Java, which is another module of a remote procedure call. We will deal with sockets and its parameter, to work out with our requirement. For generations, remote procedure call has been used to make message passing system in any environment. It can be a distributed system, standalone, or any client Server environment.
For details and a brief explanation of remote procedure calls, you can read this article to help understand, in quite a decent manner – Remote Procedure call.
Prerequisite
- Installed Java
- NetBeans SDK
Once NetBeans is installed, you have to make a Java Application. Name it as – Chat application. This will create a chat Application project inside your IDE. Also, there will be a 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 forms, one for Client and one for Server. Jframe is used to make a design of your Application. it has a simple configuration, which is similar to ASPX pages. Also, it has a decent toolbox with drag and drop functionality. Now, create two Jframe and name them Client.java and Server.java. Afterward, design is given below.
Client.java
You have to drag and drop these controls – TextArea, TextField, Button and Label. TextArea is used to view the incoming messages and TextField is used to write the message.
Server.java
Now that the coding part is there. Open client.java and Server.java. Enter 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;
- }
- 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;
- }
- 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
I hope you like it. Thank you for reading. Have a good day.
How To Create A Chat Server In Java
Source: https://www.c-sharpcorner.com/article/how-to-make-a-chat-application-using-sockets-in-java/
Posted by: hubbardwhationam.blogspot.com
0 Response to "How To Create A Chat Server In Java"
Post a Comment