Solved

Java Server Syntax error

Posted on 2003-11-20
4
671 Views
Last Modified: 2012-08-13
Below is a code for a simple web server that serves one file.  I am having problems compiling this code as i get 3 error messages i cant resolve.   The error message is

"C:\Sean Duignan\oneFile.java:97: illegal start of expression
      public static void oneFile(Socket s)
                        ^
C:\Sean Duignan\oneFile.java:131: ';' expected
                  }
                         ^
C:\Sean Duignan\oneFile.java:79: cannot resolve symbol
symbol  : constructor oneFile  (java.net.Socket)
location: class oneFile
            oneFile fs = new oneFile(ss.accept());
                                                     ^
Note: C:\Sean Duignan\oneFile.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
3 errors"

*******************************************************************
Please copy the code below into a text document, save as a java file and try to compile to see the error for yourself.  If you can fix the code it will be greatly appreciated.
*****************************************************************

//A web server that caches one file and then serves it to any client
//that connects - each connection is managed in a separate thread

import java.net.*;
import java.io.*;
import java.util. *;

public class oneFile extends Thread
{
      static String theData;
      static String ContentType;
      static int theConnection;
      
      public static void main(String[] args)
      {
            int thePort;
            ServerSocket ss;
            Socket theConnection;
            FileInputStream theFile;
            
            //cache the file and "guess" the content type
            
            try
            {
                  theFile = new FileInputStream(args[0]);
                  DataInputStream dis = new DataInputStream(theFile);
                  if(args[0].endsWith(".html") || args[0].endsWith(".htm"))
                  {
                        ContentType = "text/html";
                  }
                  else
                  {
                        ContentType = "text/plain";
                  }
                  
                  //This is where we cache the File
                  try
                  {
                        String thisLine;
                        while((thisLine = dis.readLine()) != null)
                        {
                              theData += thisLine + "\n";
                        }
                  }
                  catch(Exception e)
                  {
                        System.err.println("Error " + e);
                  }
                  catch(Exception e)
                  {
                        System.err.println(e);
                        System.err.println("Usage: java onefile filename port");
                        System.exit(1);
                  }
                  
                  //Set the port to listen on
                  
                  try
                  {
                        thePort = Integer.parseInt(args[1]);
                        if(thePort < 0 ||thePort > 65535) thePort = 80;
                  }
                  catch(Exception e)
                  {
                        thePort = 80;
                  }
                  
                  try
                  {
                        ss = new ServerSocket(thePort);
                        System.out.println("Acception connections on port " + ss.getLocalPort());
                        System.out.println("Data to be sent: ");
                        System.out.println(theData);
                        
                        //NB
                        //Spawn a new thread for each connection
                        while(true)
                        {
                              oneFile fs = new oneFile(ss.accept());
                              fs.start();  //start calls run()
                        }
                  
                  }catch(UnknownHostException e){
                                    System.err.println(e);
                  }
                  catch (SocketException e)
                  {
                        System.err.println("could not connect");
                  }
                  catch(IOException e)
                  {
                        System.err.println(e);
                  }
                  
                  //Constructor and run method of the thread
                  
                  public oneFile(Socket s)
                  {
                        theConnection = s;
                  }
                  
                  public void run()
                  {
                        try
                        {
                              PrintStream os = new PrintStream(theConnection.getOutputStream());
                              DataInputStream is = new DataInputStream(theConnection.getInputStream());
                              String request = is.readLine();
                              //if this is HTTP/1.0 or later send a MIME header
                              if(request.indexOf("HTTP") != -1)
                              {
                                    while(true)//read the rest of the MIME header
                                    {
                                          String thisLine = is.readLine();
                                          if(thisLine.trim().equals(""))
                                                break;
                                    }
                                    os.print("HTTP/1.0 200 OK\r\n");
                                    Date now = new Date();
                                    os.print("Date: " + now + "\r\n");
                                    os.print("Server: OneFile 1.0\r\n");
                                    os.print("Content-length: " + ContentLength + "\r\n");
                                    os.print("Content-type: " + ContentType + "\r\n\r\n");
                              }
                              os.println(theData);
                              theConnection.close();
                        }//end try
                        catch(IOException e)
                        {
                        }
                  }
            }
            catch(IOException e)
            {
            }
      }
}
                  
                  
                  
                  
      
