




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Wenyuan Xu
Department of Computer Science and Engineering University of South Carolina
9/8/2008 CSCE515 โ Computer Network Programming
Process Process
ICMP, ARP & RARP
9/8/2008 CSCE515 โ Computer Network Programming
ย 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
OSI model (^) protocol suiteInternet Application Presentation Session Transport Network Data link Physical
TCP IPv4, IPv
Application
Data link Physical
UDP
processorUser
kernel
Applicationdetails
Communicationsdetails
ย 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
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
ย 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
9/8/2008 CSCE515 โ Computer Network Programming
ย 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
Source IP address
Source port number
Destination IP address
Destination port number
An end-to-end protocol (TCP or UDP)
Stream sockets
Datagram 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
9/8/2008 CSCE515 โ Computer Network Programming
9/8/2008 CSCE515 โ Computer Network Programming
bind( mysock, (struct sockaddr) &myaddr, sizeof(myaddr) );*
9/8/2008 CSCE515 โ Computer Network Programming
Necessary Background Information: POSIX data types
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
ย 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
struct sockaddr henry;
henry.sa_family = AF_CSCE515; henry.sa_data[0] = 5;
9/8/2008 CSCE515 โ Computer Network Programming
16 bit port number
32 bit IP address
9/8/2008 CSCE515 โ Computer Network Programming
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 { in_addr_t s_addr; };
in_addr just provides a name for the โCโ type associated with IP addresses.
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
sin_port
sin_addr
sin_zero
9/8/2008 CSCE515 โ Computer Network Programming
Assigning an address to a socket
*int bind( int sockfd, const struct sockaddr myaddr, int addrlen);
const!
9/8/2008 CSCE515 โ Computer Network Programming
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
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.
ย 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
ย 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
**int inet_aton( char , struct in_addr );
*char inet_ntoa(struct in_addr);
9/8/2008 CSCE515 โ Computer Network Programming
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
read()
write()
close()
UNP1, 3**
Socket Programming FAQ
TCP Details