Lecture Slides on Sockets - Computer Network Programming | CSCE 515, Study notes of Computer Science

Material Type: Notes; Professor: Xu; Class: COMPUTR NETWRK PROGRAMNG; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Fall 2008;

Typology: Study notes

Pre 2010

Uploaded on 09/02/2009

koofers-user-dkv
koofers-user-dkv 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCE 515:
Computer Network
Programming
------ Sockets
Wenyuan Xu
Department of Computer Science and Engineering
University of South Carolina
CSCE515 – Computer Network Programming9/8/2008
TCP UDP
IP
802.3
Process Layer
Transport Layer
Network Layer
Data-Link Layer
Process Process
ICMP, ARP
&
RARP
CSCE515 – Computer Network Programming9/8/2008
Network API
API - Application Programming Interface
API is a set of functionality/services delivered by a
programming system.
Network API
The services ( often provided by the operating system)
that provide the interface between application and
protocol software.
CSCE515 – Computer Network Programming9/8/2008
Network API
OSI model Internet
protocol suite
Application
Presentation
Session
Transport
Network
Data link
Physical
TCP
IPv4, IPv6
Application
Data link
Physical
UDP
User
processor
kernel
Application
details
Communications
details
CSCE515 – Computer Network Programming9/8/2008
Network API wish list
Generic Programming Interface.
Support multiple communication protocol suites
(families).
Address (endpoint) representation independence.
Provide special services for Client and Server?
Support for message oriented and connection
oriented communication.
Work with existing I/O services (when this
makes sense).
Operating System independence
CSCE515 – Computer Network Programming9/8/2008
TCP/IP
TCP/IP does not include an API definition.
There are a variety of APIs for use with
TCP/IP:
Sockets by Berkeley
XTI (X/Open Transport Interface) by AT&T
Winsock - Windows Sockets API by Microsoft
MacTCP / Open Transport by Apple
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lecture Slides on Sockets - Computer Network Programming | CSCE 515 and more Study notes Computer Science in PDF only on Docsity!

CSCE 515:

Computer Network

Programming

------ Sockets

Wenyuan Xu

Department of Computer Science and Engineering University of South Carolina

9/8/2008 CSCE515 – Computer Network Programming

TCP UDP

IP

Process Layer

Transport Layer

Network Layer

Data-Link Layer

Process Process

ICMP, ARP & RARP

9/8/2008 CSCE515 – Computer Network Programming

Network API

„ API - Application Programming Interface

API is a set of functionality/services delivered by a programming system.

„ Network API

The services ( often provided by the operating system) that provide the interface between application and protocol software.

9/8/2008 CSCE515 – Computer Network Programming

Network API

OSI model (^) protocol suiteInternet Application Presentation Session Transport Network Data link Physical

TCP IPv4, IPv

Application

Data link Physical

UDP

processorUser

kernel

Applicationdetails

Communicationsdetails

Network API wish list

„ Generic Programming Interface.

Support multiple communication protocol suites (families).

Address (endpoint) representation independence.

Provide special services for Client and Server?

„ Support for message oriented and connection oriented communication. „ Work with existing I/O services (when this makes sense). „ Operating System independence

TCP/IP

„ TCP/IP does not include an API definition.

„ There are a variety of APIs for use with

TCP/IP:

Sockets by Berkeley

XTI (X/Open Transport Interface) by AT&T

Winsock - Windows Sockets API by Microsoft

MacTCP / Open Transport by Apple

9/8/2008 CSCE515 – Computer Network Programming

Client-Server Model

„ One side of communication is client, and the other side is server „ Server waits for a client request to arrive „ Server processes the client request and sends the response back to the client „ Iterative or concurrent

Server

Client 1 Client 2 Client 3

9/8/2008 CSCE515 – Computer Network Programming

Functions needed:

„ Specify local and remote communication

endpoints

„ Initiate a connection

„ Wait for incoming connection

„ Send and receive data

„ Terminate a connection gracefully

„ Error handling

9/8/2008 CSCE515 – Computer Network Programming

Berkeley Sockets

„ A socket is an abstract representation of a communication endpoint.

„ Generic:

support for multiple protocol families.

address representation independence

„ Sockets (obviously) have special needs:

establishing a connection

specifying communication endpoint addresses

„ Sockets work with Unix I/O services just like files, pipes & FIFOs 9/8/2008 CSCE515 – Computer Network Programming

Elements of a Socket

„ Each socket can be uniquely identified by

Source IP address

Source port number

Destination IP address

Destination port number

An end-to-end protocol (TCP or UDP)

Types of Sockets

„ Two different types of sockets

Stream sockets

Datagram sockets

Stream Sockets

„ Also known as connection-oriented socket „ Use TCP „ Provide reliable, connected networking service „ Error free; no out-of-order packets „ Applications: telnet, ssh, http

9/8/2008 CSCE515 – Computer Network Programming

socket()

„ The socket() system call returns a

socket descriptor (small integer) or -1 on

error.

„ socket() allocates resources needed for

a communication endpoint - but it does not

deal with endpoint addressing.

9/8/2008 CSCE515 – Computer Network Programming

Specifying an Endpoint Address

„ Remember that the sockets API is generic

„ There must be a generic way to specify

endpoint addresses.

„ TCP/IP requires an IP address and a port

number for each endpoint address.

9/8/2008 CSCE515 – Computer Network Programming

bind()

„ calling bind() assigns the address

specified by the sockaddr structure to the

socket descriptor.

