VC++ close函数包含于那个头文件?
VC++ close函数包含于那个头文件?偶在做网络技术的实验,以前也没用过VC++.程序编译的时候说close函数未定义.
懂的帅哥靓姐给偶个指示,小弟不胜感谢. 哪个 close ?
CSocket::close() ?
调用出错的代码是什么? 出错:
D:\server.cpp(71) : error C2065: 'close' : undeclared identifier
是用来关闭socket的 出错的代码…… #include <afxsock.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
#include <string.h>
#define closesocket close
#define PROTOPORT 5193
#define QLEN 6
int visits=0;
void main(int argc,char *argv[])
{
struct protoent *ptrp;
struct sockaddr_in sad;
struct sockaddr_in cad;
int sd,sd2;
int port;
int alen;
char buf;
memset((char*)&sad,0,sizeof(sad));
sad.sin_family=AF_INET;
sad.sin_addr.s_addr=INADDR_ANY;
if(argc>1){
port=atoi(argv);
}
else{
port=PROTOPORT;
}
if(port>0)sad.sin_port=htons((u_short)port);
else{
fprintf(stderr,"bad port number %s\n",argv);
exit(1);
}
if(((int)(ptrp=getprotobyname("tcp")))==0){
fprintf(stderr,"cannot map \"tcp\" to protocol number");
exit(1);
}
sd=socket(PF_INET,SOCK_STREAM,ptrp->p_proto);
if(sd<0){
fprintf(stderr,"socket creation failed\n");
exit(1);
}
if(bind(sd,(struct sockaddr*)&sad,sizeof(sad))<0){
fprintf(stderr,"bind failed\n");
exit(1);
}
if(listen(sd,QLEN)<0){
fprintf(stderr,"listen failed\n");
exit(1);
}
while(1){
alen=sizeof(cad);
if((sd2=accept(sd,(struct sockaddr*)&cad,&alen))<0){
fprintf(stderr,"accept failed\n");
exit(1);
}
visits++;
sprintf(buf,"This server had been contacted %d time %s\n",
visits,visits==1?".":"s.");
send(sd2,buf,strlen(buf),0);
closesocket(sd2);
}
} 查了下afxsocket.h 只有一个成员函数:Close()
而在winsock2.h 里面有个 closesocket(int SocketID) 调试环境VS.NET 2005 #include <winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
#include <string.h>
#define PROTOPORT 5193
#define QLEN 6
我把头文件改成了上面这样,因为winsock2.h有closesocket(),而没有close()。
运行后编译没有出错,但是.exe一开始运行就结束了。
麻烦大家再帮帮我,谢谢啦! cannot map tcp to protocol number... 什么原因就不知道了,还没学SOCKET,CSocket更没看过
给个建议:出错信息打出来了,总要给自已看到才好吧?
在exit(1)前加个 getchar() 就方便多了 #ifdef WIN32
WSADATA wsaData;
WSAStartup(0x0101,&wsaData);
#endif
对比书上,发现少了这段代码,终于可以运行了。就是不知道这段代码有什么用。
谢谢楼上
页:
[1]