Web Server
I have an assignment (already overdue so all I need is help getting it right) to create a server that reads from a browser and returns the name of a file I'm accessing.
These are the codes I used
Server
package server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
publicclass Server
{
public Server(int port)
{
try
{
ServerSocket ss =new ServerSocket(port);
while (true)
{
Socket sock = ss.accept();
ServerListener sl =new ServerListener(sock);
sl.start();
}
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}
publicstaticvoid main(String[] args)
{
if (args.length != 1)
{
System.out.println ("Please pass a port");
return;
}
Server cs =new Server(Integer.parseInt(args[0]));
}
}
ServerListener
package server;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.StringTokenizer;
publicclass ServerListenerextends Thread
{
private Socket sock;
public ServerListener(Socket sock)
{
this.sock = sock;
}
publicvoid run()
{
try
{
BufferedReader br =new BufferedReader(new InputStreamReader(sock.getInputStream()));
String line = br.readLine();
StringTokenizer st =new StringTokenizer(line," ");
String file = st.nextToken();
file = st.nextToken();
file = file.replaceAll("/","");
BufferedReader fr =new BufferedReader(new FileReader(file));
String fileLine = fr.readLine();
PrintWriter pw =new PrintWriter(sock.getOutputStream());
while (fileLine !=null)
{
pw.println(fileLine);
pw.flush();
fileLine = fr.readLine();
}
br.close();
fr.close();
pw.close();
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}
}
And a client
package client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
publicclass Clientextends Thread
{
private BufferedReader br;
public Client (String hostname,int port)
{
try
{
Socket sock =new Socket(hostname, port);
this.br =new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintWriter pw =new PrintWriter(sock.getOutputStream());
this.start();
BufferedReader br1 =new BufferedReader(new InputStreamReader(System.in));
String line = br1.readLine();
while (line !=null)
{
pw.println(line);
pw.flush();
line = br1.readLine();
}
}
catch (UnknownHostException uhe)
{
System.out.println("UnknownHostException: " + uhe.getMessage());
}
catch (IOException ioe)
{
System.out.println ("IOException: " + ioe.getMessage());
}
}
publicvoid run()
{
try
{
String line = br.readLine();
while (line !=null)
{
System.out.println (line);
line = br.readLine();
}
}
catch (IOException ioe)
{
System.out.println ("IOException: " + ioe.getMessage());
}
}
publicstaticvoid main(String[] args){
if (args.length != 2){
System.out.println ("Please pass hostname and port");
return;
}
Client cc =new Client(args[0], Integer.parseInt(args[1]));
}
}
In Eclipse, server takes a number as in its argument and client takes a host name and number for its argument.
It connects. I go to my browser. I have a text file in the same folder as the server.
I have 80 as a port for server and localhost 80 for client.
I type in http://localhost:80/, Eclipse throws IOException. I add the text file name to the end of the address, throws IOException, saying the file doesn't exist.
Basically, something is wrong with the code, I just don't know what it is.