bind( mysock, (struct sockaddr) &myaddr, sizeof(myaddr) );*

9/8/2008 CSCE515 – Computer Network Programming

Necessary Background Information: POSIX data types

int8_t signed 8bit int

uint8_t unsigned 8 bit int

int16_t signed 16 bit int

uint16_t unsigned 16 bit int

int32_t signed 32 bit int

uint32_t unsigned 32 bit int

More POSIX data types

sa_family_t address family

socklen_t length of struct

in_addr_t IPv4 address

in_port_t IP port number

Generic socket addresses

struct sockaddr { uint8_t sa_len; sa_family_t sa_family; char sa_data[14]; };

„ sa_family specifies the address type. „ sa_data specifies the address value.

Used by kernel

9/8/2008 CSCE515 – Computer Network Programming

sockaddr

„ An address that will allow me to use sockets to communicate with you. „ address type AF_CSCE „ address values: Dean 1 Sayan 6 Devon 2 Yuliya 7 Samuel 3 Razvan 8 Shamik 4 Mythri 9 Henry 5 Femitolu 10

9/8/2008 CSCE515 – Computer Network Programming

AF_CSCE

„ Initializing a sockaddr structure to point

to Henry :

struct sockaddr henry;

henry.sa_family = AF_CSCE515; henry.sa_data[0] = 5;

9/8/2008 CSCE515 – Computer Network Programming

AF_INET

„ For AF_CSCE515 we only needed 1 byte

to specify the address.

„ For AF_INET we need:

16 bit port number

32 bit IP address

IPv4 only!

9/8/2008 CSCE515 – Computer Network Programming

struct sockaddr_in (IPv4)

struct sockaddr_in { uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8]; }; A special kind of sockaddr structure

struct in_addr

struct in_addr { in_addr_t s_addr; };

in_addr just provides a name for the ‘C’ type associated with IP addresses.

Byte Ordering

„ Different computer architectures use

different byte ordering to represent

multibyte values.

„ 16 bit integer:

Low Byte High Byte

High Byte Low Byte

Address AAddress A Address A+1Address A+

9/8/2008 CSCE515 – Computer Network Programming

sa_lensa_len sin_len sin_len sa_familysa_family

sa_datasa_data

AF_INET

sin_port

sin_addr

sin_zero

sockaddrsockaddr^ sockaddr_insockaddr_in

9/8/2008 CSCE515 – Computer Network Programming

Assigning an address to a socket

„ The bind() system call is used to assign

an address to an existing socket.

*int bind( int sockfd, const struct sockaddr myaddr, int addrlen);

„ bind returns 0 if successful or -1 on error.

const!

9/8/2008 CSCE515 – Computer Network Programming

bind() Example

int mysock,err; struct sockaddr_in myaddr;

mysock = socket(PF_INET,SOCK_STREAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons( portnum ); myaddr.sin_addr = htonl( ipaddress);

*err=bind(mysock, (sockaddr ) &myaddr, sizeof(myaddr));

Why no htons/htosl?Why no htons/htosl?Why no htons/htosl?

9/8/2008 CSCE515 – Computer Network Programming

Uses for bind()

„ There are a number of uses for bind() :

Server would like to bind to a well known address (port number).

Client can bind to a specific port.

Client can ask the OS to assign any available port number.

Port schmo - who cares?

„ Clients typically don’t care what port they are assigned.

„ When you call bind you can tell it to assign you any available port:

myaddr.port = htons(0);

1-1024: reserved port (assigned by privileged processes)

Why htons? 0 is 1 byteWhy htons? 0 is 1 byte

What is my IP address?

„ How can you find out what your IP address is so you can tell bind()?

„ There is no realistic way for you to know the right IP address to give bind() - what if the computer has multiple network interfaces?

„ specify the IP address as: INADDR_ANY , this tells the OS to take care of things. myaddr.sin_addr.s_addr = htonl( INADDR_ANY );

1 byte, Why htonl?

9/8/2008 CSCE515 – Computer Network Programming

IPv4 Address Conversion

**int inet_aton( char , struct in_addr );

Convert ASCII dotted-decimal IP address to

network byte order 32 bit value. Returns 1

on success, 0 on failure.

*char inet_ntoa(struct in_addr);

Convert network byte ordered value to

ASCII dotted-decimal (a string).

9/8/2008 CSCE515 – Computer Network Programming

Client-Server Communication (TCP)

socket() bind() listen() accept()

read()

write()

read() close()

socket() connect() write()

read() close()

TCP Client

TCP Server well-known port

blocks until connection from client

process request

connection establishment data(request)

data(reply) end-of-file notification

int socket(int family, int type, int protocol); int bind(int sockfd, struct sockaddr *my_addr, int addrlen); int listen(int sockfd, int backlog); int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);int accept(int sockfd, void *addr, int *addrlen);

int close(int sockfd); int close(int sockfd);

int socket(int family, int type, int protocol);

9/8/2008 CSCE515 – Computer Network Programming

Other socket system calls

„ General Use

read()

write()

close()

  • • ConnectionConnection--orientedoriented

(TCP)(TCP)

  • connect()connect()
  • listen()listen()
  • accept()accept()
  • Connectionless (UDP) • Connectionless (UDP)
  • send()send()
  • –^ recvrecv()() 9/8/2008 2007 CSCE515 – Computer Network ProgrammingCSCE515 – Computer Network Programming

Assignment & Next time

„ Reading:

UNP1, 3**

Socket Programming FAQ

„ Next Lecture:

TCP Details