简体   繁体   English

套接字发送recv函数

[英]Socket send recv functions

I have created a socket using the following lines of code. 我使用以下代码行创建了一个套接字。 Now i change the value of the socket i get like this 现在我改变套接字的值,我得到这样的

m_Socket++; m_Socket ++;

Even now the send recv socket functions succeeds without throwing SOCKET_ERROR. 即使现在,send recv套接字函数也会成功,而不会抛出SOCKET_ERROR。 I expect that it must throw error. 我希望它必须抛出错误。

Am i doing something wrong. 难道我做错了什么。

struct sockaddr_in ServerSock; struct sockaddr_in ServerSock; // Socket address structure to bind the Port Number to listen to //用于绑定要侦听的端口号的套接字地址结构

char *localIP ;

SOCKET SocServer;

//To Set up the sockaddr structure
ServerSock.sin_family = AF_INET;
ServerSock.sin_addr.s_addr = INADDR_ANY;

ServerSock.sin_port = htons(pLantronics->m_wRIPortNo);

// To Create a socket for listening on wPortNumber
if(( SocServer = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET )
{
    return FALSE;
}

//To bind the socket with wPortNumber
if(bind(SocServer,(sockaddr*)&ServerSock,sizeof(ServerSock))!=0)
{
    return FALSE;
}

// To Listen for the connection on wPortNumber
if(listen(SocServer,SOMAXCONN)!=0)
{
    return FALSE;
}

// Structure to get the IP Address of the connecting Entity
sockaddr_in insock;

int insocklen=sizeof(insock);

//To accept the Incoming connection on the wPortNumber
pLantronics->m_Socket=accept(SocServer,(struct sockaddr*)&insock,&insocklen);   

if(pLantronics->m_Socket == INVALID_SOCKET)
{
    shutdown(SocServer, 2 );
    closesocket(SocServer );
    return FALSE;
}

// To make socket non-blocking
DWORD dwNonBlocking = 1;
if(ioctlsocket( pLantronics->m_Socket, FIONBIO, &dwNonBlocking ))
{
    shutdown(pLantronics->m_Socket, 2);
    closesocket(pLantronics->m_Socket);
    return FALSE;
}


pLantronics->m_sModemName = inet_ntoa(insock.sin_addr);

Now i do 现在我做

m_Socket++;//change to some other number ideally expecting send recv to fail.

Even now the send recv socket functions succeeds without throwing SOCKET_ERROR. 即使现在,send recv套接字函数也会成功,而不会抛出SOCKET_ERROR。 I expect that it must throw error. 我希望它必须抛出错误。

Am i doing something wrong. 难道我做错了什么。

It's because of the peculiar nature of Windows handles -- when created they are divisible by four and when used their two lowest bits are ignored. 这是因为Windows句柄的特殊性质 - 创建时它们可被4整除,使用时它们的两个最低位被忽略。 Incrementing a handle by one will make m_Socket refer to the same socket as before (only when you increment by four will the function return an error -- unless there is another handle with that value open). 将句柄递增1将使m_Socket引用与之前相同的套接字(仅当您增加4时,函数才会返回错误 - 除非有另一个句柄打开该值)。

You should not probe for open handles in this manner. 您不应该以这种方式探测打开的句柄。 While there are other ways to enumerate open handles, you shouldn't use them. 虽然还有其他方法可以枚举打开的句柄,但您不应该使用它们。 Do not depend on the system to keep track of your handles -- track them yourself. 不要依赖系统来跟踪您的手柄 - 自己跟踪它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM