?
Solved

Java Server Syntax error

Posted on 2003-11-20
4
Medium Priority
?
678 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 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