Hi, I am very new to java and trying to make this server multithreaded to handle repeated concurrent requests. I would a new thread to handle each connection.
Help would be great
Many Thanks
import java.io.*;
import java.net.*;
import java.util.*;
class Web extends Thread
{
public static void main(String[] args) throws Exception
{
String requestMessageLine;
String fileName;
ServerSocket listenSocket = new ServerSocket(8005);
while(true){
Socket connectionSocket = listenSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(
connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(
connectionSocket.getOutputStream());
requestMessageLine = inFromClient.readLine();
StringTokenizer tokenizedLine =
new StringTokenizer(requestMessageLine);
if (tokenizedLine.nextToken().equals("GET"
)
{
fileName = tokenizedLine.nextToken();
if ( fileName.startsWith("/"
==true )
fileName = fileName.substring(1);
File file = new File(fileName);
int numOfBytes = (int)file.length();
FileInputStream inFile = new FileInputStream(fileName);
byte[] fileInBytes = new byte[numOfBytes];
inFile.read(fileInBytes);
/* Send the HTTP header */
outToClient.writeBytes("HTTP/1.1 200 Document Follows\r\n"
;
if (fileName.endsWith(".jpg"
)
outToClient.writeBytes("Content-Type: image/jpeg\r\n"
;
if (fileName.endsWith(".gif"
)
outToClient.writeBytes("Content-Type: image/gif\r\n"
;
if (fileName.endsWith(".html"
)
outToClient.writeBytes("Content-Type: html/jpeg\r\n"
;
outToClient.writeBytes("Content-Length: " + numOfBytes + "\r\n"
;
outToClient.writeBytes("\r\n"
;
/* Now send the actual data */
outToClient.write(fileInBytes, 0, numOfBytes);
connectionSocket.close();
}
else
System.out.println("Bad Request Message"
;
}
}
}
Help would be great
Many Thanks
import java.io.*;
import java.net.*;
import java.util.*;
class Web extends Thread
{
public static void main(String[] args) throws Exception
{
String requestMessageLine;
String fileName;
ServerSocket listenSocket = new ServerSocket(8005);
while(true){
Socket connectionSocket = listenSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(
connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(
connectionSocket.getOutputStream());
requestMessageLine = inFromClient.readLine();
StringTokenizer tokenizedLine =
new StringTokenizer(requestMessageLine);
if (tokenizedLine.nextToken().equals("GET"
{
fileName = tokenizedLine.nextToken();
if ( fileName.startsWith("/"
fileName = fileName.substring(1);
File file = new File(fileName);
int numOfBytes = (int)file.length();
FileInputStream inFile = new FileInputStream(fileName);
byte[] fileInBytes = new byte[numOfBytes];
inFile.read(fileInBytes);
/* Send the HTTP header */
outToClient.writeBytes("HTTP/1.1 200 Document Follows\r\n"
if (fileName.endsWith(".jpg"
outToClient.writeBytes("Content-Type: image/jpeg\r\n"
if (fileName.endsWith(".gif"
outToClient.writeBytes("Content-Type: image/gif\r\n"
if (fileName.endsWith(".html"
outToClient.writeBytes("Content-Type: html/jpeg\r\n"
outToClient.writeBytes("Content-Length: " + numOfBytes + "\r\n"
outToClient.writeBytes("\r\n"
/* Now send the actual data */
outToClient.write(fileInBytes, 0, numOfBytes);
connectionSocket.close();
}
else
System.out.println("Bad Request Message"
}
}
}