编程语言

img jsphuang

socket 通讯的概念性过程

发表于2004/10/30 22:23:00  1637人阅读

分类: 技术专栏

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:
  1. IP_ADD_MEMBERSHIP: 加入一个多播组
  2. IP_DROP_MEMBERSHIP: 离开一个多播组
  3. IP_MULTICAST_IF: 指定外出多播数据报的外出接口
  4. IP_MULTICAST_TTL: TTL数
  5. IP_MULTICAST_LOOP: 是否禁止回馈,我的理解是一台机器是否可以接收到自己发送的多播数据报
    struct ip_mreq 为IP_ADD_MEMBERSHIP的数据结构!

   多播地址为224.0.0.0.1~239.255.255.255

阅读全文
0 0

相关文章推荐

img
取 消
img