TCP service:
reliable transfer of
bytes
from one
process to another
controlled by
application
developer
controlled by
application
developer
process
TCP with
buffers,
variables
socket
process
TCP with
buffers,
variables
socket
Internet
controlled by
operating
system
controlled by
operating
system
host or
server
host or
server

2: Application Layer
96
Socket Programming
with TCP
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

2: Application Layer
97
Illustration

2: Application Layer
98
Stream Jargon
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, e.g., keyboard or socket
An
output stream
is attached to an output source,
e.g., monitor or socket

2: Application Layer
99
Socket Programming with TCP
outToServer
to network
from network
inFromServer
inFromUser
keyboard
monitor
Process
input
stream
input
stream
output
stream
TCP
socket
Client
process
client TCP
socket
Example client-server app:
1) Client reads line from
standard input (
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, prints modified
line from socket
(
inFromServer
stream)

2: Application Layer
100
Client/Server Socket Interaction: TCP
Server
(running on
hostid
)
Client
wait for incoming
connection request
connectionSocket =
welcomeSocket.accept()
create socket,
port=
x
, for
incoming request:
welcomeSocket =
ServerSocket()
create socket,
connect to
hostid
, port=
x
clientSocket =
Socket()
close
connectionSocket
read reply from
clientSocket
close
clientSocket
send request using
clientSocket
read request from
connectionSocket
write reply to
connectionSocket
TCP
connection setup

Example: Java Client (TCP)
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());
Create
input stream
Create
client socket,
connect to server
Create
output stream
attached to socket
2: Application Layer
101
