M - ch2 Socket Programming With TCP and UDP

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Chapter 2: Application layer

 2.1 Principles of  2.6 P2P file sharing


network applications  2.7 Socket programming
 2.2 Web and HTTP with TCP
 2.3 FTP  2.8 Socket programming
 2.4 Electronic Mail with UDP
 SMTP, POP3, IMAP  2.9 Building a Web
 2.5 DNS server

2: Application Layer 1
Socket programming
Goal: learn how to build client/server application that
communicate using sockets

Socket API socket


 introduced in BSD4.1 UNIX,
a host-local,
1981 application-created,
 explicitly created, used, OS-controlled interface
released by apps (a “door”) into which
 client/server paradigm application process can
 two types of transport both send and
service via socket API: receive messages to/from
another application
 unreliable datagram
process
 reliable, byte stream-
oriented

2: Application Layer 2
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
TCP with TCP with controlled by
controlled by
buffers, operating
operating buffers, internet system
system variables variables

host or host or
server server

2: Application Layer 3
Socket programming with TCP
Client must contact server  When contacted by client,
 server process must first server TCP creates new
be running socket for server process to
 server must have created communicate with client
socket (door) that  allows server to talk with
welcomes client’s contact multiple clients
 source port numbers
Client contacts server by:
used to distinguish
 creating client-local TCP
clients (more in Chap 3)
socket
 specifying IP address, port application viewpoint
number of server process
TCP provides reliable, in-order
 When client creates
transfer of bytes (“pipe”)
socket: client TCP between client and server
establishes connection to
server TCP
2: Application Layer 4
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 5
Socket programming with TCP
keyboard monitor
Example client-server app:
1) client reads line from

inFromUser
standard input (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

4) client reads, prints modified


line from socket client TCP
clientSocket

(inFromServer stream) socket TCP


socket

to network from network

2: Application Layer 6
Client/server socket interaction: TCP
Server (running on hostid) Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()

TCP create socket,


wait for incoming
connection request connection setup connect to hostid, port=x
connectionSocket = clientSocket =
welcomeSocket.accept() Socket()

send request using


read request from clientSocket
connectionSocket

write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
2: Application Layer 7
Example: Java client (TCP)
import java.io.*;
import java.net.*;
class TCPClient {

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


{
String sentence;
String modifiedSentence;
Create
input stream BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Create
client socket, Socket clientSocket = new Socket("hostname", 6789);
connect to server
Create DataOutputStream outToServer =
output stream new DataOutputStream(clientSocket.getOutputStream());
attached to socket
2: Application Layer 8
Example: Java client (TCP), cont.

Create BufferedReader inFromServer =


input stream new BufferedReader(new
attached to socket InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();
Send line
to server outToServer.writeBytes(sentence + '\n');

Read line modifiedSentence = inFromServer.readLine();


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

clientSocket.close();

}
}
2: Application Layer 9
Example: Java server (TCP)
import java.io.*;
import java.net.*;

class TCPServer {

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


{
String clientSentence;
Create String capitalizedSentence;
welcoming socket
ServerSocket welcomeSocket = new ServerSocket(6789);
at port 6789
while(true) {
Wait, on welcoming
socket for contact Socket connectionSocket = welcomeSocket.accept();
by client
BufferedReader inFromClient =
Create input new BufferedReader(new
stream, attached InputStreamReader(connectionSocket.getInputStream()));
to socket

2: Application Layer 10
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();

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


Write out line
outToClient.writeBytes(capitalizedSentence);
to socket
}
}
} End of while loop,
loop back and wait for
another client connection

2: Application Layer 11
Chapter 2: Application layer
 2.1 Principles of  2.6 P2P file sharing
network applications  2.7 Socket programming
 2.2 Web and HTTP with TCP
 2.3 FTP  2.8 Socket programming
 2.4 Electronic Mail with UDP
 SMTP, POP3, IMAP  2.9 Building a Web
 2.5 DNS server

2: Application Layer 12
Socket programming with UDP

UDP: no “connection” between


client and server
 no handshaking
 sender explicitly attaches application viewpoint
IP address and port of
destination to each packet UDP provides unreliable transfer
of groups of bytes (“datagrams”)
 server must extract IP
between client and server
address, port of sender
from received packet
UDP: transmitted data may be
received out of order, or
lost

2: Application Layer 13
Client/server socket interaction: UDP
Server (running on hostid) Client

create socket, create socket,


port=x, for clientSocket =
incoming request: DatagramSocket()
serverSocket =
DatagramSocket()
Create, address (hostid, port=x,
send datagram request
read request from using clientSocket
serverSocket

write reply to
serverSocket
specifying client read reply from
host address, clientSocket
port number close
clientSocket

2: Application Layer 14
Example: Java client (UDP)
keyboard monitor

inFromUser
input
stream

Client
Process
Input: receives
process
packet (recall
Output: sends thatTCP received
packet (recall “byte stream”)

receivePacket
sendPacket
that TCP sent UDP
packet
UDP
packet
“byte stream”)
client UDP
clientSocket
socket UDP
socket

to network from network

2: Application Layer 15
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];

String sentence = inFromUser.readLine();


sendData = sentence.getBytes();
2: Application Layer 16
Example: Java client (UDP), cont.
Create datagram
with data-to-send, DatagramPacket sendPacket =
length, IP addr, port new DatagramPacket(sendData, sendData.length, IPAddress, 9876);

Send datagram clientSocket.send(sendPacket);


to server
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
Read datagram
clientSocket.receive(receivePacket);
from server
String modifiedSentence =
new String(receivePacket.getData());

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


clientSocket.close();
}
}

2: Application Layer 17
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 18
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();

String capitalizedSentence = sentence.toUpperCase();

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 19
Chapter 2: Application layer
 2.1 Principles of  2.6 P2P file sharing
network applications  2.7 Socket programming
 app architectures with TCP
 app requirements
 2.8 Socket programming
 2.2 Web and HTTP with UDP
 2.4 Electronic Mail  2.9 Building a Web
 SMTP, POP3, IMAP server
 2.5 DNS

2: Application Layer 20
Building a simple Web server
 handles one HTTP  after creating server,
request you can request file
 accepts the request using a browser (e.g.,
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 21

You might also like