Download Application Layer: Socket Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Special Topics in Computer Engineering
Application Layer 2:
Socket programming
Some of these Slides are Based on Slides by Kurose
and Ross
Prepared for Sections 2.7 , 2.8 and 2.9
of the Book Computer Networking: A Top Down
Approach Featuring the Internet
&
TCP/IP Sockets in Java : Practical Guide for
Programmers
Spring 2006
CPE 0907532: Application Layer II
1
Chapter 2: Application layer
 2.1 Principles of
network applications
 2.2 Web and HTTP
 2.3 FTP
 2.4 Electronic Mail

SMTP, POP3, IMAP
 2.5 DNS
Spring 2006
 2.6 P2P file sharing
 2.7 Socket programming
with TCP
 2.8 Socket programming
with UDP
 2.9 Building a Web
server
CPE 0907532: Application Layer II
2
Socket programming
Goal: learn how to build client/server application that
communicate using sockets
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 streamoriented
Spring 2006
socket
a host-local,
application-created,
OS-controlled interface
(a “door”) into which
application process can
both send and
receive messages to/from
another application
process
CPE 0907532: Application Layer II
3
Socket-programming using TCP
Socket: a door between application process and endend-transport protocol (UCP or TCP)
TCP service: reliable transfer of bytes from one
process to another
controlled by
application
developer
controlled by
operating
system
Spring 2006
process
process
socket
TCP with
buffers,
variables
host or
server
internet
socket
TCP with
buffers,
variables
controlled by
application
developer
controlled by
operating
system
host or
server
CPE 0907532: Application Layer II
4
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
(e.g. welcomeSocket )
Client contacts server by:
 creating client-local TCP
socket
 specifying IP address, port
number of server process
 When client creates
socket (e.g. clientSocket ):
client TCP establishes
connection to server TCP
Spring 2006
 When contacted by client,
server TCP creates new
socket
(e.g.connectionSocket) for
server process to
communicate with client
 allows server to talk with
multiple clients
 source port numbers
used to distinguish
clients
application viewpoint
TCP provides reliable, in-order
transfer of bytes (“pipe”)
between client and server
CPE 0907532: Application Layer II
5
Why use Java
 Applications are more neatly and cleanly
written in Java
fewer lines of code
 each line can be explained without much
difficulty

 Client/server programming in Java is
becoming increasingly popular
Spring 2006
CPE 0907532: Application Layer II
6
Client/server socket interaction: TCP
Server (running on hostid)
Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
TCP
wait for incoming
connection request connection
connectionSocket =
welcomeSocket.accept()
read request from
connectionSocket
write reply to
connectionSocket
close
connectionSocket
setup
create socket,
connect to hostid, port=x
clientSocket =
Socket()
send request using
clientSocket
read reply from
clientSocket
close
clientSocket
7
Spring 2006
CPE 0907532: Application Layer II
8
Stream jargon
keyboard
monitor
output
stream
input
stream
client
TCP
clientSocket
socket
to network
Spring 2006
inFromServer
Client
Process
process
input
stream
outToServer
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
destination, e.g., monitor or
socket.
inFromUser
 A stream is a sequence of
TCP
socket
from network
CPE 0907532: Application Layer II
9
TCP Client/Server Interaction
Server starts by getting ready to receive client connections…
1.
2.
3.
Client
Create a TCP socket
Communicate
Close the connection
Server
1.
Create a TCP socket
2.
Repeatedly:
a.
b.
c.
Spring 2006
Accept new connection
Communicate
Close the connection
10
TCP Client/Server Interaction
ServerSocket welcomeSocket = new ServerSocket(servPort);
1.
2.
3.
Client
Create a TCP socket
Communicate
Close the connection
Server
1.
2.
Create a TCP socket
Repeatedly:
a.
b.
c.
Spring 2006
Accept new connection
Communicate
Close the connection
11
TCP Client/Server Interaction
for (;;) {
Socket connectionSocket = welcomeSocket .accept();
1.
2.
3.
Client
Create a TCP socket
Communicate
Close the connection
Server
1.
2.
Create a TCP socket
Repeatedly:
a.
b.
c.
Spring 2006
Accept new connection
Communicate
Close the connection
12
TCP Client/Server Interaction
Server is now blocked waiting for connection from a client
1.
2.
3.
Client
Create a TCP socket
Communicate
Close the connection
Server
1.
2.
Create a TCP socket
Repeatedly:
a.
b.
c.
Spring 2006
Accept new connection
Communicate
Close the connection
13
TCP Client/Server Interaction
Later, a client decides to talk to the server…
1.
2.
3.
Client
Create a TCP socket
Communicate
Close the connection
Server
1.
2.
Create a TCP socket
Repeatedly:
a.
b.
c.
Spring 2006
Accept new connection
Communicate
Close the connection
14
TCP Client/Server Interaction
Socket clientSocket = new Socket(server, servPort);
1.
2.
3.
Client
Create a TCP socket
Communicate
Close the connection
Server
1.
2.
Create a TCP socket
Repeatedly:
a.
b.
c.
Spring 2006
Accept new connection
Communicate
Close the connection
15
TCP Client/Server Interaction
OutputStream out = clientSocket.getOutputStream();
out.write(byteBuffer);
1.
2.
3.
Client
Create a TCP socket
Communicate
Close the connection
Server
1.
Create a TCP socket
2.
Repeatedly:
a.
b.
c.
Spring 2006
Accept new connection
Communicate
Close the connection
16
TCP Client/Server Interaction
Socket connectionSocket = welcomeSocket .accept();
1.
2.
3.
Client
Create a TCP socket
Communicate
Close the connection
Server
1.
2.
Create a TCP socket
Repeatedly:
a.
b.
c.
Spring 2006
Accept new connection
Communicate
Close the connection
17
TCP Client/Server Interaction
InputStream in = connectionSocket.getInputStream();
recvMsgSize = in.read(byteBuffer);
1.
2.
3.
Client
Create a TCP socket
Communicate
Close the connection
Server
1.
Create a TCP socket
2.
Repeatedly:
a.
b.
c.
Spring 2006
Accept new connection
Communicate
Close the connection
18
TCP Client/Server Interaction
close( clientSocket );
3.
Client
Create a TCP socket
Establish connection
Communicate
4.
Close the connection
1.
2.
close(connectionSocket)
3.
Server
Create a TCP socket
Bind socket to a port
Set socket to listen
4.
Repeatedly:
1.
2.
a.
b.
Spring 2006
Accept new connection
Communicate
19
Socket programming with TCP
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)
Spring 2006
CPE 0907532: Application Layer II
20
Example: Java client (TCP)
for
creation
of
stream
objects
import java.io.*;
import java.net.*;
class TCPClient {
For creation of Socket objects
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
Create
input stream
Create
client socket,
connect to server
Create
output stream
attached to socket
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
CPE 0907532: Application Layer II
21
Example: Java client (TCP), cont.
Create
input stream
attached to socket
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
Send line
to server
outToServer.writeBytes(sentence + '\n');
Read line
from server
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
Spring 2006
}
CPE 0907532: Application Layer II
22
Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
Create
welcoming socket
at port 6789
Wait, on welcoming
socket for contact
by client
Create input
stream, attached
to socket
Spring 2006
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()));
CPE 0907532: Application Layer II
23
Example: Java server (TCP), cont
Create output
stream, attached
to socket
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
Read in line
from socket
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
Write out line
to socket
outToClient.writeBytes(capitalizedSentence);
}
}
}
Spring 2006
End of while loop,
loop back and wait for
another client connection
CPE 0907532: Application Layer II
24
Chapter 2: Application layer
 2.1 Principles of
