You must implement an Internet chat room with the following components:
There are the following commands:
This is the initial request to join the chatroom with particular username.
This sends a message only to the user called `user'.
This stops the client displaying messages from `user'.
This blocks all users.
This unblocks `user'.
This unblocks all users
This means broadcast messages will be sent only to user1 ... usern
This clears the broadcast list.
This adds user to the broadcast list.
This removes user from the broadcast list
This will display the current status of the client, including who is in the broadcast list and how long the client has been up.
This closes the client gracefully.
Mischevious feauture: The administrator can send messages pretending to be other users!
Some useful code done in class:
class firstPart
{
class Message
{
int messageType;
String username;
String message;
Message(int i, String m, String u )
{
messageType=i;
username=u;
message=m;
}
}
static String fp (String s)
{
String result="";
for (int i=0;i<s.length();i++)
{
if (s.charAt(i)==' ') return result;
else result=result+s.charAt(i);
}
return result;
}
static Message parseMessage(String s)
{
if ((fp(s).equals("!join")) return new Message(0,"",secondPart(s));
else if (s.equals("!block")) return new Message(4,"","");
else etc. etc.
}
public static void main(String [] args)
{
System.out.println(fp(args[0]));
if (fp(args[0]).equals("!join")) System.out.println("WHOOOPEEEE!");
}
}
Write a complete Java program that takes a url and a domain name as command line arguments that crawls recursively through all urls starting at args[0] finding all the broken links. It only checks web-pages in the domain given by args[1]. It does however check whether links to urls outside the current domain are broken. The broken links are stored in a database table. The first field of the table is the url of the page containing the broken link and the second field is the url of the broken link. Once all broken links have been found, they are printed out.
import java.io.*;
import java.net.*;
class evenSimplerEchoServer
{
public static void main(String[] argv) throws Exception
{ServerSocket s = new ServerSocket(5000);
Socket t = s.accept();//wait for client to connect
InputStream b = t.getInputStream();
OutputStream p =t.getOutputStream();
int c;
while((c=b.read())!=-1) {
p.write(c);
p.flush();
System.out.print((char) c);
}
}
}
import java.io.*;
import java.net.*;
class evenSimplerEchoClient
{
public static void main(String[] argv) throws Exception
{Socket s = new Socket("localhost",5000);
OutputStream p =s.getOutputStream();
InputStream i = s.getInputStream();
InputStreamReader b = new InputStreamReader(System.in);
int c;
while((c=b.read())!=-1) {
p.write(c);
p.flush();
System.out.print((char)i.read());
}
}
}
class p
{
void f()
{ while (true) System.out.println("red");}
void g()
{ while (true) System.out.println("green");}
}
class t1 extends Thread
{ p x;
t1(p y)
{x=y;}
public void run()
{x.g();}
}
class t2 extends Thread
{ p x;
t2(p y){x=y;}
public void run()
{x.f();}
}
class z
{
public static void main(String[] argv)
{
p it= new p();
new t2(it).start();
new t1(it).start();
}
}
What happens?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class evenSimplerGuiClient implements ActionListener {
private JTextField user = new JTextField("user",20);
private JTextArea server = new JTextArea("server",5,20);
private JScrollPane sp =new JScrollPane(server);
private Socket s;
private OutputStreamWriter p;
private InputStream i;
private JFrame window = new JFrame("client");
class serverReader extends Thread
{
public void run()
{
String s="";
int c;
try
{
while ((c=i.read())!=-1)
{
s=s+ ((char)c);
server.setText(s);
}
}
catch(Exception e){};
}
}
public evenSimplerGuiClient() throws Exception
{
try
{
s = new Socket("localhost",5000);
p =new OutputStreamWriter(s.getOutputStream());
i = s.getInputStream();
new serverReader().start();
}
catch (Exception e){System.out.println("error");};
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new FlowLayout());
window.add(sp);
window.add(user);
user.addActionListener(this);
window.setVisible(true);
}
public void actionPerformed(ActionEvent a)
{
String s= user.getText();
try
{
p.write(s+'\n',0,s.length()+1);
p.flush();user.setText("");
}
catch (Exception e){};
}
public static void main(String[] args) throws Exception
{
new evenSimplerGuiClient();
}
}
import java.io.*;
import java.net.*;
class simpleMultiThreadedEchoServer
{
public static void main(String[] argv) throws Exception
{
ServerSocket s = new ServerSocket(5000);
Transaction k;
while (true)
{
k = new Transaction(s.accept());
k.start();
}
}
}
class Transaction extends Thread
{
InputStream b;
OutputStream p;
public Transaction(Socket s) throws Exception
{
b=s.getInputStream();
p =s.getOutputStream();
}
public void run()
{
int c;
try
{
while((c=b.read())!=-1)
{
p.write((char)c);
p.flush();
System.out.print((char)c);
}
}
catch (Exception e)
{
}
}
}
import java.util.*;
import java.io.*;
import java.net.*;
class SynchList
{
ArrayList <OutputStream> it;
SynchList()
{
it=new ArrayList <OutputStream> ();
}
synchronized OutputStream get(int i)
{
return it.get(i);
}
synchronized void add(OutputStream o)
{
it.add(o);
}
synchronized int size()
{
return it.size();
}
}
class broadcasterWithList
{
static SynchList Outputs= new SynchList();
static int i=0;
public static void main(String[] argv) throws Exception
{ServerSocket s = new ServerSocket(5000);
Transaction k;
while (true) {k = new Transaction(i,s.accept(),Outputs);k.start();i++;
System.out.println("client joined");}//wait for client to connect
}
}
class Transaction extends Thread
{
SynchList outputs;
int n;
Socket t;
InputStream b;
OutputStream p;
public Transaction(int i,Socket s, SynchList v) throws Exception
{
outputs=v;
n=i;t=s; b = t.getInputStream();
p =t.getOutputStream();
outputs.add(p);
}
public void run()
{
int c;
try{
while((c=b.read())!=-1)
{
for (int j=0;j<outputs.size();j++)
{
if (j!=n)
{
(outputs.get(j)).write(c);
(outputs.get(j)).flush();
}
}
System.out.print((char)c);
System.out.print("size of ArrayList :"+outputs.size());
}
System.out.print("left loop");
}
catch (Exception e)
{ System.out.print(e);}
}
}
class A
{
static int a=0;
static void update()
{
a++;
}
static void print()
{
System.out.println(a);
}
}
class test
{
static class t1 extends Thread
{
public void run()
{
for (int i=0;i<100;i++) {A.update();A.print();}
}
}
static class t2 extends Thread
{
public void run()
{
for (int i=0;i<100;i++) {A.update();A.print();}
}
}
public static void main(String [] args)
{
new t1().start(); new t2().start();
}
}
class A
{
static int a=0;
synchronized static void update()
{
a++;
}
synchronized static void print() //put synchronized
{
System.out.println(a);
}
}
class test1
{
static class t1 extends Thread
{
public void run()
{
for (int i=0;i<100;i++) {A.update();A.print();}
}
}
static class t2 extends Thread
{
public void run()
{
for (int i=0;i<100;i++) {A.update();A.print();}
}
}
public static void main(String [] args)
{
new t1().start(); new t2().start();
}
}
class A
{
static int a=0;
synchronized static void update()
{
a++;System.out.println(a);
}
}
class test2
{
static class t2 extends Thread
{
public void run()
{
for (int i=0;i<100;i++) {A.update();}
}
}
static class t2 extends Thread
{
public void run()
{
for (int i=0;i<100;i++) {A.update();}
}
}
public static void main(String [] args)
{
new t1().start(); new t2().start();
}
}
import java.util.*;
import java.io.*;
import java.net.*;
class SynchList
{
ArrayList <OutputStream> it;
SynchList()
{
it=new ArrayList <OutputStream> ();
}
synchronized OutputStream get(int i)
{
return it.get(i);
}
synchronized void add(OutputStream o)
{
it.add(o);
}
synchronized int size()
{
return it.size();
}
synchronized void remove(int i)
{
it.remove(i);
}
}
class broadcasterWithListest1
{
static SynchList Outputs= new SynchList();
static int i=0;
public static void main(String[] argv) throws Exception
{ServerSocket s = new ServerSocket(5000);
Transaction k;
while (true) {k = new Transaction(Outputs.size(),s.accept(),Outputs);k.start();
System.out.println("client joined");}//wait for client to connect
}
}
class Transaction extends Thread
{
SynchList outputs;
int n;
Socket t;
InputStream b;
OutputStream p;
public Transaction(int i,Socket s, SynchList v) throws Exception
{
outputs=v;
n=i;t=s; b = t.getInputStream();
p =t.getOutputStream();
outputs.add(p);
}
public void run()
{
int c;
try{
while((c=b.read())!=-1)
{
for (int j=0;j<outputs.size();j++)
{
//if (j!=n)
{
(outputs.get(j)).write(c);
(outputs.get(j)).flush();
}
}
System.out.print((char)c);
// System.out.print("size of ArrayList :"+outputs.size());
}
System.out.print("client " + n + " left loop");
outputs.remove(n);
}
catch (Exception e)
{ System.out.print(e);}
}
}
args[2] and then sends this data to and receives data from a server which is listening on port args[1] of host args[0].
import java.io.*;
import java.net.*;
public class SebMultiProxy {
public static void main(String[] args) throws Exception {
String host = args[0];
int remoteport = Integer.parseInt(args[1]);
int localport = Integer.parseInt(args[2]);
System.out.println("Starting proxy for " + host + ":" + remoteport
+ " on port " + localport);
ServerSocket s = new ServerSocket(localport);
while (true) {
Socket cl=s.accept();
Socket se= new Socket(host,remoteport);
fromClientToServer k = new fromClientToServer(cl,se);k.start();
fromClientToServer m = new fromClientToServer(se,cl);m.start();
}
}
}
class fromClientToServer extends Thread
{
InputStream b;
OutputStream p;
public fromClientToServer(Socket c, Socket s) throws Exception
{
b = c.getInputStream();
p =s.getOutputStream();
}
public void run()
{
int c;
try{
while((c=b.read())!=-1) {p.write(c);p.flush();}
}
catch (Exception e)
{}
}
}
j!=n. Explain what happens.
ma323.gold.ac.uk is behind a firewall but
igor.gold.ac.uk
has port 5000-10000 open. How can you use the simple proxy server above to connect to
a webserver listening on port 80 of ma323.gold.ac.uk.
(pick a free port on igor)
import java.io.*;
public class Student implements Serializable
{
String name;
int mark;
public Student (String n, int a)
{
mark=a;name=n;
}
public String toString()
{
return name+" "+age;
}
}
class objectClient1
{
public static void main(String[] argv) throws Exception
{Socket s = new Socket("localhost",5000);
ObjectOutputStream p =new ObjectOutputStream(s.getOutputStream());
ObjectInputStream q =new ObjectInputStream(s.getInputStream());
Scanner b = new Scanner(System.in);
int c;
while(b.hasNext()) {
String name=b.nextLine();
int mark=Integer.parseInt(b.nextLine());
p.writeObject(new Student(name,mark));
p.flush();
System.out.println(q.readObject());
}
}
}
import java.io.*;
import java.net.*;
class objectEchoServer
{
public static void main(String[] argv) throws Exception
{ServerSocket s = new ServerSocket(5000);
Socket t = s.accept();//wait for client to connect
System.out.println("server connected");
ObjectInputStream b = new ObjectInputStream(t.getInputStream());
ObjectOutputStream q = new ObjectOutputStream(t.getOutputStream());
Object c;
while((c=b.readObject())!=null) {
q.writeObject(c);
}
}
}
objectClient1 above.
Student Object whose name
is `end' and whose mark is 0.
import java.sql.*;
public class seb5 {
public static void main(String[] args) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection connect=
DriverManager.getConnection("jdbc:mysql://localhost/art55","mas01sd","sebastian");
Statement st = connect.createStatement();
st.executeUpdate("INSERT INTO one VALUES('" + args[0] +"','" + args[1] + "');");
ResultSet resultSet = st.executeQuery("SELECT * from one");
while (resultSet.next())
{
for (int i=1;i<3;i++)System.out.print(resultSet.getString(i) + " ");
System.out.println();
}
}
}
javac -cp .:mysql-connector-java-5.0.8-bin.jar seb5.java
and to run:
For example:
java -cp .:mysql-connector-java-5.0.8-bin.jar seb5
if (x instanceOf Student) ... for this.)
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class SebLinks
{
public static void main(String[] args) throws IOException
{
String url = args[0];
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href]");
for (Element link : links) System.out.println(link.attr("abs:href"));
}
}
javac -cp jsoup-1.6.1.jar SebLinks.java
To run do
javac-cp .:jsoup-1.6.1.jar SebLinks http://localhost(or something else apart from
http://localhost.)
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashSet.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class NewSpider
{
static ArrayList<String> listLinks (String url)
{
ArrayList<String> a= new ArrayList<String>();
try{org.jsoup.Connection z=Jsoup.connect(url);
Document doc = z.get();
Elements links = doc.select("a[href]");
for (Element link : links) a.add(link.attr("abs:href"));
}
catch (Exception e)
{
}
return a;
}
static HashSet <String> listToSet (ArrayList <String> m)
{
HashSet <String> S=new HashSet <String> ();
for (String t : m) S.add(t);
return S;
}
static HashSet <String> links (String url)
{
return listToSet(listLinks(url));
}
static void Spider (String url, int n)
{
HashSet<String> alreadyVisited = new HashSet <String> ();
HashSet<String> toVisit = new HashSet <String> ();
toVisit.addAll(links(url));
alreadyVisited.add(url);
int i=0;
while (i<n && !toVisit.isEmpty())
{
String z= toVisit.iterator().next();
boolean already=alreadyVisited.contains(z);
if (already) toVisit.remove(z);
else
{
System.out.println(z);
HashSet <String> k= links(z);
toVisit.addAll(k);
alreadyVisited.add(z);
i++;
}
}
}
public static void main(String[] args) throws IOException
{
String url = args[0];
Spider(url,100);
}
}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashSet.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class NewSpider1
{
static ArrayList<String> listLinks (String url)
{
ArrayList<String> a= new ArrayList<String>();
try{org.jsoup.Connection z=Jsoup.connect(url);
Document doc = z.get();
Elements links = doc.select("a[href]");
for (Element link : links) a.add(link.attr("abs:href"));
}
catch (Exception e)
{
}
return a;
}
static HashSet <String> listToSet (ArrayList <String> m)
{
HashSet <String> S=new HashSet <String> ();
for (String t : m) S.add(t);
return S;
}
static HashSet <String> links (String url)
{
return listToSet(listLinks(url));
}
static void Spider (String url, int n, String contains)
{
HashSet<String> alreadyVisited = new HashSet <String> ();
HashSet<String> toVisit = new HashSet <String> ();
toVisit.addAll(links(url));
alreadyVisited.add(url);
int i=0;
while (i<n && !toVisit.isEmpty())
{
String z= toVisit.iterator().next();
boolean already=alreadyVisited.contains(z);
if (already) toVisit.remove(z);
else
{
if (z.contains(contains))
{
System.out.println(z);
HashSet <String> k= links(z);
toVisit.addAll(k);
}
alreadyVisited.add(z);
i++;
}
}
}
public static void main(String[] args) throws IOException
{
String url = args[0];
Spider(url,100,args[1]);
}
}
mport java.util.ArrayList;
import java.util.HashSet;
import java.util.HashSet.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class Brokens
{
static boolean broken(String url)
{
try {Jsoup.connect(url).get(); return false;}
catch (java.net.MalformedURLException e) {return true;}
catch (IOException e){if (e.toString().contains("java.io.IOException: 404"))
{
return true;
return false; //this means it exists but isn't html
}
catch(Exception e) {
return false;
//for any other errror assume not broken - this is a guess
}
}
static ArrayList<String> listLinks (String url)
{
ArrayList<String> a= new ArrayList<String>();
try{org.jsoup.Connection z=Jsoup.connect(url);
Document doc = z.get();
Elements links = doc.select("a[href]");
for (Element link : links) a.add(link.attr("abs:href"));
}
catch (Exception e)
{
}
return a;
}
static HashSet <String> listToSet (ArrayList <String> m)
{
HashSet <String> S=new HashSet <String> ();
for (String t : m) S.add(t);
return S;
}
static HashSet <String> links (String url)
{
return listToSet(listLinks(url));
}
static void Spider (String url, int n, String contains)
{
HashSet<String> alreadyVisited = new HashSet <String> ();
HashSet<String> toVisit = new HashSet <String> ();
toVisit.addAll(links(url));
alreadyVisited.add(url);
int i=0;
while (i<n && !toVisit.isEmpty())
{
String z= toVisit.iterator().next();
boolean already=alreadyVisited.contains(z);
if (already) toVisit.remove(z);
else
{
if (z.contains(contains))
{
System.out.println(z);
HashSet <String> k= links(z);
toVisit.addAll(k);
}
alreadyVisited.add(z);
i++;
}
}
for (String k:alreadyVisited)
if (broken(k)) System.out.println("Broken: " +k);
}
public static void main(String[] args) throws IOException
{
String url = args[0];
Spider(url,100,args[1]);
}
}