TCPIP Socket Programming - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

TCPIP Socket Programming

Description:

station Address - hardware address ( MAC Address ) ????????????????? interface card ... ??? bind( ) ?????????? IP Address ??? Port ?????? socket ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 31
Provided by: Julla
Category:

less

Transcript and Presenter's Notes

Title: TCPIP Socket Programming


1
TCP/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
4
Network 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

5
Service 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

6
Socket
  • 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.
9
Byte 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)

10
Socket 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
11
Generic Socket Address
struct sockaddr sa_family_t sa_family
char sa_data14
sa_family ???? address type. sa_data ????
address value.
12
TCP/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

15
Connection-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()
16
Connectionless protocol
Server
socket()
Client
bind()
socket()
recvfrom()
bind()
blocks until data received from a client
data (request)
sendto()
process request
data (reply)
sendto()
recvfrom()
17
Socket 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
????
18
Protocol
  • Parameter Protocol ????????????????????? 0
    (IPPROTO_IP) ??????????????????? default ???
    family ??? types ????
  • ??????????? ICMP ?????????????????????????????????
    ?????????? default ???

19
Unix Descriptor Table
Descriptor Table
Data structure for file 0
0
1
Data structure for file 1
2
3
Data structure for file 2
4
20
Socket 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
21
Assigning an address to a socket
  • ??? bind( ) ?????????? IP Address ??? Port ??????
    socket
  • Server ??????????????????????? address
    ????????????????? client ????????????
  • ?????????????????? connectionless ???????????????
    Address ?????? client ???????? server
    ????????????????????
  • ???????????? OS ???????????? port
    ??????????????????????????????????? port ???? 0

22
Bind 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
23
What 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 ???????? )

24
Socket 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)

25
Socket 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 )
26
Socket 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
27
Other 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()

28
TCP Socket Programming
29
UDP Socket Programming
30
Socket multiple connection
  • Server 1 ???????????? Connection ??? client
    ?????????? 1 ???????? ? ???
Write a Comment
User Comments (0)
About PowerShow.com