2 Application SocketProgramming (PDF)




File information


Title: Microsoft PowerPoint - 2_Application_SocketProgramming.pptx
Author: user

This PDF 1.4 document has been generated by PrimoPDF http://www.primopdf.com / Nitro PDF PrimoPDF, and has been sent on pdf-archive.com on 18/12/2015 at 04:48, from IP address 85.245.x.x. The current document download page has been viewed 402 times.
File size: 299.2 KB (8 pages).
Privacy: public file
















File preview


Redes de Computadores
LEIC-A, MEIC-A

2 – Application Layer
Socket Programming
Prof. Paulo Lobato Correia
IST, DEEC – Área Científica de Telecomunicações

Objectives


Socket Programming with TCP and UDP

RC – Prof. Paulo Lobato Correia

2

1

Client-Server Architecture
Server:


Always-on host;



Permanent IP address;



Server farms for scaling.
client/server

Clients:


Communicate with server;



May be intermittently connected;



May have dynamic IP addresses;



Do not communicate directly with
each other.
RC – Prof. Paulo Lobato Correia

3

Socket Programming
Goal: Learn how to build client/server applications that communicate
using sockets
Socket API:




Introduced in BSD4.1 UNIX, 1981;
Client/Server paradigm;
Two types of transport service via socket API:
 Unreliable datagram - UDP;
 Reliable, byte stream-oriented - TCP.

socket
A host, local, application-created, OS-controlled interface
(a “door”) into/from which application process can
send/receive messages to/from another application process.
RC – Prof. Paulo Lobato Correia

4

2

Socket Programming using TCP
Socket: a door between application process and the end-end-transport
protocol (UDP or TCP);
TCP service: reliable transfer of bytes from one process to another.

controlled by
application
developer
controlled by
operating
system

process

process

socket
TCP with
buffers,
variables

socket
TCP with
buffers,
variables

host or
server

internet

controlled by
application
developer
controlled by
operating
system

host or
server
RC – Prof. Paulo Lobato Correia

5

Socket Programming using TCP
Client must contact server:
 Server process must first be running;
 Server must have created socket (door) to welcome client contacts.
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
communication between server and client:



Allows server to talk with multiple clients;
Source port numbers are used to distinguish clients.

TCP provides reliable, in-order transfer of bytes
(“pipe”) between client and server
RC – Prof. Paulo Lobato Correia

6

3

Client/server Socket Interaction: TCP
Server (running on hostid)
create socket,
port=x, for
incoming request:
welcomeSocket =
socket()
wait for incoming
connection request
connectionSocket =
accept()
read request from
connectionSocket
write reply to
connectionSocket
close
connectionSocket

TCP
connection setup

Client

create socket,
connect to hostid, port=x
clientSocket =
socket()
connect ()

send request using
clientSocket

read reply from
clientSocket
close
clientSocket
RC – Prof. Paulo Lobato Correia

7

Client/server Socket Interaction: TCP

RC – Prof. Paulo Lobato Correia

8

4

Sockets: Streams



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.

input
stream

Client
Process
process

output
stream

inFromServer

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

outToServer



monitor

inFromUser

keyboard

input
stream

client
TCP
clientSocket
socket
to network

TCP
socket

from network

RC – Prof. Paulo Lobato Correia

9

Concurrent TCP Servers

RC – Prof. Paulo Lobato Correia

10

5

Socket Programming with UDP
UDP – no “connection” between client and server:
 No handshaking;
 Sender explicitly includes IP address and port of destination to
each packet;
 Server must extract IP address and port of sender from the
received packet.
UDP – transmitted data may be received out of order, or lost!

UDP provides unreliable transfer
of groups of bytes (“datagrams”)
between client and server

RC – Prof. Paulo Lobato Correia

11

Client/server Socket Interaction: UDP
Server (running on hostid)
create socket,
port= x.
serverSocket =
socket(…,SOCK_DGRAM,…)

read datagram from
serverSocket
write reply to
serverSocket
specifying
client address,
port number

Client
create socket,
clientSocket =
socket(…,SOCK_DGRAM,…)

Create datagram with server IP and
port=x; send datagram via
clientSocket

read datagram from
clientSocket
close
clientSocket

RC – Prof. Paulo Lobato Correia

12

6

Client/server Socket Interaction: UDP
Server
Client

RC – Prof. Paulo Lobato Correia

13

Client/server Socket Interaction: UDP

input
stream

Client
process

monitor

inFromUser

keyboard

Process

Input: receives packet

(recall that TCP
received “byte stream”)
UDP
packet

receivePacket

(recall that TCP sent
“byte stream”)

sendPacket

Output: sends packet

client
UDP
clientSocket
socket
to network

UDP
packet

UDP
socket

from network

RC – Prof. Paulo Lobato Correia

14

7

Socket Programming: TCP vs UDP
TCP:


read() and write();



Byte stream (and no byte is lost);
Bytes read with read() may correspond to several write();
Bytes written with write() may need to be read with several
read();




UDP:


sendto() and recvfrom();




Preserves boundary between messages;
Each message read with recvfrom() corresponds to a single
sendto();



A message may be lost.
RC – Prof. Paulo Lobato Correia

15

8






Download 2 Application SocketProgramming



2_Application_SocketProgramming.pdf (PDF, 299.2 KB)


Download PDF







Share this file on social networks



     





Link to this page



Permanent link

Use the permanent link to the download page to share your document on Facebook, Twitter, LinkedIn, or directly with a contact by e-Mail, Messenger, Whatsapp, Line..




Short link

Use the short link to share your document on Twitter or by text message (SMS)




HTML Code

Copy the following HTML code to share your document on a Website or Blog




QR Code to this page


QR Code link to PDF file 2_Application_SocketProgramming.pdf






This file has been shared publicly by a user of PDF Archive.
Document ID: 0000324922.
Report illicit content