63
Department of Computer and IT Department of Computer and IT Engineering Engineering University of Kurdistan University of Kurdistan Computer Networks II Socket programming By: Dr. Alireza Abdollahpouri By: Dr. Alireza Abdollahpouri

Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Embed Size (px)

DESCRIPTION

Department of Computer and IT Engineering University of Kurdistan Computer Networks II Socket programming By: Dr. Alireza Abdollahpouri. Network Applications communicates through a network. API is a programming means, either a library or part of OS. - PowerPoint PPT Presentation

Citation preview

Page 1: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Department of Computer and IT EngineeringDepartment of Computer and IT EngineeringUniversity of KurdistanUniversity of Kurdistan

Computer Networks IISocket programming

By: Dr. Alireza AbdollahpouriBy: Dr. Alireza Abdollahpouri

Page 2: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Network Application-Communication Network Applications communicates through a network. API is a programming means, either a library or part of OS. API provides the communication functionality for the

Network Applications.

Application Program Interface (API)

Communication Software & HardwarePlatform (OS + Hardware)

Application Software(Network Application)Application Software(Network Application)

OSOS

API comes as a library

Page 3: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

API Technologies Sockets (earliest technology)

Low level functions for the communication Socket, ServerSocket, connectionSocket, clientSocket,

DatagramSocket, Send, Read, Write, Close, Accept

Remote Procedure Call (RPC) Client code invokes a procedure on a remote server. RPCs support a wire format common to all platforms. Client and server must translate from their binary format into the wire

format. Each RPC involves four translation: Client to wire format, Wire to server format, Server back to wire (for

the return value), Wire format back to client format. Components: Components Look and act like objects,

CORBA (common object request Broker Architecture) COM, COM+ ,DCOM (Distributed Component Object Model)

Page 4: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Socket API introduced in BSD4.1 UNIX,

1981 explicitly created, used,

released by apps client/server paradigm two types of transport

service via socket API: unreliable datagram reliable, byte stream-

oriented

a host-local, application-created,

OS-controlled interface (a “door”)

into whichapplication process can both send and receive messages to/from another

application process

socket

Goal: learn how to build client/server application that communicate using sockets

Socket Programming

Page 5: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Socket Socket programming programming

with TCPwith TCP

Page 6: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Socket: a door between application process and end-end-transport protocol (UCP or TCP)

TCP service: reliable transfer of bytes from one process to another

process

socketsocket

controlled byapplicationdeveloper

controlled byoperating

system

client or server

process

socketsocket

controlled byapplicationdeveloper

controlled byoperatingsystem

client or server

NetworkTCP withTCP withbuffers,buffers,

variablesvariables

TCP withTCP withbuffers,buffers,

variablesvariables

Socket Programming Using TCP

Page 7: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Client must contact server server process must first be

running server must have created

socket (door) that welcomes client’s contact

Client contacts server by: creating client-local TCP

socket specifying IP address, port

number of server process When client creates socket:

client TCP establishes connection to server TCP

When contacted by client, server TCP creates new socket for server process to communicate with client allows server to talk with

multiple clients source port numbers

used to distinguish clients (more in Chap 3)

TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server

application viewpoint

Socket Programming with TCP

Page 8: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Sockets

ClientsocketClientsocket

ConnectionsocketConnectionsocket

WelcomingsocketWelcomingsocket

Three-way handsh

ake

Client processClient process Server processServer process

Client IP Address&

Port Number

Server IP Address&

Port Number2

Server IP Address&

Port Number1

bytes

sendread read write

Page 9: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Example client-server app. in Java:1) client reads line from standard input (keyboard)

(inFromUser stream) , sends to server via socket (outToServer stream)

2) server reads line from socket

3) server converts line to uppercase, sends back to client

4) client reads (inFromServer stream), prints modified line from socket on its standard output (monitor)

Example client-server app. in Java:1) client reads line from standard input (keyboard)

(inFromUser stream) , sends to server via socket (outToServer stream)

2) server reads line from socket

3) server converts line to uppercase, sends back to client

4) client reads (inFromServer stream), prints modified line from socket on its standard output (monitor)

Example: Socket Programming with TCP

Page 10: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Example: 3 streams and 1 socket

ClientProcessClient

Process

ClientSocketClientSocket

ou

tTO

Ser

vero

utT

OS

erve

rin

Fo

rmU

serin

Fo

rmU

ser

inF

orm

Ser

verin

Fo

rmS

erve

r

InputStream

OutputStream

InputStream

TCP socket

