Socket Addresses
Download
Report
Transcript Socket Addresses
Socket Addresses
Domains
• Internet domains
– familiar with these
• Unix domains
– for processes communicating on the same hosts
– not sure of widespread use
– used to debug internally
Protocol families
•
•
•
•
•
•
SNA - IBM Systems Network Architecture
UUCP - Unix to Unix copy
XNS - Xerox Network System
NETBIOS - IBM Basic I/O system
TCP/IP (PF_INET)
UNIX (PF_UNIX)
Constants for addresses
•
•
•
•
•
•
•
•
PF_UNIX
PF_INET
PF_NS
PF_SNA
PF_DECnet
PF_APPLETALK
PF_X25
…...
1
2
6 (xns)
11
12
16
20
Address Formation
All Protocols do not agree
TCP/IP
Socket Abstraction
IP Address
Protocol Port
Address
format
may be
different
Unix socket
Other Protocols
Built to support
multiple protocols
Address Families
• Socket abstraction has a series of address
families so that each family can have it’s own
format for endpoint addresses.
• TCP/IP has address family named AF_INET
• TCP/IP also has a protocol family : PF_INET
• Both have numeric value 2 which leads to
further confusion .. They can be switched at
will with no apparent consequence.
Generic Address
• (address family, endpoint address in family)
• socket API defines one to use as a general
address, but it won’t work for everything
• Works for TCP/IP not Unix
struct sockaddr {
u_short sa_family;
char sa_data[14];
};
E.g. Unix sockets (internal)
use a named pipe for the endpoint
and the length of it is comparable
to a path/file name (much longer)
What TCP/IP uses
for an address
struct sock_addr_in{
u_short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8]
}
• 2 byte address
family (AF_INET)
• 2 byte port number
• 4 byte IP address
• 8 bytes unused
TCP/IP apps need not use sockaddr
Use sockaddr_in
Procedures in API
socket()
• Purpose: create a socket for communications
• Returns: descriptor for a new socket
• Arguments:
– protocol family PF_INET
– type of service
• stream (TCP)
• datagram (UDP)
connect()
• Purpose: establish the connection
• Returns: success/failure
• Arguments:
– remote endpoint
send()
• Purpose: send data through to other end
• Returns: success/failure
• Arguments:
–
–
–
–
socket descriptor
address
length of data
control directives
recv()
• Purpose: get response
• Returns: message received
• Arguments:
–
–
–
–
socket descriptor
address of buffer
length of buffer
control bits
• NOTE - recv is limited to buffer space and
will cut off if necessary. Must go back
recv() (UDP)
• Works similarly
• If too much data:
– truncated at the size of the receive buffer
– discard remainder
– return error condition
closesocket()
• Purpose: give socket resource back to OS
• if shared with other processes, reference
count decremented (until 0).
• Returns: message received
• Arguments:
–
–
–
–
socket descriptor
address of buffer
length of buffer
control bits
bind()
• Purpose: specify local endpoint address
• Returns: message received
• Arguments:
– socket descriptor
– endpoint address (sockaddr_in for TCP/IP)
• IP address
• port
listen()
• Purpose: put socket in passive mode and
specify size of queue for incoming requests
• Returns: success/fail
• Arguments:
– socket descriptor
– size of queue
accept()
• Purpose: wait for next incoming request
• Returns: new socket for interaction
• Arguments:
– socket descriptor
Windows client-server usage
CLIENT
SERVER
WSAStartup
WSAStartup
socket
socket
connect
bind
send
listen
recv
accept
closesocket
recv
WSACleanUP
send
closesocket
WSACleanup