Web Browser using Java

import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class WebBrowser extends JFrame{
String temp,urlQueue[]=new String[20];
int F=0,R=-1;
JButton go=new JButton(“Go”);
JButton prev=new JButton(“Prev”);
JButton next=new JButton(“Next”);
JTextField url=new JTextField(30);
JEditorPane page=new JEditorPane();
public static void main(String args[]){
JFrame obj=new WebBrowser();
obj.setTitle(“Browser”);
obj.setSize(800,500);
obj.setVisible(true);
}
public WebBrowser(){
Components();
Events();
}
public void Components(){
getContentPane().setLayout(null);
url.setBounds(20,400,200,20);
go.setBounds(240,400,20,20);
prev.setBounds(100,440,40,20);
next.setBounds(160,440,40,20);
page.setBounds(10,10,790,375);
getContentPane().add(page);
getContentPane().add(url);
getContentPane().add(go);
getContentPane().add(next);
getContentPane().add(prev);
}
public void Events(){
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
);
go.addActionListener(new Browser());
prev.addActionListener(new Browser());
next.addActionListener(new Browser());
page.setEditable(false);
page.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent he){
if(he.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
{
try{
temp= he.getURL().toString();
page.setPage(temp);
R++;
urlQueue[R]=temp;
url.setText(temp);
}
catch(Exception e)
{

}
}
}
}
);
}
public class Browser implements ActionListener{
public void actionPerformed(ActionEvent ae) {
try{

if(ae.getSource()==go){
temp=url.getText();
page.setPage(temp);
R++;
urlQueue[R]=temp;
}
else if(ae.getSource()==prev){
R–;
temp=urlQueue[R];
page.setPage(temp);
}
else if(ae.getSource()==next){
R++;
temp=urlQueue[R];
page.setPage(temp);
}
}
catch(IOException e)
{
}

}
}
}

Java Socket – Client and Server Communication

/*  UDP – User Data Protocol – Client and server communication

A Graphical User Interface Program */

 

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

public class GUIClient extends JFrame
{

JButton send=new JButton(“Send”);
JTextField msg=new JTextField(20);
TextArea chat=new TextArea(20,20);
DatagramSocket ds=null;
DatagramPacket dp=null;

public class Receiver extends Thread
{
Thread t=null;
public Receiver()
{
t=new Thread(this);
t.start();
}
public void run()
{
String str1=”Server:”;
String str2;
byte data[]=new byte[1024];
try{
do{
DatagramPacket dp=new DatagramPacket(data,1024);
ds.receive(dp);
str2=dp.getData().toString();
chat.append(str1+str2+”\n”);

}while(!str2.equals(“end”));
}catch(Exception e)
{System.out.println(e);}
}
}

public static void main(String s[])
{
JFrame obj=new GUIClient();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setTitle(“CLIENT”);
obj.setVisible(true);
obj.setSize(400,400);
}

public GUIClient()
{
try{
ds=new DatagramSocket(7090);
}
catch(Exception e)
{
System.out.println(e);
}
components();
events();
Receiver r=new Receiver();
}

void components()
{
getContentPane().setLayout(null);
send.setBounds(270,20,80,30);
msg.setBounds(50,20,200,20);
chat.setBounds(20,60,320,300);
getContentPane().add(send);
getContentPane().add(msg);
getContentPane().add(chat);
}

void events()
{
send.addActionListener(new s());
}

public class s implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String str3;
byte data2[]=new byte[1024];
try{
InetAddress ia=InetAddress.getByName(“localhost”);// or IP of server
str3=msg.getText();
data2=str3.getBytes();
dp=new DatagramPacket(data2,data2.length,ia,7000);
ds.send(dp);
//ds.close();
}catch(Exception e1)
{
System.out.println(e1);
}
}
}
}

—————————–

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

public class GUIServer extends JFrame
{

JButton send=new JButton(“Send”);
JTextField msg=new JTextField(20);
TextArea chat=new TextArea(20,20);
DatagramSocket ds=null;
DatagramPacket dp=null;

public class Receiver extends Thread
{
Thread t=null;
public Receiver()
{
t=new Thread(this);
t.start();
}
public void run()
{
String str1=”Client:”;
String str2;
byte data[]=new byte[1024];
try{
do{
DatagramPacket dp=new DatagramPacket(data,1024);
ds.receive(dp);
str2=dp.getData().toString();
chat.append(str1+str2+”\n”);
}while(!str2.equals(“end”));
}catch(Exception e)
{System.out.println(e);}
}
}

public static void main(String s[])
{
JFrame obj=new GUIServer();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setTitle(“SERVER”);
obj.setVisible(true);
obj.setSize(400,400);
}

public GUIServer()
{
try{
ds=new DatagramSocket(7000);
}
catch(Exception e)
{
System.out.println(e);
}
components();
events();
Receiver r=new Receiver();
}

void components()
{
getContentPane().setLayout(null);
send.setBounds(270,20,80,30);
msg.setBounds(50,20,200,20);
chat.setBounds(20,60,320,300);
getContentPane().add(send);
getContentPane().add(msg);
getContentPane().add(chat);
}

void events()
{
send.addActionListener(new s());
}

public class s implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String str3;
byte data2[]=new byte[1024];
try{
InetAddress ia=InetAddress.getByName(“localhost”);
str3=msg.getText();
data2=str3.getBytes();
dp=new DatagramPacket(data2,data2.length,ia,7090);
ds.send(dp);
//ds.close();
}catch(Exception e1)
{
System.out.println(e1);
}
}
}
}

Chat program using Java RMI

A Chat program using Remote Method Invocation ( RMI )

import java.rmi.*;
public interface Chat extends java.rmi.Remote
{

public String send()throws RemoteException;
public void receive(String str)throws RemoteException;

}

—–

import java.rmi.*;
import java.rmi.ServerError.*;
import java.io.*;
public class ChatImpl extends java.rmi.server.UnicastRemoteObject implements Chat
{
public ChatImpl() throws java.rmi.RemoteException
{
super();

}
public String send()throws java.rmi.RemoteException

{try
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();System.out.println(str);
return str;
}catch(Exception e)
{
String str1=”some error”;
return str1;
}

}
public void receive(String str)throws java.rmi.RemoteException

{
System.out.println(str);
}
}

————

import java.rmi.*;
import java.io.*;
public class ChatClient implements Runnable{
static Chat c;
public static void main(String args[])throws IOException
{System.out.println(“enter static main”);

try
{
c=(Chat)Naming.lookup(“rmi://localhost/Chat”);
System.out.println(“lookupdone”);
}catch(Exception e){System.out.println(e);}
new ChatClient();
}
public ChatClient()
{
run();
}
public void run()
{

try{

String str=send();
System.out.println(“String is “+str);
c.receive(str);
System.out.println(c.send());
run();
}catch(Exception e){}
}
public String send()throws java.rmi.RemoteException

{try
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();

return str;
}catch(Exception e)
{
String str1=”some error”;
return str1;
}

}
public void receive(String str)throws java.rmi.RemoteException

{
System.out.println(str);
send();
}
}

—————-

import java.rmi.*;
import java.io.*;
public class ChatServer1
{
public ChatServer1() {

try{
ChatImpl c=new ChatImpl();
Naming.rebind(“//localhost:4444/Chat”,c);
}catch(Exception e){}
}
public static void main(String args[])throws IOException
{
try
{

new ChatServer1();
}catch(Exception e){}
}

}