To transport layerFrom transport layer

Page 11: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

A stream is a sequence of characters that flow into or out of a process.

An input stream is attached to some input source for the process, eg, keyboard or socket.

An output stream is attached to an output source, eg, monitor or socket.

Streams

Page 12: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

wait for incomingconnection requestconnectionSocket =welcomeSocket.accept()

create socket,port=x, forincoming request:welcomeSocket =

ServerSocket()

create socket,connect to hostid, port=xclientSocket =

Socket()

closeconnectionSocket

read reply fromclientSocket

closeclientSocket

Server (running on hostid) Client

send request usingclientSocketread request from

connectionSocket

write reply toconnectionSocket

TCP connection setup

Client/Server Socket Interaction: TCP

Page 13: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

import java.io.*; import java.net.*; class TCPClient {

public static void main(String argv[ ]) throws Exception { String sentence; String modifiedSentence;

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

Socket clientSocket = new Socket("hostname", 6789);

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

import java.io.*; import java.net.*; class TCPClient {

public static void main(String argv[ ]) throws Exception { String sentence; String modifiedSentence;

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

Socket clientSocket = new Socket("hostname", 6789);

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

Createsinput stream

Creates client socket,

TCP connection to server

Createsoutput stream

attached to socket

Example: TCPClient.java

server name

server port no.

importStatements

make Java classes available

networking class

class: is a collection of data and methods that operate on that data

Page 14: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence + '\n');

modifiedSentence = inFromServer.readLine();

System.out.println("FROM SERVER: " + modifiedSentence);

clientSocket.close(); } }

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence + '\n');

modifiedSentence = inFromServer.readLine();

System.out.println("FROM SERVER: " + modifiedSentence);

clientSocket.close(); } }

Createsinput stream

attached to socket

Sends lineto server

Reads linefrom server

Example: TCPClient.java (cont.)

Page 15: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-TCPClient.java

import java.io.*;

import java.net.*; java.io and java.net are java packages. The java.io package contains

classes for input and output streams. In particular, the java.io package contains the BufferedReader and DataOutputStream classes, classes that the program uses to create the three steams previously illustrated.

The java.net package provides classes for network support. In particular, it contains the Socket and ServerSocket classes. The clientSocket object of this program is derived from the Socket class.

Page 16: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-TCPClient.javaclass TCPClient {public static void main(String argv[ ]) throws Exception{......}} The first line is the beginning of a class definition block. The keyword

class begins the class definition for the class named TCPClient. A class contains variables and methods. The variables and methods of the class are embraced by the curly brackets that begin and end the class definition block. The class TCPClient has no class variables and exactly one method, the main() method.

When the Java interpreter executes an application, it starts by calling the class's main method. The main method then calls all the other methods required to run the application.

Page 17: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-TCPClient.javaString sentence;String modifiedSentence; These above two lines declare objects of type String. The object

sentence is the string typed by the user and sent to the server. The object modifiedSentence is the string obtained from the server and sent to the user's standard output.

BufferedReader inFromUser = new BufferedReader (new InputStreamReader(System.in));

The above line creates the stream object inFromUser of type BufferedReader. The input stream is initialized with System.in, which attaches the stream to the standard input. The command allows the client to read text from its keyboard.

Page 18: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-TCPClient.javaSocket clientSocket = new Socket("hostname", 6789);

The above line creates the object clientSocket of type Socket. It also initiates the TCP connection between client and server. The string "hostname" must be replaced with the host name of the server (for example, "fling.seas.upenn.edu"). Before the TCP connection is actually initiated, the client performs a DNS look-up on the hostname to obtain the host's IP address. The number 6789 is the port number. You can use a different port number; but you must make sure that you use the same port number at the server side of the application.

As discussed earlier, the host's IP address along with the application's port number identifies the server process.

Page 19: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-TCPClient.java

DataOutputStream outToServer =new DataOutputStream(clientSocket.getOutputStream());

BufferedReader inFromServer =new BufferedReader(new inputStreamReader(clientSocket.getInputStream()));

The above two lines create stream objects that are attached to the socket. The outToServer stream provides the process output to the socket. The inFromServer stream provides the process input from the socket.

Page 20: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-TCPClient.javasentence = inFromUser.readLine();

The above line places a line typed by the user into the string sentence. The string sentence continues to gather characters until the user ends the line by typing a carriage return. The line passes from standard input through the stream inFromUser into the string sentence.

Page 21: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-TCPClient.javaoutToServer.writeBytes(sentence + '\n'); The above line sends the string sentence augmented with a carriage