network applications
 2.2 Web and HTTP
 2.3 FTP
 2.4 Electronic Mail

SMTP, POP3, IMAP
 2.5 DNS
Spring 2006
 2.6 P2P file sharing
 2.7 Socket programming
with TCP
 2.8 Socket programming
with UDP
 2.9 Building a Web
server
CPE 0907532: Application Layer II
25
Socket programming with UDP
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
application viewpoint
UDP provides unreliable transfer
of groups of bytes (“datagrams”)
between client and server
UDP: transmitted data may be
received out of order, or
lost
Spring 2006
CPE 0907532: Application Layer II
26
Client/server socket interaction: UDP
Server (running on hostid)
create socket,
port=x, for
incoming request:
serverSocket =
DatagramSocket()
read request from
serverSocket
write reply to
serverSocket
specifying client
host address,
port number
Spring 2006
Client
create socket,
clientSocket =
DatagramSocket()
Create, address (hostid, port=x)
send datagram request
using clientSocket
read reply from
clientSocket
close
clientSocket
CPE 0907532: Application Layer II
27
Example: Java client (UDP)
input
stream
Client
process
monitor
inFromUser
keyboard
Process
Input: receives
packet (recall
thatTCP received
“byte stream”)
UDP
packet
receivePacket
packet (recall
that TCP sent
“byte stream”)
sendPacket
Output: sends
UDP
packet
client
UDP
clientSocket
socket
to network
Spring 2006
UDP
socket
from network
CPE 0907532: Application Layer II
28
Example: Java client (UDP)
import java.io.*;
import java.net.*;
Create
input stream
Create
client socket
Translate
hostname to IP
address using DNS
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();
Spring 2006
sendData = sentence.getBytes();
Convert a
string to an
array of
29
bytes
Example: Java client (UDP), cont.
Create datagram
with data-to-send,
length, IP addr, port
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
Send datagram
to server
clientSocket.send(sendPacket);
Read datagram
from server
clientSocket.receive(receivePacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
String modifiedSentence =
new String(receivePacket.getData());
Convert an
array of
bytes into
string
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
Spring 2006
CPE 0907532: Application Layer II
30
Example: Java server (UDP)
import java.io.*;
import java.net.*;
Create
datagram socket
at port 9876
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)
{
Create space for
received datagram
Receive
datagram
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
CPE 0907532: Application Layer II
31
Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData());
Get IP addr
port #, of
sender
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
Create datagram
to send to client
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress,
port);
Write out
datagram
to socket
serverSocket.send(sendPacket);
}
}
}
Spring 2006
End of while loop,
loop back and wait for
another datagram
32
Chapter 2: Application layer
 2.1 Principles of
network applications


app architectures
app requirements
 2.2 Web and HTTP
 2.4 Electronic Mail
 SMTP, POP3, IMAP
 2.5 DNS
Spring 2006
 2.6 P2P file sharing
 2.7 Socket programming
with TCP
 2.8 Socket programming
with UDP
 2.9 Building a Web
server
CPE 0907532: Application Layer II
33
Building a simple Web server
 handles one HTTP




request
accepts the request
parses header
obtains requested file
from server’s file
system
creates HTTP response
message:

 after creating server,
you can request file
using a browser (e.g.,
IE explorer)
 see text for details
header lines + file
 sends response to client
Spring 2006
CPE 0907532: Application Layer II
34
Related documents