socket 通讯的概念性过程

类别:编程语言 点击:0 评论:0 推荐:
1.SOCKET 通讯TCP方式一般流程图

服务端                                                                             客户端
socket()                                                                           socket()             
       |                                                                                       |
       |-------------------------setsockopt()-----------------------|
       |                                                                                       |
   bind()                                                                             connect()
       |                                                                                       |
       |                                                                                       |
   listen()                                                                           recv()/send()/write()
       |
       |
  accept()
       |
       |
       |
recv()/send()/read()
setsockopt的可用level为SOL_SOCKET:
option为:SO_REUSEADDR  选项就是可以实现端口重绑定的
 其他的选项可以使用也可以不用.

2.SOCKET 通讯UDP方式一般流程图

服务端                                                                             客户端
socket()                                                                           socket()             
       |                                                                                       |
       |-------------------------setsockopt()---------------------- |
       |                                                                                       |
   bind()                                                                          sendto()
       |                                                                                       
       |                                                                                     
recvfrom()
 如果是广播,则
 setsockopt的可用level为SOL_SOCKET:
option为:SO_BROADCAST  选项就是该SOCKET 是用于广播的.
 sendto地址可以为:255.255.255.255 受限地址
                                netid.255.255  (B类地址)该网络号下所有子网
                                10.128.1.255 (B类地址)该1子网的广播
                                10.128.255.255 (B类地址)所有子网的广播

如果是多播,则
 setsockopt的可用level为IPPROTO_IP: IP_ADD_MEMBERSHIP: 加入一个多播组 IP_DROP_MEMBERSHIP: 离开一个多播组 IP_MULTICAST_IF: 指定外出多播数据报的外出接口 IP_MULTICAST_TTL: TTL数 IP_MULTICAST_LOOP: 是否禁止回馈,我的理解是一台机器是否可以接收到自己发送的多播数据报
struct ip_mreq 为IP_ADD_MEMBERSHIP的数据结构!

   多播地址为224.0.0.0.1~239.255.255.255

本文地址:http://com.8s8s.com/it/it26028.htm