本文介绍了Winsock套接字选择功能失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一些我试图理解并开始工作的代码.我正在学习套接字.我遇到问题的方法是一种称为选择"方法的方法.它失败并返回错误:WSAENOTSOCK.谁能告诉我是什么原因造成的.我真的在竭尽全力来解决这个问题.

变量m_listeningSocket已定义为int,它是套接字的返回值:

I have inherited some code that I am trying to understand and get working. I''m learning about sockets as I go. The method I am having problems with is one that calles the ''select'' method. It fail and returns the error: WSAENOTSOCK. Could anyone possibly tell me what might be causing this. I have really struggled to anything that helps me with this.

The variable m_listeningSocket has been defined as an int and is the return value from socket:

int m_listeningSocket = socket(AF_INET, SOCK_STREAM, 0);


尽管我不明白为什么将此返回值设置为int,因为我认为它应该是SOCKET类型-否则会引起问题.



although I don''t understand why this return value has been set to an int as I thought it should be a SOCKET type - is this going to cause problems.


int Select( int timeout)
{
  struct timeval timout;
  int    ret, maxfdp1;

  if( timeout <= 0 )
	  return -1;
  /* setup sockets to read */
  maxfdp1 = m_listeningSocket + 1;

  FD_ZERO( &m_rset );
  FD_SET ( m_listeningSocket, &m_rset );
  FD_SET ( 0, &m_rset );		// this adds stdin to the set
  FD_ZERO( &m_wset );
  FD_ZERO( &m_eset );

  timout.tv_sec  = timeout / 1000;
  timout.tv_usec = timeout % 1000;

  ret = ::select( maxfdp1, &m_rset, &m_wset, &m_eset, &timout );

  return ret;
}




"getsockopt"返回零,表示没有问题,在调用Select方法之前设置套接字的其余代码为:




''getsockopt'' returns zero, indicating that there is no problem, and the rest of the code that sets the socket up before I call the Select method is:

int CCCD_Socket::CreateListenSock()
{
	int retVal = -1;

	WSADATA wsaData;
	WORD wVersionRequested = MAKEWORD(1, 1);
	int err = WSAStartup(wVersionRequested, &wsaData);

	if(err != 0)
	{
		printf("WSAStartup error %ld", WSAGetLastError());
		WSACleanup();
		return retVal;
	}

	struct sockaddr_in channel;
	memset(&channel, 0, sizeof(channel)); //zero channel
	channel.sin_family = AF_INET;
	channel.sin_addr.s_addr = htonl(INADDR_ANY);//can use any interface for the defined port - should be ok if only one device wants to connect to the port
	channel.sin_port = htons(SERVER_PORT);

	m_listeningSocket = socket(AF_INET, SOCK_STREAM, 0);//0 = prototcol i snot defined, i think IPPROTO_TCP is default
	if(m_listeningSocket == -1)
	{
		printf("socket error %ld", WSAGetLastError());
		WSACleanup();
		return retVal;
	}

	//not sure if this is necessary
	int on = 1;
	setsockopt(m_listeningSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));

	int b = bind(m_listeningSocket, (struct sockaddr*)&channel, sizeof(channel));
	if(b < 0)
	{
		printf("bind error %ld", WSAGetLastError());
		WSACleanup();
		return retVal;
	}

	int l = listen(m_listeningSocket, 10);
	if(l < 0)
	{
		printf("listen error %ld", WSAGetLastError());
		WSACleanup();
		return retVal;
	}
	else
	{
		retVal = m_listeningSocket;
	}
	return retVal;
}

推荐答案




这篇关于Winsock套接字选择功能失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 02:18
查看更多