Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Chatting Application Project in JAVA

ClientApp.java

package com.java8s;
import java.awt.*;

public class ClientApp extends Frame implements ActionListener,Runnable
{
	//Declarations
	private static final long serialVersionUID = 1L;
	Button b;
	TextField tf;
	TextArea ta;
	Socket s;
	PrintWriter pw;
	BufferedReader br;
	Thread th;
	public ClientApp()
	{
		Frame f=new Frame("Bikash ");//Frame for Client
		f.setLayout(new FlowLayout());//set layout
		f.setBackground(Color.green);//set background color of the Frame
		b=new Button("Send");//Send Button
		b.addActionListener(this);//Add action listener to send button.
		f.addWindowListener(new W1());//add Window Listener to the Frame
		tf=new TextField(15);
		ta=new TextArea(12,20);
		ta.setBackground(Color.cyan);
		f.add(tf);//Add TextField to the frame
		f.add(b);//Add send Button to the frame
		f.add(ta);//Add TextArea to the frame
		try{
			s=new Socket(InetAddress.getLocalHost(),enter your system port number);//Socket for client
			//below line reads input from InputStreamReader
			br=new BufferedReader(new InputStreamReader(s.getInputStream()));
			//below line writes output to OutPutStream
			pw=new PrintWriter(s.getOutputStream(),true);
		}
		catch(Exception e)
		{
		}
		th=new Thread(this);//start a new thread
		th.setDaemon(true);//set the thread as demon
		th.start();
		setFont(new Font("Arial",Font.BOLD,20));
		f.setSize(200,200);//set the size
		f.setVisible(true);
		f.setLocation(100,300);//set the location
		f.validate();
	}
	//method required to close the Frame on clicking "X" icon.

	private class W1 extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{
		System.exit(0);
		}
	}

	//This method will called after clicking on Send button.
	public void actionPerformed(ActionEvent ae)
	{
		pw.println(tf.getText());//write the value of textfield into PrintWriter
		tf.setText("");//clean the textfield
	}
	//Thread running as a process in background
	public void run()
	{
		while(true)
		{
			try{
				ta.append(br.readLine()+"\n");//Append to TextArea
			}
			catch(Exception e) {}
		}
	}
	//Main method

	public static void main(String args[])

	{

		//Instantiate AppClient class
		@SuppressWarnings("unused")
		ClientApp client = new ClientApp();
	}
}

AppServer.java

package com.java8s;
import java.awt.*;
public class AppServer extends Frame implements ActionListener,Runnable
{
	//Declarations
	private static final long serialVersionUID = 1L;
	Button b1;
	TextField tf;
	TextArea ta;
	ServerSocket ss;
	Socket s;
	PrintWriter pw;
	BufferedReader br;
	Thread th;
	public AppServer()
	{
		int port = enter your system port number;
		Frame f=new Frame("server ");//Frame for Client
		f.setLayout(new FlowLayout());//set layout
		f.setBackground(Color.green);//set background color of the Frame
		b=new Button("Send");//Send Button
		b1.setBackground(Color.pink);
		b.addActionListener(this);//Add action listener to send button.
		tf=new TextField(15);
		ta=new TextArea(12,20);
		ta.setBackground(Color.cyan);
		f.addWindowListener(new W1());
		f.add(tf);//Add TextField to the frame
		f.add(b1);//Add send Button to the frame
		f.add(ta);//Add TextArea to the frame
		try{
			ss=new ServerSocket(port);//Socket for client
			s=ss.accept();
			System.out.println(s);
			//below line reads input form InputStreamReader
			br=new BufferedReader(new InputStreamReader(s.getInputStream()));
			//below line writes output to OutPutStream
			pw=new PrintWriter(s.getOutputStream(),true);
		}
		catch(Exception e)
		{
		}
		th=new Thread(this);//start a new thread
		th.setDaemon(true);//set the thread as demon
		th.start();
		setFont(new Font("Arial",Font.BOLD,20));
		f.setSize(200,200);//set the size
		f.setLocation(300,300);//set the location
		f.setVisible(true);
		f.validate();
	}
	//method required to close the Frame on clicking "X" icon.

	private class W1 extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{
			System.exit(0);
		}
	}

	//This method will called after clicking on Send button.
	public void actionPerformed(ActionEvent ae)
	{
		pw.println(tf.getText());//write the value of textfield into PrintWriter
		tf.setText("");//clean the textfield
	}
	//Thread running as a process in background
	public void run()
	{
		while(true)
		{
			try{
				String s=br.readLine();//reads the input from textfield
				ta.append(s+"\n");//Append to TextArea
			}
			catch(Exception e) {}
		}
	}
	//Main method

	public static void main(String args[])

	{
		//Instantiate AppClient class
		@SuppressWarnings("unused")
		AppServer server = new AppServer();
	}
}

Output:

java9-1

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext