连接被拒绝,错误10061,是为什么?
#include <winsock.h>#pragma comment(lib,"ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <stdarg.h>
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 1, 1 );
int err;
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we couldn't find a useable */
/* winsock.dll. */
return(0);
}
int c,d,e,g,i,n;
SOCKET a,b;
char s[]="00000000";
struct sockaddr_in my_addr;
n=strlen(s);
struct sockaddr_in remote_addr;
a=socket(AF_INET,SOCK_STREAM,0);//调用Socket函数//
if(a==-1)
{
printf("创建SOCKET失败",NULL,MB_OK);
}
memset(&my_addr,0,sizeof(my_addr));
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(0);
my_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if(bind(a,( struct sockaddr*)&my_addr,sizeof(my_addr))==-1)
{
printf("错误码%d",GetLastError());
}
c=listen(a,10);//调用listen()函数监听端口//
if(c==-1)
{
printf("错误码%d",GetLastError());//出错提示//
}
else
{
printf("服务器已启动!\n");
}
d=sizeof(remote_addr);
while(true)
{
printf("等待连接...");
b=accept(a,(struct sockaddr*)&remote_addr,&d);//accept()函数,用于接受客户端的连接请求//
if(b!=-1)
{
printf("已连接上!");
send(b,s,sizeof(s),0);
}
else
{
printf("未成功连接! %d\n",GetLastError());//没连接上,提示错误码//
}
closesocket(a);//关闭socket//
closesocket(b);//关闭socket1//
WSACleanup();//释放资源//
}
}