0% found this document useful (0 votes)
28 views

Tutorial 11 - Socket Programming Part 2

This document provides source code for a simple Java web server and instructions for testing it. The source code uses sockets to listen for client requests and send back requested files along with HTTP headers. To test the server, requests need to be made to it to check that it responds properly. If errors occur, debugging techniques could help solve issues. A web server differs from a UDP application in that it uses TCP for reliable connections and supports the HTTP protocol for request-response transactions.

Uploaded by

ismael
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Tutorial 11 - Socket Programming Part 2

This document provides source code for a simple Java web server and instructions for testing it. The source code uses sockets to listen for client requests and send back requested files along with HTTP headers. To test the server, requests need to be made to it to check that it responds properly. If errors occur, debugging techniques could help solve issues. A web server differs from a UDP application in that it uses TCP for reliable connections and supports the HTTP protocol for request-response transactions.

Uploaded by

ismael
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tutorial 11 Socket programming part 2

Contents
1

Objectives .................................................................................................................................................... 1

Source code ................................................................................................................................................. 1

How to test? ................................................................................................................................................ 2

How to solve the issue? ............................................................................................................................... 2

Question ...................................................................................................................................................... 2

Tutorial 11 Socket programming part 2

1 Objectives
In this session we want to develop a simple web server.

2 Source code1
public class Web {
public static void main(String[] args) throws Exception {
String requestMessageLine;
String fileName;
ServerSocket listenSocket = new ServerSocket(88);
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);
outToClient.writeBytes("HTTP/1.0 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: text/HTML\r\n");
}
outToClient.writeBytes("Content-Length: " + numOfBytes + "\r\n");
outToClient.writeBytes("\r\n");
outToClient.write(fileInBytes, 0, numOfBytes);
connectionSocket.close();
} else {
System.out.println("Bad Request Message");
}
1

Thanks to Tony Gauvin, university of Maine

DECOM Tutorial document

last update September 2014

Tutorial 11 Socket programming part 2


}
}

3 How to test?
We need to make sure this application is able to response to our request, how to test this simple web
server?

4 How to solve the issue?


If you are not able to see the result and the application shows any error upon request, what are the ways to
solve this issue?

5 Question
In the real world, is a web server same as UDP socket application? Please illustrate your answer.

References
-

All About Sockets (Sun tutorial), http://www.javaworld.com/javaworld/jw-12-1996/jw-12sockets.html


Socket Programming in Java: a tutorial, http://www.javaworld.com/javaworld/jw-12-1996/jw-12sockets.html

DECOM Tutorial document

last update September 2014

You might also like