return into the outToServer stream. The augmented sentence flows through the client's socket and into the TCP pipe. The client then waits to receive characters from the server.

modifiedSentence = inFromServer.readLine(); When characters arrive from the server, they flow through the stream

inFromServer and get placed into the string modifiedSentence. Characters continue to accumulate in modifiedSentence until the line ends with a carriage return character.

Page 22: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-TCPClient.javaSystem.out.println("FROM SERVER " + modifiedSentence);

The above line prints to the monitor the string modifiedSentence returned by the server.

clientSocket.close();

This last line closes the socket and, hence, closes the TCP connection between the client and the server. It causes TCP in the client to send a TCP message to TCP in the server (see Section 3.5).

Page 23: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

import java.io.*; import java.net.*;

class TCPServer {

public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence;

ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept();

BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

Createwelcoming socket

at port 6789

Wait, on welcomingsocket for contact

by client

Create inputstream, attached

to socket

Example: TCPServer.java

Page 24: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

clientSentence = inFromClient.readLine();

capitalizedSentence = clientSentence.toUpperCase() + '\n';

outToClient.writeBytes(capitalizedSentence); } } }

Read in linefrom socket

Create outputstream,

attached to socket

Write out lineto socket

End of while loop,loop back and wait foranother client connection

Example: TCPServer.java (cont.)

Page 25: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-TCPServer.java TCPServer has many similarities with TCPClient. We will not comment on

the lines that are identical or similar to commands in TCPClient.java. The first line in TCPServer that is substantially different from what we saw in

TCPClient is:

ServerSocket welcomeSocket = new ServerSocket(6789);

That line creates the object welcomeSocket, which is of type ServerSocket. The WelcomeSocket is a sort of door that waits for a knock from some client.

The port number 6789 identifies the process at the server.

Page 26: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-TCPServer.java

Socket connectionSocket = welcomeSocket.accept();

This line creates a new socket, called connectionSocket, when some client knocks on welcomeSocket.

TCP then establishes a direct virtual pipe between clientSocket at the client and connectionSocket at the server.

The client and server can then send bytes to each other over the pipe, and all bytes sent arrive at the other side in order.

With connectionSocket established, the server can continue to listen for other requests from other clients for the application using welcomeSocket. (This version of the program doesn't actually listen for more connection requests, but it can be modified with threads to do so.)

Page 27: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-TCPServer.java

The program then creates several stream objects, analogous to the stream objects created in clientSocket. Now consider:

capitalizedSentence = clientSentence.toUpperCase() + '\n';

This command is the heart of the application. It takes the line sent by the client, capitalizes it, and adds a carriage return. It uses the method toUpperCase().

All the other commands in the program are peripheral; they are used for communication with the client.

Page 28: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-TCPServer.java

To test the program pair, you install and compile TCPClient.java in one host and TCPServer.java in another host. Be sure to include the proper host name of the server in TCPClient.java.

You then execute TCPServer.class, the compiled server program, in the server. This creates a process in the server that idles until it is contacted by some client.

Then you execute TCPClient.class, the compiled client program, in the client. This creates a process in the client and establishes a TCP connection between the client and server processes.

Finally, to use the application, you type a sentence followed by a carriage return.

Page 29: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Socket Socket programming programming

with UDPwith UDP

Page 30: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

UDP: no “connection” between client and server,

no handshaking, sender explicitly attaches IP

address and port of destination to each packet,

server must extract IP address, port of sender from received packet.

UDP: transmitted data may be received out of order, or lost.

application viewpoint

UDP provides unreliable transfer of groups of bytes (“datagrams”)

between client and server

Socket Programming with UDP

Page 31: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

closeclientSocket

Server (running on hostid)

read reply fromclientSocket

create socket,clientSocket = DatagramSocket()

Client

Create, address (hostid, port=x,send datagram request using clientSocket

create socket,port=x, forincoming request:serverSocket = DatagramSocket()

read request fromserverSocket

write reply toserverSocketspecifying clienthost address,port number

Client/Server Socket Interaction: UDP

Page 32: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Example: Java Client (UDP)

Output: sends packet (TCP sent “byte stream”)

Input: receives packet (TCP received “byte stream”)

ProcessProcess

ClientSocketClientSocketre

ciev

epac

ketse

nd

pac

ket

inF

rom

Use

r

InputStream

UDPdatagram

packet

UDPdatagrampacket

UDP socket

To transport layer From

transport layer

Page 33: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine();

sendData = sentence.getBytes();

Createinput stream

Create client socket

Translate hostname to IP

address using DNS

Example: UDPClient.java

Page 34: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); }

}

