java client server quiz. interesting!!!

Reply

Join Date: Oct 2004
Posts: 6
Reputation: Jackie001 is an unknown quantity at this point 
Solved Threads: 0
Jackie001 Jackie001 is offline Offline
Newbie Poster

java client server quiz. interesting!!!

 
0
  #1
Oct 20th, 2004
okay u got me its not interesting at all but i thought if i said that more people would have a look!!!! sorry
im trying to create a server that after the 10th connection of a client reads files and sends to the client, the client then sends back an answer to the question and if correct messages of the prize will be sent to the client.
I had it working to the point of the server sent over the file but the client could not send back. so to fix i made alterations. i tried to implement the server as also a client and the client as also a server so both can send and recieve(is this necessary) and other slight alterations and now the program doesnt display a thing, just 2 blank screens. Tomorrow we have to demonstrate this project and marks will be given on what is displayed, which in my case will be nothing even though i have worked really hard. java tutorials on the web implement guis which just confuse me i cant figure it out. Basically im clutching at straws here. can someone help me please?!
here is the code:
Client
  1. import java.net.* ;
  2. import java.util.* ;
  3. import java.io.* ;
  4.  
  5. public class ReadFileClientApp {
  6.  
  7. public static void main(String args[])
  8. {
  9. try {
  10. InetAddress inet = InetAddress.getLocalHost() ;//gets the address of the host/server
  11. try {
  12. Socket s = new Socket(inet, 2001);//creates socket on port 2011
  13. InputStream in = s.getInputStream();
  14. BufferedReader d = new BufferedReader(new InputStreamReader(in));
  15. OutputStream o = s.getOutputStream() ;
  16. PrintWriter p = new PrintWriter(o) ;///allows to read and write to server
  17.  
  18. String line ;
  19.  
  20. while ((line = d.readLine()) != null)
  21. {
  22. System.out.println(line);//prints input from server
  23. while ((line = d.readLine()) != null)
  24. {
  25. System.out.println(line);//prints input from server
  26. }
  27.  
  28.  
  29.  
  30. ServerSocket server = new ServerSocket(2002) ;//attempt to make client also a server
  31. while (true) {
  32. Socket client = server.accept() ;
  33.  
  34. String answers;
  35. BufferedReader inp= new BufferedReader(new InputStreamReader(System.in));
  36. answers=inp.readLine();
  37. System.out.println(answers);
  38. p.println(answers);
  39. p.flush() ;
  40. line=d.readLine();
  41. System.out.println(line);//sending answer to the questions in one long string to server
  42. }
  43.  
  44. }
  45. }
  46. catch (IOException e)
  47. {
  48. System.out.println("Failed connection to server") ;
  49. }
  50. }
  51. catch (UnknownHostException e) {
  52. System.out.println("Unknown Host ") ;
  53. e.printStackTrace();
  54. }
  55. }
  56. }



