Title: TCPIP Socket Programming
1TCP/IP Socket Programming
- ??? ????????
- ??????????????????????????
- ??????????????????????
2????????????????????
Service Access Point
Applications
1 2 ( ) ( )
Applications
Transport
Network Address
1 2 3 4 ( ) ( ) ( ) ( )
Transport
Network access
Communication network
Network access
Network Address ?????????????? ?????????????????
??????????????????? Service Access Point
?????????????? ??? ???????????????????????????????
?
Applications
1 2 3 ( ) ( ) ( )
Transport
Network access
3????????????????????????????????
OSI
TCP/IP
4Network Address
- ?????? TCP/IP ???????? 3 ????? ???
- Host names
- ??????????????????/????????? . ????
nontri.ku.ac.th - Internet (IP) address
- ????????????????????? 32 Bits ??? . ????
158.108.2.71 - station Address - hardware address ( MAC Address
) - ????????????????? interface card
- ????????????????????? 48 Bits ??? ????
00c6134a
5Service Access Point (SAP)
- ?????? TCP/IP ?????????? Port
- Port ??????????? 16 ??? ( 0 - 65535 )
- ????????? Port
- 0 ????????? ( ??????? ? ?????????????????????
Port ????????????????? ) - 1-255 ???????????????????????????????????????
???? FTP 20 , TELNET 23 , SMTP 25 - 256-1023 ?????????????????????? ?
- 1024-65535 ????????? USER ?????????
- ???????? UNIX ?????? port ????????????????????????
/etc/services
6Socket
- socket ?????? IP address ?????????? port
IP address
port number
lt158.108.33.3, 3000gt
IP address ??? Node ?????????????????????????
Port ?????????????????? Node Socket
????????????????????????????????????? Application
7??????? Socket
- TCP/IP ( AF_INET , Internet )
- Unix ( AF_UNIX )
- XNS ( AF_NS )
- Appletalk
- IPX
- etc...
8????? byte ordering
- ???????????????????????? byte ordering
??????????????????? 2 ????? - little endian Intel 80x86 , DEC VAX , DEC
PDP-11 - big endian IBM 370 , Motorola 68000 , Pyramid
high-order byte
low-order byte
little endian
addr A
addr A1
high-order byte
low-order byte
big endian
addr A
addr A1
???????? Network Byte Order ???? big endian.
9Byte Ordering Routine
- include ltnetinet/in.hgt
- u_long htonl (u_long hostlong)
- ?????????????????? host ????? network (long int)
- u_short htons (u_short hostshort)
- ?????????????????? host ????? network (short int)
- u_long ntohl (u_long netlong)
- ?????????????????? network ????? host (long int)
- u_short ntohs (u_short netshort)
- ?????????????????? network ????? host (short int)
10Socket Address
- Connection ???????????? socket address
???????????????????????? - client socket 158.108.33.3,3000
158.108.2.71,21 - server socket 158.108.2.71,21
158.108.33.3,3000
IP 158.108.2.71
IP 158.108.33.3
connection
port 21
port 3000
server
client
11Generic Socket Address
struct sockaddr sa_family_t sa_family
char sa_data14
sa_family ???? address type. sa_data ????
address value.
12TCP/IP Socket Address (IP v4)
struct in_addr u_long s_addr / 32 ??? Net ID
/ Host ID / struct sockaddr_in short
sin_family / AF_INET / u_short
sin_port / ??????? Port 16 ??? / struct
in_addr sin_addr / 32 ??? Net ID / Host ID
/ char sin_zero8 / ???????????? /
???????? ???? sin_port ??? sin_addr ????????
Network Byte Order
13??????????????? sockaddr
- ????????????????????????????? structure ???
sockaddr ??????????????? sockaddr_in ???
sockaddr
sockaddr_in
14???????????????????????? TCP/IP
- ???????? 2 ???? ??? Connection-Oriented ???
Connectionless - Connection-oriented
- ??????????????????????????????????????????????????
??????? ???? TCP - Connectionless
- ??????????????????????????????????????????????????
???? ???? UDP
15Connection-oriented protocol
server
socket()
bind()
listen()
client
accept()
blocks until connection from client
socket()
connection establishment
connect()
data (request)
read()
write()
data(reply)
write()
read()
16Connectionless protocol
Server
socket()
Client
bind()
socket()
recvfrom()
bind()
blocks until data received from a client
data (request)
sendto()
process request
data (reply)
sendto()
recvfrom()
17Socket System Call (IP v4)
include ltsys/types.hgt include
ltsys/socket.hgt int socket (int family, int type,
int protocol)
Return Value -1 ??????? ERROR ??????????? Error
?? return descriptor ?????????????????? socket
????
18Protocol
- Parameter Protocol ????????????????????? 0
(IPPROTO_IP) ??????????????????? default ???
family ??? types ???? - ??????????? ICMP ?????????????????????????????????
?????????? default ???
19Unix Descriptor Table
Descriptor Table
Data structure for file 0
0
1
Data structure for file 1
2
3
Data structure for file 2
4
20Socket Descriptor Data Structure
Descriptor Table
Family PF_INET Service SOCK_STREAM Local IP
158.108.33.66 Remote IP 158.108.2.71 Local
Port 2249 Remote Port 21
0
1
2
3
4
???????? ?????? Family PF_INET
?????????????????????? AF_INET
21Assigning an address to a socket
- ??? bind( ) ?????????? IP Address ??? Port ??????
socket - Server ??????????????????????? address
????????????????? client ???????????? - ?????????????????? connectionless ???????????????
Address ?????? client ???????? server
???????????????????? - ???????????? OS ???????????? port
??????????????????????????????????? port ???? 0
22Bind System Call
include ltsys/types.hgt include
ltsys/socket.hgt int bind(int sockfd, struct
sockaddr myaddr, int addrlen)
myaddr pointer protocol-specific
address addrlen size of address structure.
Return Value -1 ??????? Error 0 ??????????
Error
23What is my IP Address ?
- ???????????????????? IP Address ??? server ??????
bind( ) ????????? structure sockaddr - ???????????????? IP Address ?????? bind( )
??????? IP Address ????? ? ??? ( ??????????????
connection ??? Interface ?????????????????????????
Interface ( multihome ) - ??????? IP Address ??? server ???? INADDR_ANY (
???????????????????? Interface ???????? )
24Socket Bind Example
- include ltgt
- define SERV_TCP_PORT 2499
- int sockfd , err
- struct sockaddr_in serv_addr
- sockfd socket(AF_INET, SOCK_STREAM, 0)
- if(sockfd lt 0)
- puts("server can't open stream socket")
- exit(1)
25Socket Bind Example
- bzero((char ) serv_addr, sizeof(serv_addr))
- serv_addr.sin_family AF_INET
- serv_addr.sin_addr.s_addr htonl(INADDR_ANY)
- serv_addr.sin_port
htons(SERV_TCP_PORT) - errbind(sockfd, (struct sockaddr ) serv_addr,
sizeof(serv_addr)) - if(err lt 0 )
- puts("server can't bind local address")
- exit(1)
-
bzero ???? function ??? ??? ?????? set
?????????? 0 ( NULL )
26Socket Descriptor Data Structure
Descriptor Table
Family AF_INET Service SOCK_STREAM Local IP
INADDR_ANY Remote IP Local Port 2499 Remote
Port
0
1
2
...
sockfd
27Other Socket System Calls
- Connection-oriented (TCP)
- connect()
- listen()
- accept()
- send() , sendto() , sendmsg()
- recv() , recvfrom() , recvmsg()
- General Use
- read()
- write()
- close()
- Connectionless (UDP)
- sendto() , sendmsg()
- recvfrom() , recvmsg()
28TCP Socket Programming
29UDP Socket Programming
30Socket multiple connection
- Server 1 ???????????? Connection ??? client
?????????? 1 ???????? ? ???