为什么我不能绑定网卡

underskyli 2005-11-02 05:15:23
下面是我写的部分程序,为什么到最后一步时弹出的对话框总是绑定错误,实在搞不懂
请各位大侠指点一二,谢谢拉!!!
WSAData wsaData;
BOOL flag=true;
SOCKET sock;
SOCKADDR_IN addr_in;
in_addr addr;
AnsiString strAdd;
char LocalName[16];
struct hostent *pHost;
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
ShowMessage("错误的Winsock版本");
return;
}
if ((sock = socket(AF_INET,SOCK_RAW,IPPROTO_IP)) == INVALID_SOCKET)
ShowMessage("初始化失败!");
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag)) == SOCKET_ERROR)
ShowMessage("设置IP头错");
if (gethostname((char*)LocalName, sizeof(LocalName)-1) == SOCKET_ERROR)
ShowMessage("获取主机名错");
if ((pHost=gethostbyname((char*)LocalName)) == NULL)
ShowMessage("获取地址错");
for(int i = 0; pHost->h_addr_list[i] != 0; ++i) //注意这里的循环
{
memcpy(&addr, pHost->h_addr_list[i], sizeof(in_addr));
strAdd = strAdd + " " + StrPas(inet_ntoa(addr));
}
ShowMessage(strAdd);
addr_in.sin_addr=*(in_addr *)pHost->h_addr_list[0];//IP
addr_in.sin_family = AF_INET;
addr_in.sin_port = htons(57274);
if (bind(sock, (PSOCKADDR)&addr_in, sizeof(addr_in)) == SOCKET_ERROR)
ShowMessage("绑定错误");
else
ShowMessage("绑定");
...全文
337 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
underskyli 2005-12-02
  • 打赏
  • 举报
回复
谢谢楼上的详细解释,我已经搞定了,这么辛苦,接分吧!
shadowstar 2005-11-11
  • 打赏
  • 举报
回复
真的是非常的抱歉,这段代码来自我的一篇文章《Sniffer 实现之一 —— 用 Raw Socket 实现 Sniffer》。

为免误人子弟,又仔细的比较了楼主,各网站转载,http://shadowstar.126.com/,以及本人现有的代码,又对各代码分别进行了测试。

在WinXP下打了SP2后确实有些问题,去年还有人问过,不过只改了一个地方,就是把IPPROTO_RAW改为IPPROTO_IP,但只是会出现假死现象,绑定应该是可以的。一时没记起XP下究竟是什么问题,乱讲了一通,十分抱歉!


最后证明:

代码是正确的,文章也是正确的,而且早已更新过了,终于长吁一口气~~~~~~~

文中代码实现部分:“五、实现 Sniffer”,开头有一句话,可能楼主没注意到:

(需要在工程文件里加入WS2_32.LIB这个文件)

我也不明白为什么不加这个LIB也可以编译


为了突出这句话,文章再次更新:

http://shadowstar.q99.cn/essay/security/sniffer1.htm
痞子酷 2005-11-10
  • 打赏
  • 举报
回复
The Windows Sockets setsockopt function sets a socket option.

int setsockopt (

SOCKET s,
int level,
int optname,
const char FAR * optval,
int optlen
);


Parameters

s

[in] A descriptor identifying a socket.

level

[in] The level at which the option is defined; the supported levels include SOL_SOCKET and IPPROTO_TCP. (See annex for more protocol-specific levels.)

optname

[in] The socket option for which the value is to be set.

optval

[in] A pointer to the buffer in which the value for the requested option is supplied.

optlen

[in] The size of the optval buffer.



Remarks

setsockopt sets the current value for a socket option associated with a socket of any type, in any state. Although options can exist at multiple protocol levels, they are always present at the uppermost "socket'' level. Options affect socket operations, such as whether expedited data is received in the normal data stream, whether broadcast messages can be sent on the socket.
There are two types of socket options: Boolean options that enable or disable a feature or behavior, and options which require an integer value or structure. To enable a Boolean option, optval points to a nonzero integer. To disable the option
optval points to an integer equal to zero. optlen should be equal to sizeof(int) for Boolean options. For other options, optval
points to the an integer or structure that contains the desired value for the option, and optlen is the length of the integer or structure.

The following options are supported for setsockopt. The Type identifies the type of data addressed by optval.
level = SOL_SOCKET

Value Type Meaning
SO_BROADCAST BOOL Allow transmission of broadcast messages on the socket.
SO_DEBUG BOOL Record debugging information.
SO_DONTLINGER BOOL Do not block close waiting for unsent data to be sent. Setting this option is equivalent to setting SO_LINGER with l_onoff set to zero.
SO_DONTROUTE BOOL Do not route: send directly to interface.
SO_GROUP_PRIORITY int Specify the relative priority to be established for sockets that are part of a socket group.
SO_KEEPALIVE BOOL Send keepalives
SO_LINGER struct linger Linger on close if unsent data is present
SO_OOBINLINE BOOL Receive out-of-band data in the normal data stream. (See section Out-Of-Band data for a discussion of this topic.)
SO_RCVBUF int Specify buffer size for receives
SO_REUSEADDR BOOL Allow the socket to be bound to an address which is already in use. (See bind.)
SO_SNDBUF int Specify buffer size for sends.
PVD_CONFIG Service Provider Dependent This object stores the configuration information for the service provider associated with socket s. The exact format of this data structure is service provider specific.


