Hello, I have an error on my program, after hours of trial and error I couldn't fix it. I am trying to build a simple server which needs to have threads so more than 1 connection can take place. I don't know if I am doing the threads right, I also get an error on serve.start(); Line 23 compiler says method undefined but I am trying to start a thread.
Any help would be great!
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class EchoServer
{
public static void main(String[] args)
{
ServerSocket serverSock = null;
Socket connSock = null;
SecondThread serve = null;
if( args.length != 1 )
{
System.out.println("Usage: Server port");
System.exit(1);
}
try
{
serverSock = new ServerSocket(Integer.parseInt(args[0]));
while(true)
{
connSock = serverSock.accept();
serve = new SecondThread(connSock);
serve.start();
}
}
catch (IOException e)
{
System.err.println("Server: error on socket");
System.exit(1);
}
}
}
class SecondThread extends Thread
{
Socket conn = null;
public SecondThread(Socket c) { conn = c; }
public void run()
{
while(true)
{
Scanner scanConnection = new Scanner(conn.getInputStream());
String lineString = null;
int lineCount = 0;
String buf[] = new String[32];
String reply = "HTTP/1.0 404 Not Found\r\n" +
"Connection: close\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<h1>Sorry, work in progress</h1>\r\n";
String reply2 = "HTTP/1.0 200 OK\r\n" +
"Connection: close\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" ;
OutputStream outsConnection = conn.getOutputStream();
byte pagebuff[] = new byte[100];
while (true)
{
lineString = scanConnection.nextLine();
if(lineString.length()==0) break;
buf[lineCount] = lineString;
lineCount = lineCount + 1;
}
for(int resfile = 0; resfile < lineCount; resfile++)
{
//System.out.println(buf[i]);
}
String too = buf[128];
Scanner scans = new Scanner(too);
String command = scans.next();
String resource = "http" + scans.next();
String fileName = resource;
//System.out.println(fileName);
File resfile = new File(fileName);
if(!resfile.exists())
{
outsConnection.write(reply.getBytes());
}
else
{
InputStream ins = new FileInputStream(fileName);
outsConnection.write(reply2.getBytes());
while(true)
{
int rc = ins.read(pagebuff, 0, 100);
if(rc <= 0)break;
outsConnection.write(pagebuff, 0, rc);
}
}
conn.close();
}
}
}