DreamsCoder.com

We Code your Dreams
home > java programs

Chat application using Socket programming in JAVA





Chat application using Socket programming in JAVA

import java.io.*;
import java.net.*;

public class chat_server
{
	
	public static void main(String[] args) {
		
	ServerSocket ss;  //Server 
	Socket client; // to accept clients request
	String nm;
	String msg,smsg;
	
	try
	{
	ss = new ServerSocket(1212);

	System.out.println("Server is Waitng For Client");
	client = ss.accept();

	System.out.println("Client is Connected ");

	BufferedReader br =
 new BufferedReader(new InputStreamReader
(System.in));
	BufferedReader br1 = 
 new BufferedReader(new InputStreamReader
(client.getInputStream()));

	PrintStream ps = new
PrintStream(client.getOutputStream());

	while(true)
	{
	msg = br1.readLine();
	System.out.println("Message is "+msg);
if(msg.equals("bye"))
	{
		break;
	}
	System.out.println("Enter Your Message");
	 smsg = br.readLine();
	ps.println(smsg);
	
	if(smsg.equals("bye"))
		{
			ps.println("bye");
			break;
		}
	}
	ps.close();
	br1.close();
	br.close();
	
	ss.close();
	
	}//try
	catch(Exception e)
	{
		System.out.println(e);
	}

}//main
}


/*CLient Program*/

import java.io.*;
import java.net.*;

public class chat_client
{

