Solved

Java Server Syntax error

Posted on 2003-11-20
4
675 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 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