Chapter 5 - Sockets
Chapter 5 - Sockets
Chapter 5 - Sockets
2: Application Layer 1
Socket-programming using TCP
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
controlled by
controlled by process application
application process
developer
developer socket socket
controlled by TCP with TCP with controlled by
buffers, operating
operating buffers, internet system
system variables variables
host or host or
server server
2: Application Layer 2
Socket programming with TCP
Client must contact server When contacted by client,
server process must first be server TCP creates new
running socket for server process to
server must have created communicate with client
socket (door) that welcomes allows server to talk with
client’s contact multiple clients
Client contacts server by: source port numbers
creating client-local TCP used to distinguish
socket clients (more in Chap 3)
specifying IP address, port
number of server process application viewpoint
When client creates socket: TCP provides reliable, in-order
client TCP establishes transfer of bytes (“pipe”)
connection to server TCP between client and server
2: Application Layer 3
Stream jargon
keyboard monitor
A stream is a sequence
of characters that flow
into or out of a process.
inFromUser
input
stream
inFromServer
outToServer
output input
attached to an output
stream stream
2: Application Layer 4
Socket programming with TCP
keyboard monitor
inFromUser
input
stream
stream) , sends to server via Client
socket (outToServer Process
process
stream)
2) server reads line from socket
3) server converts line to
uppercase, sends back to
inFromServer
outToServer
output input
client stream stream
2: Application Layer 5
Client/server socket interaction: TCP
Server (running on hostid) Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
2: Application Layer 6
Example: Java client (TCP)
import java.io.*;
import java.net.*;
class TCPClient {
sentence = inFromUser.readLine();
Send line
to server outToServer.writeBytes(sentence + '\n');
clientSocket.close();
}
}
2: Application Layer 8
Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
2: Application Layer 9
Example: Java server (TCP), cont
Create output
stream, attached DataOutputStream outToClient =
to socket new DataOutputStream(connectionSocket.getOutputStream());
Read in line
from socket clientSentence = inFromClient.readLine();
2: Application Layer 10
Socket programming with UDP
2: Application Layer 11
Client/server socket interaction: UDP
Server (running on hostid) Client
create socket,
port=x, for create socket,
clientSocket =
incoming request: DatagramSocket()
serverSocket =
DatagramSocket()
Create, address (hostid, port=x,
send datagram request
using clientSocket
read request from
serverSocket
write reply to
serverSocket
specifying client read reply from
host address, clientSocket
port number close
clientSocket
2: Application Layer 12
Example: Java client (UDP)
keyboard monitor
inFromUser
input
stream
Client
Process
Input: receives
process
packet (TCP
Output: sends received “byte
packet (TCP sent stream”)
receivePacket
sendPacket
“byte stream”) UDP UDP
packet packet
client
clientSocket UDP
socket UDP
socket
2: Application Layer 13
Example: Java client (UDP)
import java.io.*;
import java.net.*;
class UDPClient {
public static void main(String args[]) throws Exception
{
Create
input stream BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Create
client socket DatagramSocket clientSocket = new DatagramSocket();
Translate
InetAddress IPAddress = InetAddress.getByName("hostname");
hostname to IP
address using DNS byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
2: Application Layer 15
Example: Java server (UDP)
import java.io.*;
import java.net.*;
class UDPServer {
public static void main(String args[]) throws Exception
Create {
datagram socket
DatagramSocket serverSocket = new DatagramSocket(9876);
at port 9876
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
Create space for
DatagramPacket receivePacket =
received datagram
new DatagramPacket(receiveData, receiveData.length);
Receive serverSocket.receive(receivePacket);
datagram
2: Application Layer 16
Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData());
Get IP addr
InetAddress IPAddress = receivePacket.getAddress();
port #, of
sender int port = receivePacket.getPort();
sendData = capitalizedSentence.getBytes();
Create datagram
DatagramPacket sendPacket =
to send to client new DatagramPacket(sendData, sendData.length, IPAddress,
port);
Write out
datagram serverSocket.send(sendPacket);
to socket }
}
} End of while loop,
loop back and wait for
another datagram
2: Application Layer 17
Building a simple Web server
handles one HTTP after creating server,
request you can request file
accepts the request using a browser (eg IE
explorer)
parses header
see text for details
obtains requested file
from server’s file
system
creates HTTP response
message:
header lines + file
sends response to client
2: Application Layer 18