0
Comment
Question by:Raftor
  • 2
4 Comments
 

Author Comment

by:Raftor
ID: 9792397
If it works please paste in the corrected code.
Cheers
Raftor
0
 

Expert Comment

by:Lavendermpl
ID: 9792668
Your structure which in code is wrong.

The constructor must under the class, not under main method.

Following code is correct sample:
========================================
public class oneFile {
    public oneFile(Socket s) {}

    public static void main(String[] args) {
        oneFile onefile = new oneFile(new Socket());
    }
}
========================================
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 9792670
the following fixes those errors but you have more:

import java.net.*;
import java.io.*;
import java.util. *;

public class oneFile extends Thread
{
     static String theData;
     static String ContentType;
     static Socket theConnection;
     
     public static void main(String[] args)
     {
          int thePort;
          ServerSocket ss;
          Socket theConnection;
          FileInputStream theFile;
         
          //cache the file and "guess" the content type
         
          try
          {
               theFile = new FileInputStream(args[0]);
               DataInputStream dis = new DataInputStream(theFile);
               if(args[0].endsWith(".html") || args[0].endsWith(".htm"))
               {
                    ContentType = "text/html";
               }
               else
               {
                    ContentType = "text/plain";
               }
               
               //This is where we cache the File
               try
               {
                    String thisLine;
                    while((thisLine = dis.readLine()) != null)
                    {
                         theData += thisLine + "\n";
                    }
               }
               catch(Exception e)
               {
                    System.err.println("Error " + e);
               }
               catch(Exception e)
               {
                    System.err.println(e);
                    System.err.println("Usage: java onefile filename port");
                    System.exit(1);
               }
               
               //Set the port to listen on
               
               try
               {
                    thePort = Integer.parseInt(args[1]);
                    if(thePort < 0 ||thePort > 65535) thePort = 80;
               }
               catch(Exception e)
               {
                    thePort = 80;
               }
               
               try
               {
                    ss = new ServerSocket(thePort);
                    System.out.println("Acception connections on port " + ss.getLocalPort());
                    System.out.println("Data to be sent: ");
                    System.out.println(theData);
                   
                    //NB
                    //Spawn a new thread for each connection
                    while(true)
                    {
                         oneFile fs = new oneFile(ss.accept());
                         fs.start();  //start calls run()
                    }
               
               }catch(UnknownHostException e){
                              System.err.println(e);
               }
               catch (SocketException e)
               {
                    System.err.println("could not connect");
               }
               catch(IOException e)
               {
                    System.err.println(e);
               }
          }
          catch(IOException e)
          {
          }
     }
       
           //Constructor and run method of the thread
           
           public oneFile(Socket s)
           {
                theConnection = s;
           }
           
           public void run()
           {
                try
                {
                     PrintStream os = new PrintStream(theConnection.getOutputStream());
                     DataInputStream is = new DataInputStream(theConnection.getInputStream());
                     String request = is.readLine();
                     //if this is HTTP/1.0 or later send a MIME header
                     if(request.indexOf("HTTP") != -1)
                     {
                          while(true)//read the rest of the MIME header
                          {
                               String thisLine = is.readLine();
                               if(thisLine.trim().equals(""))
                                    break;
                          }
                          os.print("HTTP/1.0 200 OK\r\n");
                          Date now = new Date();
                          os.print("Date: " + now + "\r\n");
                          os.print("Server: OneFile 1.0\r\n");
                          os.print("Content-length: " + ContentLength + "\r\n");
                          os.print("Content-type: " + ContentType + "\r\n\r\n");
                     }
                     os.println(theData);
                     theConnection.close();
                }//end try
                catch(IOException e)
                {
                }
           }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9793870
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
backtracking recursion  code 19 57
Java Timer (static) 9 43
oracle 11g 23 73
servlet example 11 40
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now