Server



  1. import java.net.* ;
  2. import java.util.* ;
  3. import java.io.* ;
  4. import java.math.*;
  5.  
  6.  
  7.  
  8. class createFile ///this just creates the files to be used, skip this stuff
  9. {
  10. public void createFilemain()throws IOException
  11. {
  12. File outputfile = new File("questions.txt");
  13. FileOutputStream fos = new FileOutputStream(outputfile);
  14. PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
  15.  
  16. pw.println(" ");
  17. pw.println("Q1.what is the capital of spain?");
  18. pw.println("a.paris");
  19. pw.println("b.berlin");
  20. pw.println("c.madrid");
  21. pw.println("Q2.who sang 'i shot the sheriff'?");
  22. pw.println("a.elvis");
  23. pw.println("b.bob marley");
  24. pw.println("c.bob dylan");
  25. pw.println("Q3.what language is most widely spoken?");
  26. pw.println("a.spanish");
  27. pw.println("b.english");
  28. pw.println("c.portugese");
  29. pw.println("Q4.who was charles stewart parnell's lover?");
  30. pw.println("a.margaret thatcher");
  31. pw.println("b.kitty o shea");
  32. pw.println("c.bridey mc manus");
  33. pw.println("Q5.where does chess come from?");
  34. pw.println("a.poland");
  35. pw.println("b.india");
  36. pw.println("c.china");
  37. pw.println("Q6.what does the letter 'c' in CIA stand for :");
  38. pw.println("a.criminal");
  39. pw.println("a.clinical");
  40. pw.println("a.convicts");
  41. pw.println("Q7.who won the 1966 world cup?");
  42. pw.println("a.england");
  43. pw.println("b.italy");
  44. pw.println("c.germany");
  45. pw.println("Q8.what language is spoken in brazil");
  46. pw.println("a.spanish");
  47. pw.println("b.portugese");
  48. pw.println("c.french");
  49. pw.println("Q9.who shot john f kennedy(supposidly)?");
  50. pw.println("a.lee harvey oswald");
  51. pw.println("b.elvis preistley");
  52. pw.println("c.john lennon");
  53. pw.println("Q10.In what year was sean lemass reinstated");
  54. pw.println("a.1962");
  55. pw.println("b.1964");
  56. pw.println("c.1966");
  57.  
  58. pw.flush();
  59. pw.close();
  60. }
  61.  
  62. public void createFile1()throws IOException
  63. {
  64. File outputfile = new File("finalq1.txt");
  65. FileOutputStream fos = new FileOutputStream(outputfile);
  66. PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
  67.  
  68. pw.println(" ");
  69. pw.println("You have answered correctly. the final question is ;");
  70. pw.println("Q.where does the pope come from");
  71. pw.println("a.poland");
  72. pw.println("b.italy");
  73. pw.println("c.bulgaria");
  74. pw.flush();
  75. pw.close();
  76. }
  77.  
  78.  
  79. public void createFile2()throws IOException
  80. {
  81. File outputfile = new File("finalq2.txt");
  82. FileOutputStream fos = new FileOutputStream(outputfile);
  83. PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
  84.  
  85. pw.println(" ");
  86. pw.println("You have answered correctly. the final question is ;");
  87. pw.println("Q.what is the name of bob marleys band");
  88. pw.println("a.the wailers");
  89. pw.println("b.the cryers");
  90. pw.println("c.the screamers");
  91. pw.flush();
  92. pw.close();
  93. }
  94.  
  95. public void createFile3()throws IOException
  96. {
  97. File outputfile = new File("finalq3.txt");
  98. FileOutputStream fos = new FileOutputStream(outputfile);
  99. PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
  100.  
  101. pw.println(" ");
  102. pw.println("You have answered correctly. the final question is ;");
  103. pw.println("Q.what is the name of bob marleys back up singers");
  104. pw.println("a.i-threes");
  105. pw.println("b.i-nines");
  106. pw.println("c.hi-fives");
  107. pw.flush();
  108. pw.close();
  109. }///end of files being created
  110. }
  111. public class ReadFileServerApp {
  112.  
  113. public static void main(String args[]) {
  114.  
  115. try {
  116. ServerSocket server = new ServerSocket(2001) ;
  117. int i=0;
  118. while (true) {
  119. Socket client = server.accept() ; //thread used per client socket
  120. i++;
  121. if(i%10==0)
  122. {
  123. calls c= new calls(client);
  124. c.start();
  125. }
  126.  
  127. }
  128. }
  129. catch (IOException e) {
  130. System.out.println("an exception occurred: " + e.getMessage());
  131. e.printStackTrace();
  132. }
  133. }
  134. }
  135.  
  136. class calls extends Thread
  137. {
  138. Socket s;
  139.  
  140. calls(Socket s)
  141. {
  142. this.s=s;
  143. }
  144.  
  145. public void run()
  146. {
  147. try{
  148.  
  149.  
  150. OutputStream o = s.getOutputStream() ;
  151. PrintWriter p = new PrintWriter(o) ;
  152.  
  153. p.println("You are the lucky caller");
  154. createFile c= new createFile();
  155. c.createFilemain();//creating the questions file
  156. String file="questions.txt";
  157. String ans;
  158. int j=0;
  159.  
  160. while(j<3)
  161. {
  162. FileHandler h = new FileHandler(s, file) ;
  163. h.start() ;//call filehandler to pass files to client
  164. j++;//increment every time a file is passed
  165.  
  166. try {
  167. InetAddress inet = InetAddress.getLocalHost() ;
  168. try {
  169. Socket sc = new Socket(inet, 2002);
  170. InputStream inc = sc.getInputStream();
  171. BufferedReader dc = new BufferedReader(new InputStreamReader(inc));
  172. ans = dc.readLine();
  173.  
  174. System.out.println(ans) ;
  175.  
  176. if(j==1)//for the first file
  177. {
  178.  
  179. if (ans=="cbabbaabac")//if answer to first file is correct
  180. {
  181.  
  182. int fnum;
  183. fnum= 1+((int)(Math.random()*3));
  184.  
  185. switch (fnum)
  186. {
  187. case 1: file="finalq1.txt";
  188. createFile c1= new createFile();
  189. c1.createFile1();
  190. break;
  191. case 2: file="finalq2.txt";
  192. createFile c2= new createFile();
  193. c2.createFile2();
  194. break;
  195. case 3: file="finalq3.txt";
  196. createFile c3= new createFile();
  197. c3.createFile3();
  198. break;
  199. default: file="finalq1.txt";
  200. createFile c4= new createFile();
  201. c4.createFile1();
  202. break;
  203. }
  204. }
  205. else
  206. {
  207. p.println("Unlucky, try again");
  208. j=3;
  209. }
  210. }
  211.  
  212. if(j==2)//the second time a file is called i.e the final q
  213. {
  214. if (ans=="a")
  215. p.println("You have won $10,000, congratulations");
  216. else
  217. p.println("You have answered incorrectly. your prize is a tv");
  218. j=3;
  219. }
  220.  
  221. }
  222. catch (IOException e)
  223. {
  224. System.out.println("Failed connection to server") ;
  225. }
  226. }
  227. catch (UnknownHostException e) {
  228. System.out.println("Unknown Host ") ;
  229. e.printStackTrace();
  230. }
  231. }
  232.  
  233. }
  234. catch (IOException e) {
  235. System.out.println("an exception occurred: " + e.getMessage());
  236. e.printStackTrace();
  237. }
  238. }
  239. }
  240.  
  241.  
  242.  
  243. class FileHandler extends Thread {
  244. Socket s ;
  245. String file;
  246.  
  247. FileHandler(Socket s, String file) {
  248. this.s = s ;
  249. this.file=file;
  250. }
  251.  
  252. public void run() {
  253. try {
  254.  
  255. OutputStream o = s.getOutputStream() ;
  256. PrintWriter p = new PrintWriter(o) ;
  257.  
  258. FileInputStream fin = new FileInputStream(file) ;
  259. BufferedReader f = new BufferedReader(new InputStreamReader(fin));
  260.  
  261. String line ;
  262. while ((line = f.readLine()) != null) {
  263. p.println(line);
  264. }
  265.  
  266.  
  267.  
  268. }
  269. catch (IOException e) {
  270. System.out.println("an exception occurred: " + e.getMessage());
  271. e.printStackTrace();
  272. }
  273. }
  274. }



Thanks to anyone who takes the time to read this or has a go.
p.s. i did a good deed and helped out someone here with their problem!!!
Last edited by alc6379; Oct 23rd, 2004 at 4:29 am. Reason: added code tags for clarity
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC