创建socket时的协议参数的疑问~~~

wuzhiwenk3001 2012-05-02 11:41:05
SOCKET socket( int af, int type, int protocol );中, protocol参数是这个socket所用的协议.
我所理解这个参数的意思是: 你的数据是基于这个协议的( 比如:TCP,那么我发送的数据的TCP头不用你构造,接收到的数据也不包含TCP头,内核已经帮你去掉了).

但最近看到的程序与我的理解不同.比如这个从网上找到的ping程序:
sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
它用原始套接字,发送数据时不用自已写IP头,但要自已填ICMP头,当收到数据时,能收到IP头!当然也能收到ICMP头!

晕死,搞到我都弄不同白这个 protocol参数是搞什么的,MSDN也只有这句 "Protocol to be used with the socket that is specific to the indicated address family. "

请大牛指点~~~这个protocol的真正用意是什么~
...全文
115 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2012-05-03
  • 打赏
  • 举报
回复

The protocol specifies a particular protocol to be used with the socket. Normally only a single protocol exists to support a particular socket type
within a given protocol family, in which case protocol can be specified as 0. However, it is possible that many protocols may exist, in which case a
particular protocol must be specified in this manner. The protocol number to use is specific to the “communication domain” in which communication is to
take place; see protocols(5). See getprotoent(3) on how to map protocol name strings to protocol numbers.


AME
protocols - protocols definition file

DESCRIPTION
This file is a plain ASCII file, describing the various DARPA internet protocols that are available from the TCP/IP subsystem. It should be consulted
instead of using the numbers in the ARPA include files, or, even worse, just guessing them. These numbers will occur in the protocol field of any IP
header.

Keep this file untouched since changes would result in incorrect IP packages. Protocol numbers and names are specified by the IANA (Internet Assigned
Numbers Authority).

Each line is of the following format:

protocol number aliases ...

where the fields are delimited by spaces or tabs. Empty lines are ignored. If a line contains a hash mark (#), the hash mark and the part of the line
following it are ignored.

The field descriptions are:

protocol
the native name for the protocol. For example ip, tcp, or udp.

number the official number for this protocol as it will appear within the IP header.

aliases
optional aliases for the protocol.
FILES
/etc/protocols
The protocols definition file.


# /etc/protocols:
# $Id: protocols,v 1.9 2009/09/29 15:11:55 ovasik Exp $
#
# Internet (IP) protocols
#
# from: @(#)protocols 5.1 (Berkeley) 4/17/89
#
# Updated for NetBSD based on RFC 1340, Assigned Numbers (July 1992).
# Last IANA update included dated 2009-06-18
#
# See also http://www.iana.org/assignments/protocol-numbers

ip 0 IP # internet protocol, pseudo protocol number
hopopt 0 HOPOPT # hop-by-hop options for ipv6
icmp 1 ICMP # internet control message protocol
igmp 2 IGMP # internet group management protocol
ggp 3 GGP # gateway-gateway protocol
ipencap 4 IP-ENCAP # IP encapsulated in IP (officially ``IP'')
st 5 ST # ST datagram mode
tcp 6 TCP # transmission control protocol
cbt 7 CBT # CBT, Tony Ballardie <A.Ballardie@cs.ucl.ac.uk>
egp 8 EGP # exterior gateway protocol
igp 9 IGP # any private interior gateway (Cisco: for IGRP)
bbn-rcc 10 BBN-RCC-MON # BBN RCC Monitoring
nvp 11 NVP-II # Network Voice Protocol
pup 12 PUP # PARC universal packet protocol
argus 13 ARGUS # ARGUS
emcon 14 EMCON # EMCON
xnet 15 XNET # Cross Net Debugger
chaos 16 CHAOS # Chaos
udp 17 UDP # user datagram protocol


luciferisnotsatan 2012-05-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

可以理解为语种(比如中文,英文,德文,法文)么?
报文协议相当于A语言。
流协议相当于B语言。
底层协议相当于C语言。
不同的语言用于不同的沟通。
socket的也是。
-
协议只是一个规定,遵循就好。
[/Quote]
type看成语言,中文,英文。
protocol 看成文章类型,记叙文,诗歌。

想要具体理解,还是找书看下吧。
TCP/IP 详解 卷一
unix网络编程
cobra_chen 2012-05-03
  • 打赏
  • 举报
回复
可以理解为语种(比如中文,英文,德文,法文)么?
报文协议相当于A语言。
流协议相当于B语言。
底层协议相当于C语言。
不同的语言用于不同的沟通。
socket的也是。
-
协议只是一个规定,遵循就好。
wuzhiwenk3001 2012-05-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

C/C++ code

The protocol specifies a particular protocol to be used with the socket. Normally only a single protocol exists to support a particular socket type
within a given p……
[/Quote]

英语刚过4级,很吃力
wuzhiwenk3001 2012-05-03
  • 打赏
  • 举报
回复
我也看过TCP/IP 详解 ,我是懂协议的,我就是问单单在这具socket函数里那个协议参数的意义.
矫情狗_____ 2012-05-02
  • 打赏
  • 举报
回复
套接字指定协议...(即TCP/UDP/等)...0为自动选定...

64,691

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