•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,900 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,321 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 4257 | Replies: 0
![]() |
•
•
Join Date: Oct 2004
Location: ireland
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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
Server
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!!!
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
import java.net.* ;
import java.util.* ;
import java.io.* ;
public class ReadFileClientApp {
public static void main(String args[])
{
try {
InetAddress inet = InetAddress.getLocalHost() ;//gets the address of the host/server
try {
Socket s = new Socket(inet, 2001);//creates socket on port 2011
InputStream in = s.getInputStream();
BufferedReader d = new BufferedReader(new InputStreamReader(in));
OutputStream o = s.getOutputStream() ;
PrintWriter p = new PrintWriter(o) ;///allows to read and write to server
String line ;
while ((line = d.readLine()) != null)
{
System.out.println(line);//prints input from server
while ((line = d.readLine()) != null)
{
System.out.println(line);//prints input from server
}
ServerSocket server = new ServerSocket(2002) ;//attempt to make client also a server
while (true) {
Socket client = server.accept() ;
String answers;
BufferedReader inp= new BufferedReader(new InputStreamReader(System.in));
answers=inp.readLine();
System.out.println(answers);
p.println(answers);
p.flush() ;
line=d.readLine();
System.out.println(line);//sending answer to the questions in one long string to server
}
}
}
catch (IOException e)
{
System.out.println("Failed connection to server") ;
}
}
catch (UnknownHostException e) {
System.out.println("Unknown Host ") ;
e.printStackTrace();
}
}
}
Server
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();
}
}
}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 3:29 am. Reason: added code tags for clarity
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
ad advertising architecture backup beta business centro collaboration tools com crash daniweb database dell development doubleclick emai security enterprise google internet java linux longhorn mail mail client malware microsoft microsoft sdk for java microsystems mmorpg news novell open outlook platform pop programming qmail red hat rhel server smtp software source sun suse web windows windows server
- Java Client/Server (Java)
- URGENT: FTP Client / Server using RMI (Java)
- chat server gui (Java)
- Problem with Client-Server Socket Connection (Java)
- Simple Client Server Instant Messaging Java code using UDP datagrams (Java)
- Client Server Applications (Pascal and Delphi)
- Client-server vs RMI using Java (Java)
Other Threads in the Java Forum
- Previous Thread: I have 1 error and I can't fix it
- Next Thread: Cannot incorporate equals method for a yes or no answer.


Linear Mode