Solved

Java Server Syntax error

Posted on 2003-11-20
4
673 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
hibernate example for saving data 19 54
runtime exception 2 42
jsp login check 12 43
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

828 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