Transcript Document
Overview
Last Lecture
» TCP/UDP (2)
» Source: chapter 17
This Lecture
» Socket programming
» Source: chapter 17
Next Lecture
» Internet applications (1)
» Source: chapter 19
TELE202 Lecture 15 Socket programming
1
Lecturer Dr Z. Huang
Socket programming
Socket is a UNIX construct and is the
basis for UNIX network I/O
» It is available under MS Windows as well, i.e.
WinSock 2
Developed by UC Berkeley
Originally used in UCB Unix as an
API for TCP and UDP
Socket and its related primitives are
transport interface to higher layer
applications
Socket is a communication identifier
consisting of a local port number and
an Internet address
TELE202 Lecture 15 Socket programming
2
Lecturer Dr Z. Huang
Socket programming
Client/server model
» Involves two distinct programs running on
different machines at different locations
» They have network connections
» The server provides services and responds to
requests coming in from clients
Primitives for socket
»
»
»
»
»
»
»
»
socket: create a communication end point
bind: attach a local address to a socket
listen: announce number of connections
accept: waiting for a connection call
connect: attempt to establish a connection
send/write: send data over the connection
recv/read: receive data from the connection
close: close the connection
TELE202 Lecture 15 Socket programming
3
Lecturer Dr Z. Huang
Data structures
Relevant data structures
» Socket address
struct sockaddr_in {
u_short sin_family; /* protocol type *
u_short sin_port; /* port number */
struct in_addr sin_addr; /* 4 bytes IP
address */
char sin_zero[8]; /* unused, all zeros */
}
struct in_addr {
u_int s_addr
}
» Host information
Struct hostent {
char *h_name; /* domain name of a host
*/
char **h_alias; /* alternative names of a
host */
int h_addrtype; /* address type */
int h_length; /* address length */
char **h_addr_list; /* address list */
}
TELE202 Lecture 15 Socket programming
4
Lecturer Dr Z. Huang
Create/close a socket
The socket function creates sockets
on demand
» result = socket(pf, type, protocol)
» pf specifies the protocol family to be used
with the socket
– PF_INET, TCP/IP internet
– PF_UNIX, UNIX file system
– PF_APPLETALK, AppleTalk network
» type specifies the type of communication
desired
– SOCK_STREAM, reliable stream
delivery service
– SOCK_DGRAM, connectionless
datagram delivery service
– SOCK_RAW, a raw type that allows
privileged programs to access low-level
protocols or network interfaces
» protocol is used to select a specific protocol in
the protocol family if there are multiple
protocols to support the same service type
The close function is called when a
process finishes using a socket
» close(socket)
» socket specifies the descriptor of a socket to
close
TELE202 Lecture 15 Socket programming
5
Lecturer Dr Z. Huang
Socket inheritance
TELE202 Lecture 15 Socket programming
6
Lecturer Dr Z. Huang
functions
Auxiliary functions
» gethostbyname(char *hostname): return a
hostent structure containing information
about a host named by hostname
» gethostname(char *hostname, int length): get
the domain name of the local host and put it
in hostname
» getsockname(int sock, struct sockaddr *addr,
int addr_length): after a socket is bound by
bind, this function is used to get the address
information of the socket sock, and put it into
addr
» bcopy(a1, a2, length): copy length bytes from
a1 to a2
» htons(int port_number): convert the
port_number into a standard format of port
number
TELE202 Lecture 15 Socket programming
7
Lecturer Dr Z. Huang
Summary
TELE202 Lecture 15 Socket programming
8
Lecturer Dr Z. Huang