Create datagram with data-to-

send,length, IP addr,

portSend datagram

to server

Read datagramfrom server

Example: Example: UDPClient.java (cont.)

Page 35: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-UDPClient.java The program UDPClient.java constructs one stream and one socket.

The socket is called clientSocket, and it is of type DatagramSocket. UDP uses a different kind of socket than TCP at the client. Here the

client uses a DatagramSocket whereas with TCP our client used a Socket.

The stream inFromUser is an input stream to the program; it is attached to the standard input, that is, to the keyboard. We had an equivalent stream in our TCP version of the program. When the user types characters on the keyboard, the characters flow into the stream inFromUser.

In contrast with TCP, there are no streams (input or output) attached to the socket. Instead of feeding bytes to the stream attached to a Socket object, UDP will push individual packets through the DatagramSocket object.

Page 36: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-UDPClient.javaDatagramSocket clientSocket = new DatagramSocket();

It creates the object clientSocket of type DatagramSocket. In contrast with TCPClient.java, this line does not initiate a TCP connection.

In particular, the client host does not contact the server host upon execution of this line. For this reason, the constructor DatagramSocket() does not take the server hostname or port number as arguments.

The execution of the above line creates a port for the client process but does not create a connection between the two processes.

Page 37: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-UDPClient.javaInetAddress IPAddress = InetAddress.getByName("hostname");

In order to send bytes to a destination process, we shall need to obtain the address of the process. Part of this address is the IP address of the destination host.

The above line invokes a DNS look-up that translates the hostname to an IP address.

DNS was also invoked by the TCP version of the client, although it was done there implicitly rather than explicitly.

The method getByName() takes as an argument the hostname of the server and returns the IP address of this same server. It places this address in the object IPAddress of type InetAddress.

Page 38: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-UDPClient.javabyte[ ] sendData = new byte[ ];

byte[ ] receiveData = new byte[ ];

The byte arrays sendData and receiveData will hold the data the client sends and receives, respectively.

sendData = sentence.getBytes();

The above line essentially performs a type conversion. It takes the string sentence and renames it as sendData, which is an array of bytes.

Page 39: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-UDPClient.javaDatagramPacket sendPacket = new DatagramPacket(sendData,

sendData.length, IPAddress, 9876);

This line constructs the packet, sendPacket, that the client will pop into the network through its socket.

Note that sendPacket is of type DatagramPacket. This packet includes that data that is contained in the packet,

sendData, the length of this data, the IP address of the server, and the port number of the application (which we have set to 9876).

Page 40: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-UDPClient.javaclientSocket.send(sendPacket);

In the above line, the method send() of the object clientSocket takes the packet just constructed and pops it into the network through clientSocket.

Once again, note that UDP sends the line of characters in a manner very different from TCP.

TCP simply inserted the line into a stream, which had a logical direct connection to the server; UDP creates a packet that includes the address of the server.

After sending the packet, the client then waits to receive a packet from the server.

Page 41: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-UDPClient.javaDatagramPacket receivePacket = new DatagramPacket(receiveData,

receiveData.length);

In the above line, while waiting for the packet from the server, the client creates a place holder for the packet, receivePacket, an object of type DatagramPacket.

clientSocket.receive(receivePacket);

The client idles until it receives a packet; when it does receive a packet, it puts the packet in receivePacket.

Page 42: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-UDPClient.javaString modifiedSentence = new String(receivePacket.getData()); It extracts the data from receivePacket and performs a type

conversion, converting an array of bytes into the string modifiedSentence.

System.out.println("FROM SERVER:" + modifiedSentence); This line, which is also present in TCPClient, prints out the string

modifiedSentence at the client's monitor.

clientSocket.close(); This last line closes the socket. Because UDP is connectionless, this

line does not cause the client to send a transport-layer message to the server (in contrast with TCPClient).

Page 43: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

import java.io.*; import java.net.*; class UDPServer { public static void main(String args[ ]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[ ] receiveData = new byte[1024]; byte[ ] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

serverSocket.receive(receivePacket);

Createdatagram socket

at port 9876

Create space forreceived datagram

Receivedatagra

m

Example: UDPServer.java

Page 44: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase();

sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } }

}

Get IP addrport #, of

sender

Write out datagramto socket

End of while loop,loop back and wait foranother datagram

Create datagramto send to client

