import java.net.* ;
import java.util.* ;
import java.io.* ;
import java.math.*;
class createFile ///this just creates the files to be used, skip this stuff
{
public void createFilemain()throws IOException
{
File outputfile = new File("questions.txt");
FileOutputStream fos = new FileOutputStream(outputfile);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
pw.println(" ");
pw.println("Q1.what is the capital of spain?");
pw.println("a.paris");
pw.println("b.berlin");
pw.println("c.madrid");
pw.println("Q2.who sang 'i shot the sheriff'?");
pw.println("a.elvis");
pw.println("b.bob marley");
pw.println("c.bob dylan");
pw.println("Q3.what language is most widely spoken?");
pw.println("a.spanish");
pw.println("b.english");
pw.println("c.portugese");
pw.println("Q4.who was charles stewart parnell's lover?");
pw.println("a.margaret thatcher");
pw.println("b.kitty o shea");
pw.println("c.bridey mc manus");
pw.println("Q5.where does chess come from?");
pw.println("a.poland");
pw.println("b.india");
pw.println("c.china");
pw.println("Q6.what does the letter 'c' in CIA stand for :");
pw.println("a.criminal");
pw.println("a.clinical");
pw.println("a.convicts");
pw.println("Q7.who won the 1966 world cup?");
pw.println("a.england");
pw.println("b.italy");
pw.println("c.germany");
pw.println("Q8.what language is spoken in brazil");
pw.println("a.spanish");
pw.println("b.portugese");
pw.println("c.french");
pw.println("Q9.who shot john f kennedy(supposidly)?");
pw.println("a.lee harvey oswald");
pw.println("b.elvis preistley");
pw.println("c.john lennon");
pw.println("Q10.In what year was sean lemass reinstated");
pw.println("a.1962");
pw.println("b.1964");
pw.println("c.1966");
pw.flush();
pw.close();
}
public void createFile1()throws IOException
{
File outputfile = new File("finalq1.txt");
FileOutputStream fos = new FileOutputStream(outputfile);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
pw.println(" ");
pw.println("You have answered correctly. the final question is ;");
pw.println("Q.where does the pope come from");
pw.println("a.poland");
pw.println("b.italy");
pw.println("c.bulgaria");
pw.flush();
pw.close();
}
public void createFile2()throws IOException
{
File outputfile = new File("finalq2.txt");
FileOutputStream fos = new FileOutputStream(outputfile);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
pw.println(" ");
pw.println("You have answered correctly. the final question is ;");
pw.println("Q.what is the name of bob marleys band");
pw.println("a.the wailers");
pw.println("b.the cryers");
pw.println("c.the screamers");
pw.flush();
pw.close();
}
public void createFile3()throws IOException
{
File outputfile = new File("finalq3.txt");
FileOutputStream fos = new FileOutputStream(outputfile);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
pw.println(" ");
pw.println("You have answered correctly. the final question is ;");
pw.println("Q.what is the name of bob marleys back up singers");
pw.println("a.i-threes");
pw.println("b.i-nines");
pw.println("c.hi-fives");
pw.flush();
pw.close();
}///end of files being created
}
public class ReadFileServerApp {
public static void main(String args[]) {
try {
ServerSocket server = new ServerSocket(2001) ;
int i=0;
while (true) {
Socket client = server.accept() ; //thread used per client socket
i++;
if(i%10==0)
{
calls c= new calls(client);
c.start();
}
}
}
catch (IOException e) {
System.out.println("an exception occurred: " + e.getMessage());
e.printStackTrace();
}
}
}
class calls extends Thread
{
Socket s;
calls(Socket s)
{
this.s=s;
}
public void run()
{
try{
OutputStream o = s.getOutputStream() ;
PrintWriter p = new PrintWriter(o) ;
p.println("You are the lucky caller");
createFile c= new createFile();
c.createFilemain();//creating the questions file
String file="questions.txt";
String ans;
int j=0;
while(j<3)
{
FileHandler h = new FileHandler(s, file) ;
h.start() ;//call filehandler to pass files to client
j++;//increment every time a file is passed
try {
InetAddress inet = InetAddress.getLocalHost() ;
try {
Socket sc = new Socket(inet, 2002);
InputStream inc = sc.getInputStream();
BufferedReader dc = new BufferedReader(new InputStreamReader(inc));
ans = dc.readLine();
System.out.println(ans) ;
if(j==1)//for the first file
{
if (ans=="cbabbaabac")//if answer to first file is correct
{
int fnum;
fnum= 1+((int)(Math.random()*3));
switch (fnum)
{
case 1: file="finalq1.txt";
createFile c1= new createFile();
c1.createFile1();
break;
case 2: file="finalq2.txt";
createFile c2= new createFile();
c2.createFile2();
break;
case 3: file="finalq3.txt";
createFile c3= new createFile();
c3.createFile3();
break;
default: file="finalq1.txt";
createFile c4= new createFile();
c4.createFile1();
break;
}
}
else
{
p.println("Unlucky, try again");
j=3;
}
}
if(j==2)//the second time a file is called i.e the final q
{
if (ans=="a")
p.println("You have won $10,000, congratulations");
else
p.println("You have answered incorrectly. your prize is a tv");
j=3;
}
}
catch (IOException e)
{
System.out.println("Failed connection to server") ;
}
}
catch (UnknownHostException e) {
System.out.println("Unknown Host ") ;
e.printStackTrace();
}
}
}
catch (IOException e) {
System.out.println("an exception occurred: " + e.getMessage());
e.printStackTrace();
}
}
}
class FileHandler extends Thread {
Socket s ;
String file;
FileHandler(Socket s, String file) {
this.s = s ;
this.file=file;
}
public void run() {
try {
OutputStream o = s.getOutputStream() ;
PrintWriter p = new PrintWriter(o) ;
FileInputStream fin = new FileInputStream(file) ;
BufferedReader f = new BufferedReader(new InputStreamReader(fin));
String line ;
while ((line = f.readLine()) != null) {
p.println(line);
}
}
catch (IOException e) {
System.out.println("an exception occurred: " + e.getMessage());
e.printStackTrace();
}
}
}