Title: TCP???????
1TCP???????
- ????
- http//okaweb.ec.kyushu-u.ac.jp/lectures/in-ng/
2TCP???????(????)
include ltstdio.hgt include ltstrings.hgt include
ltsys/types.hgt include ltsys/socket.hgt include
ltnetinet/in.hgt main(narg,arg) int narg
char arg int sock,sock2,saddr2_len int
s_addr unsigned short port struct
sockaddr_in saddr,saddr2 s_addrinet_addr(arg
1) portatoi(arg2) bzero(saddr,sizeof(s
truct sockaddr_in)) bzero(saddr2,sizeof(struct
sockaddr_in)) saddr.sin_family
AF_INET saddr.sin_addr.s_addr s_addr
saddr.sin_port htons(port)
socksocket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
switch (arg30) case 'b'
if(bind(sock, (struct sockaddr )saddr,sizeof(sad
dr)) lt 0) perror("TCPbind ")
return -1 listen(sock,8)
sock2accept(sock,(struct sockaddr
)saddr2,saddr2_len) printf("connected
from s.\n",inet_ntoa(saddr2.sin_addr))
break case 'c' if(connect(sock, (struct
sockaddr )saddr,sizeof(saddr)) lt 0)
perror("TCPconnect ") return -1
getchar() break close (sock)
3TCP ? UDP ???(bind)
- UDP
- bind ??????ID?????????
- TCP
- bind ???????ID ? accept ??????????????????accept
??????????ID???????? ID ????????????
4bind ??????IP????
saddr.sin_family AF_INET saddr.sin_addr.s_
addr s_addr saddr.sin_port
htons(port) socksocket(AF_INET, SOCK_STREAM,
IPPROTO_TCP) if(bind(sock, (struct sockaddr
)saddr,sizeof(saddr)) lt 0)
perror("TCPbind ")
?????????????????? ???????????????????????????????
???????? 0.0.0.0 ?????????????????????????????????
????????
5TCP ? UDP ???(connect)
- UDP
- ????? bind ??????? connect ??????
- TCP
- connect ????????????????? bind/listen/accept
??????????
6????????????
- TCP ? UDP ???? write() ??????????????????????read(
)??????????????????????? - ????TCP ? bind ??????ID?????accept ??????ID?
7??
- UDP ???????? EOF ?????????????? TCP ???????????
- ????????????????????
- ?Mbyte??????????????????????