Solved

Java Server Syntax error

Posted on 2003-11-20
4
665 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
Comment Utility
If it works please paste in the corrected code.
Cheers
Raftor
0
 

Expert Comment

by:Lavendermpl
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

763 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

6 Experts available now in Live!

Get 1:1 Help Now