Вы находитесь на странице: 1из 5

Overview

qUNIX Socket API overview


qSocket Programming
v Function

The Application Layer:


Socket Programming I

v Example

qDiscuss TCP vs. UDP


v UDP:

CSC 249
Sept 28, 2012

v TCP:

Socket Programming

socket

application

process

transport

transport

network

network

link
physical

Internet

Socket API Overview


q Socket Programming Procedures
v Socket()
v Bind()
v Listen()
v Accept()
v Connect()
v Send and receive procedures
v Close()

Application layer communication via the transport layer


goal: learn how to build client/server applications that
communicate using sockets
socket: door between application process and end-endtransport protocol

process

unreliable datagram
reliable, byte stream-oriented

All material copyright 1996-2012


J.F Kurose and K.W. Ross, All Rights Reserved

application

calls

link

controlled by
app developer

q And for DNS


v getHostByName
v getServByName
v getProtoByName

controlled
by OS

physical

SERVER
socket()

bind()

listen()

SERVER
CLIENT
socket()

bind()

accept()

connect
()

recv()

send()

send()

recv()

socket()

TCP
Flow
Chart

UDP
Flow
Chart

CLIENT
socket()

bind()

bind()

recvfrom()

sendto()

sendto()

recvfrom()

Procedures: Socket()

Socket-programming using TCP


Socket: an interface between application process and
the transport protocol (UCP or TCP)
TCP service: reliable transfer of bytes from one
process to another

q descriptor = socket(protoFamily, type)


v Creates a socket and returns an integer
descriptor
v ProtoFamily refers to Family of protocols that
this protocol belongs to, for TCP/IP use
PF_INET
v Type SOCK_STREAM, SOCK_DGRAM

application

process

SOCK_STREAM Connection Oriented (TCP)


SOCK_DGRAM Connectionless (UDP)

transport
network
link
physical

socket

application

process

TCP: define/reserve
variables and buffers

transport

Internet

link

network

controlled by
app developer
controlled
by OS

physical

A first, simple socket example

Client
Process
process

input
stream

Client contacts server

Server creates new socket

q server process must first be

q when contacted by client, for

running
q server must have created
socket (door) that accepts
clients contact

output
stream

inFromServer

Client contacts server by:


outToServer

1) The client reads a line from


standard input (inFromUser
stream), sends it to server via
socket (outToServer stream)
2) The server reads the line from its
socket
3) The server converts the line to
uppercase, and sends it back to the
client
4) The client reads the modified line
from its socket (inFromServer
stream) and prints it to standard
output

monitor

inFromUser

keyboard

Client-server simple application:

Client/server socket interaction: TCP

client
TCP
clientSocket
socket
to network

q Creating TCP socket,


v Reserve required memory
v Specify server IP address and
port number
q Have client TCP layer

input
stream

establish connection to
server TCP layer (via TCP
handshaking, chap 3)

TCP
socket

from network

server process to communicate


with that particular client
v allows server to talk with
multiple clients
v source port numbers used to
distinguish clients (more in
Chap 3)

Application viewpoint:
TCP provides reliable, in-order
byte-stream transfer (pipe)
between client and server

Example app: TCP client

Client/server socket interaction: TCP


Server

(running on hostid)

10

Python TCPClient

Client

create socket,
port=x, for
incoming request:

serverSocket = socket()
wait for incoming
connection request

TCP
connection setup

connectionSocket =
serverSocket.accept()
read request from
connectionSocket
write reply to
connectionSocket
close
connectionSocket

create TCP socket for


server, remote port 12000

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

No need to attach server


name, port

send request using


clientSocket

from socket import *


serverName = servername
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = raw_input(Input lowercase sentence:)
clientSocket.send(sentence)
modifiedSentence = clientSocket.recv(1024)
print From Server:, modifiedSentence
clientSocket.close()

read reply from


clientSocket
close
clientSocket
11

Application Layer

2-12

Example app: TCP server

Socket programming with UDP

Python TCPServer

read bytes from socket (but


not address as in UDP)
close connection to this
client (but not welcoming
socket)

sentence = connectionSocket.recv(1024)
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence)
connectionSocket.close()

Application Layer

application viewpoint

UDP provides unreliable transfer


of groups of bytes (datagrams)
between client and server
2-13

Client/server socket interaction: UDP


server (running on serverIP)
create socket, port= x:
serverSocket =
socket(AF_INET,SOCK_DGRAM)

read datagram from


serverSocket
write reply to
serverSocket
specifying
client address,
port number

UDP: transmitted data may be received out of order,


or lost

14

Example: UDP client


keyboard

client
create socket:
clientSocket =
socket(AF_INET,SOCK_DGRAM)
Create datagram with server IP and
port=x; send datagram via
clientSocket

input
stream

Client
Process

monitor

Input: receives

process

packet (recall
that TCP sent
byte stream)

UDP
packet

client
UDP
clientSocket
socket

read datagram from


clientSocket

to network

close
clientSocket
Application 2-15

packet (recall that


TCP received
byte stream)

Output: sends

receivePacket

loop forever
server waits on accept()
for incoming requests, new
socket created on return

inFromUser

server begins listening for


incoming TCP requests

UDP: no connection between client and server


q no handshaking
q sender explicitly attaches IP address and port of
destination to each packet
q server must extract IP address, port of sender from
received packet

sendPacket

create TCP welcoming


socket

from socket import *


serverPort = 12000
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind((,serverPort))
serverSocket.listen(1)
print The server is ready to receive
while 1:
connectionSocket, addr = serverSocket.accept()

UDP
packet

UDP
socket

from network

16

Example app: UDP client


include Pythons socket
library

create UDP socket for


server
get user keyboard
input
Attach server name, port to
message; send into socket
read reply characters from
socket into string
print out received string
and close socket

Example app: UDP server

Python UDPClient

Python UDPServer

from socket import *


serverName = hostname
serverPort = 12000
clientSocket = socket(socket.AF_INET,
socket.SOCK_DGRAM)
message = raw_input(Input lowercase sentence:)
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress =
clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()

from socket import *


serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print The server is ready to receive
while 1:

Application Layer

2-17

create UDP socket


bind socket to local port
number 12000
loop forever
Read from UDP socket into
message, getting clients
address (client IP and port)
send upper case string
back to this client

message, clientAddress = serverSocket.recvfrom(2048)


modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage, clientAddress)

Application Layer

2-18

Socket Summary
qSockets defined
v Through analogy to a door
An interface between the application and
the Internet
v Through

examples with socket-API


programming
v Through understanding the differences
between TCP and UDP sockets

19

Вам также может понравиться