Example: Example: UDPServer.java (cont.)

Page 45: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

handles one HTTP request

accepts the request parses header obtains requested file

from server’s file system creates HTTP response

message: header lines + file

sends response to client

after creating server, you can request file using a browser (eg IE explorer)

see the following slides for details.

Building a Simple Web Server

Page 46: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Example: WebServer.javaimport java.io.*;

import java.net.*;

import java.util.*;

class WebServer {

public static void main(String argv[ ]) throws Exception

{

String requestMessageLine;

String fileName;

ServerSocket listenSocket = new ServerSocket(6789);

Socket connectionSocket = listenSocket.accept();

BufferedReader inFromClient = new BufferedReader(

new InputStreamReader(connectionSocket.getInputStream()));

DataOutputStream outToClient =

new DataOutputStream(connectionSocket.getOutputStream());

import java.io.*;

import java.net.*;

import java.util.*;

class WebServer {

public static void main(String argv[ ]) throws Exception

{

String requestMessageLine;

String fileName;

ServerSocket listenSocket = new ServerSocket(6789);

Socket connectionSocket = listenSocket.accept();

BufferedReader inFromClient = new BufferedReader(

new InputStreamReader(connectionSocket.getInputStream()));

DataOutputStream outToClient =

new DataOutputStream(connectionSocket.getOutputStream());

contain the first line in the HTTP request messagecontain the file name of the requested filecontain the first line in the HTTP request messagecontain the file name of the requested file

When a request for a connection arrives, the accept( ) method of listenSocket creates a new object, connectionSocket, of type Socket.

When a request for a connection arrives, the accept( ) method of listenSocket creates a new object, connectionSocket, of type Socket.

two s

tream

s a

re

cre

ate

dtw

o s

tream

s a

re

cre

ate

d

Page 47: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Example: WebServer.java (cont.)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[ ];

inFile.read(fileInBytes);

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[ ];

inFile.read(fileInBytes);

reads the first line of the HTTP request message.

reads the first line of the HTTP request message.

tokenizedLine holds the request line with each of the "words" GET, file_name, and HTTP/1.1 placed in a separate placeholder called a token.

tokenizedLine holds the request line with each of the "words" GET, file_name, and HTTP/1.1 placed in a separate placeholder called a token.

removes the backslash that may precede the filename.

removes the backslash that may precede the filename.

These commands determine the size of the file and construct an array of bytes of that size. The name of the array is fileInBytes.

These commands determine the size of the file and construct an array of bytes of that size. The name of the array is fileInBytes.

reads from the stream inFile to the byte array fileInBytes

reads from the stream inFile to the byte array fileInBytes

Page 48: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Example: WebServer.java (cont.)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");

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");

} }

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");

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");

} }

sends the mandatory status line: HTTP/1.1 200 Document Follows, followed by a carriage return and a line feed.

sends the mandatory status line: HTTP/1.1 200 Document Follows, followed by a carriage return and a line feed.

◊ to transfer a GIF image, server prepares the header line Content-Type: image/gif. ◊ to transfer a JPEG image, server prepares the header line Content-Type: image/jpeg.

◊ to transfer a GIF image, server prepares the header line Content-Type: image/gif. ◊ to transfer a JPEG image, server prepares the header line Content-Type: image/jpeg.

sends a content-length header

line and a mandatory blank

line

sends a content-length header

line and a mandatory blank

line sends the requested file, fileInBytes, to the TCP send buffer

sends the requested file, fileInBytes, to the TCP send buffer

closing the socket connectionSocketclosing the socket connectionSocket

Page 49: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.java

The first half of the program is almost identical to TCPServer.java. As

with TCPServer.java, we import the java.io and java.net packages.

In addition to these two packages we also import the java.util

package, which contains the StringTokenizer class, which is used for

parsing HTTP request messages.

Page 50: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.java Looking now at the lines within the classWebServer, we define two

string objects:

String requestMessageLine;

String fileName;

The object requestMessageLine is a string that will contain the first line in the HTTP request message.

The object fileName is a string that will contain the file name of the requested file.

Page 51: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.java The next set of commands is identical to the corresponding set of

commands in TCPServer.java.

ServerSocket listenSocket = new ServerSocket(6789);

Socket connectionSocket = listenSocket.accept(); Two socket-like objects are created. The first of these objects is

listenSocket, which is of type ServerSocket. The object listenSocket is created by the server program before receiving a request for a TCP connection from a client. It listens at port 6789 and waits for a request from some client to establish a TCP connection. When a request for a connection arrives, the accept() method of listenSocket creates a new object, connectionSocket, of type Socket.