	public static void main(String[] args) 
	{
		Socket s;
		try
		{
		s = new Socket("127.0.0.1",1212);
		String msg,umsg;
		System.out.println("Clent is Conneccted
to the Server");
		
		BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
		BufferedReader br1 = new
BufferedReader(new InputStreamReader(s.getInputStream()));

		PrintStream ps = new PrintStream
(s.getOutputStream());
		while(true)
		{
		System.out.println("Enter Your
Message");
		msg = br.readLine();
		if(msg.equals("bye"))
			{
			break;
			}
		ps.println(msg);
		umsg = br1.readLine();
		System.out.println(umsg);
			
		if(umsg.equals("bye"))
		{
			ps.println("bye");
			break;
		}
		}	
		
	br1.close();
	ps.close();
	br.close();
	
	s.close();

	}
	catch(Exception e)
	{
		System.out.println(""+e);
	}//catch
	}//main
}

Label - java





Simple Moving Banner program in JAVA





Simple Moving Banner program in JAVA

Label - java





Form to accept users details in JAVA





Form to accept users details in JAVA

import java.awt.*;
import javax.swing.*;


class InfoWindow
{
	JFrame f ;
	JPanel p1 , p2 ;
	JLabel l1 , l2 , l3 , l4 ;
	JTextField t1 , t2 , t3 , t4 ;
	JRadioButton b1 , b2 , b3  ;
	ButtonGroup bg ;
	JComboBox b ;
	JCheckBox c1 , c2 , c3 ;
	
	
	InfoWindow()
	{
		f = new JFrame("Information") ;
		f.setBounds(200,200,300,275) ;
		p1 = new JPanel(new GridLayout(4,2)) ;
		p2 = new JPanel(new GridLayout(3,2)) ;
		l1 = new JLabel("Your Name : ") ;
		l2 = new JLabel("Name : ") ;
		l3 = new JLabel("Year : ") ;
		l4 = new JLabel("Hobbies : ") ;
		t1 = new JTextField(10) ;
		t2 = new JTextField(10) ;
		t3 = new JTextField(10) ;
		t4 = new JTextField(10) ;
		bg = new ButtonGroup() ;
		b1 = new JRadioButton("FY",true) ;
		b2 = new JRadioButton("SY") ;
		b3 = new JRadioButton("TY") ;
		c1 = new JCheckBox("Music",true) ;
		c2 = new JCheckBox("Dance") ;
		c3 = new JCheckBox("Sports") ;
		bg.add(b1) ;
		bg.add(b2) ;
		bg.add(b3) ;
		p1.add(b1) ;
		p1.add(c1) ;
		p1.add(b2) ;
		p1.add(c2) ;
		p1.add(b3) ;
		p1.add(c3) ;
		p2.add(l2) ;
		p2.add(t2) ;
		p2.add(l3) ;
		p2.add(t3) ;
		p2.add(l4) ;
		p2.add(t4) ;
		f.add(l1) ;
		f.add(t1) ;
		f.add(p1) ;
		f.add(p2) ;
		f.setLayout(new FlowLayout()) ;
		f.setVisible(true) ;
	}
	
	
	
	public static void main(String[] arg)
	{
		InfoWindow w = new InfoWindow() ;
	}
}

Label - java





Menubar with sub-menus in JAVA





Menubar with sub-menus in JAVA

import java.awt.*;
import javax.swing.*;



class MenuBarWindow
{
	JFrame f ;
	JMenuBar m ;
	JMenu file , edit , view , m1 , r1 ;
	JMenuItem m2 , m3 , m4 , m5 , m6 , m7 , m8 ,
                   m9 ;
	ButtonGroup bg ;
	JRadioButtonMenuItem r2 ;
	JCheckBoxMenuItem c1 , c2 , c3 ;
	
	public MenuBarWindow() 
	{
		f = new JFrame("Menu") ;
		f.setSize(200, 200) ;
		m = new JMenuBar() ;
		m.setSize(200, 200) ;
		file = new JMenu("FILE") ;
		view = new JMenu("VIEW") ;
		edit = new JMenu("EDIT") ;
		m1 = new JMenu("New") ;
		m2 = new JMenuItem("Open") ;
		m3 = new JMenuItem("Save") ;
		m4 = new JMenuItem("Exit") ;
		m5 = new JMenuItem("File") ;
		m6 = new JMenuItem("Folder") ;
		m7 = new JMenuItem("Copy") ;
		m8 = new JMenuItem("Paste") ;
		m9 = new JMenuItem("Cut") ;
		bg = new ButtonGroup() ;
		r1 = new JMenu("Ruler") ;
		r2 = new
JRadioButtonMenuItem("ToolBar") ;
		c1 = new JCheckBoxMenuItem("Standard") ;
		c2 = new JCheckBoxMenuItem("Format");
		c3 = new JCheckBoxMenuItem("Drawing");
		bg.add(r1) ;
		bg.add(r2) ;
		m1.add(m5) ;
		m1.add(m6) ;
		r1.add(c1) ;
		r1.add(c2) ;
		r1.add(c3) ;
		file.add(m1) ;
		file.add(m2) ;
		file.add(m3) ;
		file.add(new JMenuItem("______")) ;
		file.add(m4) ;
		edit.add(m7) ;
		edit.add(m9) ;
		edit.add(m8) ;
		view.add(r1) ;
		view.add(r2) ;
		m.add(file) ;
		m.add(edit) ;
		m.add(view) ;
		f.add(m) ;
		f.setLayout(new FlowLayout()) ;
		f.setVisible(true) ;		
	}
	
	
	public static void main(String[] arg)
	{
			MenuBarWindow w = new
MenuBarWindow() ;
	}

}

Label - java





Password Window Design in JAVA





Password Window Design in JAVA

import java.awt.*;
import javax.swing.*;



public class PasswordWindow
{
	JFrame f ;
	JTextField t ;
	JPasswordField p ;
	JLabel l1 , l2;
	JButton ok , cancel ;
	
	PasswordWindow()
	{
		f = new JFrame("Login Window") ;
		f.setBounds(200,200,350,150) ;
		t = new JTextField(20) ;
		p = new JPasswordField(20) ;
		l1 = new JLabel("Username : ");
		l2 = new JLabel("Password : ");
		ok = new JButton("OK") ;
		cancel = new JButton("Cancel") ;
		f.add(l1);
		f.add(t) ;
		f.add(l2);
 		f.add(p) ;
		f.add(ok) ;
		f.add(cancel) ;
		f.setLayout(new FlowLayout()) ;
		f.setVisible(true) ;
	}
	
	
	public static void main(String[] arg)
	{
		PasswordWindow w = new PasswordWindow() ;
	}
}

Label - java





< previous page | next page>






Privacy Policy
Copyright © 2018 by DreamsCoder. All Rights Reserved.
DreamsCoder Google Plus DreamsCoder Facebook



Latest Technology,Tricks and Tips