我是网络编程的新手,但过去三年一直在使用C ++从事业余爱好,最近也在使用AS3。
我正在尝试编写一个服务器(对于客户端来说,我正在研究AS3项目),据我所知,这个SOCKET非常好。我不会重新创建它,多线程,没有重新分配或任何东西。没有滑稽的bisuness。只是试图将它全部设置好并且bind()正在吐出那个令人讨厌的10038在我脸上。
我看过MSDN,我非常清楚10038意味着“尝试在无效套接字上进行操作”;对于我的生活,我无法看到它的无效。
但是我的漫无边际,继承了代码:( functions.h是空的,havnt已经走得那么远了)
//Server for Project7 - Client written in AS3 under FlashDevelop. Developed under and for the Windows Operating System Enviroment
//All connections handled under TCP/IP on port 3011
//Client is URL locked to www.cutdev.com
//Copyright Tyler Buchinski 2012 All Rights Reserved
#include <iostream>
#include "functions.h"
#define WIN32_MEAN_AND_LEAN
#include <winsock2.h>
#include <windows.h>
using namespace std;
int main()
{
const int iReqWinsockVer = 2; // Minimum winsock version required
WSADATA wsaData;
if (WSAStartup(MAKEWORD(iReqWinsockVer,0), &wsaData)==0)
{
// Check if major version is at least iReqWinsockVer
if (LOBYTE(wsaData.wVersion) >= iReqWinsockVer)
{
SOCKET SocketListen;
SocketListen = (AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(SocketListen == INVALID_SOCKET)
{
cout << "ERROR - could not creaate listening socket." << endl;
system("pause");
return 4;
}
sockaddr_in Listener, Channel1;
Listener.sin_family = AF_INET;
Listener.sin_port = htons(3011);
Listener.sin_addr.S_un.S_addr = INADDR_ANY;
int err = bind(SocketListen,(sockaddr*)(&Listener),sizeof(Listener));
if (!err == 0)
{
cout << "Listener binding failed!" << endl;
cout << err << endl;
cout << WSAGetLastError();
return 3;
}
}
else
{
// Required version not available
cout <<"Required version of Winsock not installed." << endl;
}
// Cleanup winsock
if (!WSACleanup() == 0)
{
// cleanup failed
cout << "WSACleanup Failed!!" << endl;
system("pause");
}
}
else
{
cout << "WSA Startup failed!" << endl;
}
return 0;
}
提前感谢您的帮助! -Tyler
答案 0 :(得分:2)
错误10038
为WSAENOTSOCK
:
尝试对非套接字的操作进行操作。
如果s参数中的描述符不是套接字,则返回此错误。
这是因为您省略了对socket()
的调用而SocketListen
包含IPPROTO_TCP
常量的值而不是套接字描述符:
SocketListen = (AF_INET,SOCK_STREAM,IPPROTO_TCP);
应该成为:
SocketListen = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);