Page 52: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.javaBufferedReader inFromClient =

new BufferedReader(new InputStreamReader

(connectionSocket.getInputStream()));

DataOutputStream outToClient =

new DataOutputStream(connectionSocket. getOutputStream());

Next two streams are created: the BufferedReader inFromClient and the DataOutputStream outToClient. The HTTP request message comes from the network, through connectionSocket and into inFromClient; the HTTP response message goes into outToClient, through connectionSocket and into the network.

Page 53: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.java The remaining portion of the code differs significantly from TCPServer.java.

requestMessageLine = inFromClient.readLine();

The above command reads the first line of the HTTP request message. This line is supposed to be of the form:GET file_name HTTP/1.1

Page 54: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.java Our server must now parse the line to extract the filename.

StringTokenizer tokenizedLine =new StringTokenizer(requestMessageLine);

if (tokenizedLine.nextToken().equals("GET")) {fileName = tokenizedLine.nextToken();

They parse the first line of the request message to obtain the requested filename.

The object tokenizedLine can be thought of as the original request line with each of the "words" GET, file_name, and HTTP/1.1placed in a separate placeholder called a token.

Page 55: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.java The server knows from the HTTP RFC that the file name for the

requested file is contained in the token that follows the token containing "GET."

This file name is put in a string called fileName.

if (fileName.startsWith("/") == true )

fileName = fileName.substring(1);

The purpose of the above if statement in the above code is to remove the backslash that may precede the filename.

Page 56: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.javaFileInputStream inFile = new FileInputStream (fileName); The above command attaches a stream, inFile, to the file fileName.

byte[] fileInBytes = new byte[];

inFile.read(fileInBytes); These commands determine the size of the file and construct an

array of bytes of that size. The name of the array is fileInBytes. The last command reads from the stream inFile to the byte array

fileInBytes. The program must convert to bytes because the output stream outToClient may only be fed with bytes.

Page 57: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.java Now we are ready to construct the HTTP response message. To this

end we must first send the HTTP response header lines into the DataOutputStream outToClient:

outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");

The above and next two commands are particularly interesting. These commands prepare the header lines for the HTTP response message and send the header lines to the TCP send buffer.

The above command sends the mandatory status line: HTTP/1.1 200 Document Follows, followed by a carriage return and a line feed.

Page 58: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.javaif (fileName.endsWith(".jpg")) outToClient.writeBytes("Content-Type:

image/jpeg\r\n");if (fileName.endsWith(".gif")) outToClient.writeBytes("Content-Type:

image/gif\r\n");

These tow command lines prepare a single content-type header line. If the server is to transfer a GIF image, then the server prepares the

header line Content-Type: image/gif. If the server is to transfer a JPEG image, then the server prepares

the header line Content-Type: image/jpeg. (In this simple Web server, no content line is sent if the object is neither a GIF nor a JPEG image.)

Page 59: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.javaoutToClient.writeBytes("Content-Length: " + numOfBytes + "\r\n");

outToClient.writeBytes("\r\n");

The server then prepares and sends a content-length header line and a mandatory blank line to precede the object itself that is to be sent. We now must send the file FileName into the DataOutputStream outToClient.

Page 60: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.java We can now send the requested file:

outToClient.write(fileInBytes, 0, numOfBytes);

The above command sends the requested file, fileInBytes, to the TCP send buffer. TCP will concatenate the file, fileInBytes, to the header lines just created, segment the concatenation if necessary, and send the TCP segments to the client.

connectionSocket.close();

After serving one request for one file, the server performs some housekeeping by closing the socket connectionSocket.

Page 61: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

Code Descriptions-WebServer.java To test this Web server,

install it on a host. put some files in the host.

Then use a browser running on any machine to request a file from the server.

When you request a file, you will need to use the port number that you include in the server code (for example, 6789).

So if your server is located at somehost.somewhere.edu, the file is somefile.html, and the port number is 6789, then the browser should request the following:

http://somehost.somewhere.edu:6789/somefile.html

Page 62: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

C-language tutorial (audio/slides): “Unix Network Programming” (J. Kurose),

http://manic.cs.umass.edu/~amldemo/courseware/intro.

Java-tutorials: “All About Sockets” (Sun tutorial),

http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html

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

Socket Programming: References

Page 63: Department of Computer and IT Engineering University of Kurdistan Computer Networks II

63QuestionsQuestionsQuestionsQuestions