level = IPPROTO_TCP1

TCP_NODELAY BOOL Disables the Nagle algorithm for send coalescing.
1 included for backward compatibility with Windows Sockets 1.1


BSD options not supported for setsockopt are:

Value Type Meaning
SO_ACCEPTCONN BOOL Socket is listening
SO_RCVLOWAT int Receive low water mark
SO_RCVTIMEO int Receive time-out
SO_SNDLOWAT int Send low water mark
SO_SNDTIMEO int Send time-out
SO_TYPE int Type of the socket


SO_DEBUG

Windows Sockets service providers are encouraged (but not required) to supply output debug information if the SO_DEBUG option is set by an application. The mechanism for generating the debug information and the form it takes are beyond the scope of this specification.

SO_GROUP_PRIORITY

Group priority indicates the relative priority of the specified socket relative to other sockets within the socket group. Values are non-negative integers, with zero corresponding to the highest priority. Priority values represent a hint to the underlying service provider about how potentially scarce resources should be allocated. For example, whenever two or more sockets are both ready to transmit data, the highest priority socket (lowest value for SO_GROUP_PRIORITY) should be serviced first, with the remainder serviced in turn according to their relative priorities.

The WSAENOPROTOOPT error is indicated for nongroup sockets or for service providers which do not support group sockets.

SO_KEEPALIVE

An application can request that a TCP/IP provider enable the use of "keep-alive" packets on TCP connections by turning on the SO_KEEPALIVE socket option. A Windows Sockets provider need not support the use of keep-alives: if it does, the precise semantics are implementation-specific but should conform to section 4.2.3.6 of RFC 1122: Requirements for Internet Hosts ?Communication Layers. If a connection is dropped as the result of "keep-alives" the error code WSAENETRESET is returned to any calls in progress on the socket, and any subsequent calls will fail with WSAENOTCONN.

SO_LINGER

SO_LINGER controls the action taken when unsent data is queued on a socket and a closesocket is performed. See closesocket for a description of the way in which the SO_LINGER settings affect the semantics of closesocket. The application sets the desired behavior by creating a struct linger (pointed to by the optval argument) with the following elements:

struct linger {
u_short l_onoff;
u_short l_linger;
}


To enable SO_LINGER, the application should set l_onoff to a nonzero value, set l_linger to zero or the desired time-out (in seconds), and call setsockopt. To enable SO_DONTLINGER (that is, disable SO_LINGER) l_onoff should be set to zero and setsockopt should be called. Note that enabling SO_LINGER with a nonzero time-out on a nonblocking socket is not recommended.
Enabling SO_LINGER also disables SO_DONTLINGER, and vice versa. Note that if SO_DONTLINGER is DISABLED (that is, SO_LINGER is ENABLED) then no time-out value is specified. In this case, the time-out used is implementation dependent. If a previous time-out has been established for a socket (by enabling SO_LINGER), then this time-out value should be reinstated by the service provider.

SO_REUSEADDR

By default, a socket cannot be bound (see bind) to a local address which is already in use. On occasion, however, it may be necessary to "re-use" an address in this way. Since every connection is uniquely identified by the combination of local and remote addresses, there is no problem with having two sockets bound to the same local address as long as the remote addresses are different. To inform the Windows Sockets provider that a bind on a socket should not be disallowed because the desired address is already in use by another socket, the application should set the SO_REUSEADDR socket option for the socket before issuing the bind. Note that the option is interpreted only at the time of the bind: it is therefore unnecessary (but harmless) to set the option on a socket which is not to be bound to an existing address, and setting or resetting the option after the bind has no effect on this or any other socket.

SO_RCVBUF
SO_SNDBUF

When a Windows Sockets implementation supports the SO_RCVBUF and SO_SNDBUF options, an application can request different buffer sizes (larger or smaller). The call to setsockopt can succeed, although the implementation did not provide the whole amount requested. An application must call getsockopt with the same option to check the buffer size actually provided.

PVD_CONFIG

This object stores the configuration information for the service provider associa
痞子酷 2005-11-10
  • 打赏
  • 举报
回复
你的代码,我在CB XP编译不通过。

呵呵~~~接分
underskyli 2005-11-03
  • 打赏
  • 举报
回复
不可能,我用DELPHI都可以做到,为什么C++builder做不到!
shadowstar 2005-11-02
  • 打赏
  • 举报
回复
XP不支持rawsocket,换个操作系统玩吧。
underskyli 2005-11-02
  • 打赏
  • 举报
回复
是的,Windows XP操作系统!!有没有什么解决方法吗??
shadowstar 2005-11-02
  • 打赏
  • 举报
回复
楼主用的是不是Windows XP操作系统?
shadowstar 2005-11-02
  • 打赏
  • 举报
回复
咦,代码咋这么熟呢^-^

1,316

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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