遇到socket error 10014 错误,请求技术支持 ,谢谢!

kaikai158 kaikai158 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
刚开始学习《windows网络编程》第二版中的第一个例子(tcpserver 这个),运行出错,
屏幕显示如下:

We are awaiting a connection on port 5150.
accept failed with error 10014
Press any key to continue

请指教。

/////////////////////////////////////source code below

// Module Name: tcpserver.cpp
//
// Description:
//
// This sample illustrates how to develop a simple TCP server application
// that listens for a TCP connection on port 5150 and receives data. This
// sample is implemented as a console-style application and simply prints
// status messages when a connection is accepted and when data is received
// by the server.
//
// Compile:
//
// cl -o tcpserver tcpserver.cpp ws2_32.lib
//
// Command Line Options:
//
// tcpserver.exe
//
// NOTE: There are no command parameters.
//


#include <winsock2.h>
#include <stdio.h>

void main(void)
{
WSADATA wsaData;
SOCKET ListeningSocket;
SOCKET NewConnection;
SOCKADDR_IN ServerAddr;
SOCKADDR_IN ClientAddr;
int ClientAddrLen;
int Port = 5150;
int Ret;
char DataBuffer[1024];

// Initialize Winsock version 2.2

if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function.

printf("WSAStartup failed with error %d\n", Ret);
return;
}

// Create a new socket to listening for client connections.

if ((ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))
== INVALID_SOCKET)
{
printf("socket failed with error %d\n", WSAGetLastError());
WSACleanup();
return;
}

// Setup a SOCKADDR_IN structure that will tell bind that we
// want to listen for connections on all interfaces using port
// 5150. Notice how we convert the Port variable from host byte
// order to network byte order.

ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);

// Associate the address information with the socket using bind.

if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr))
== SOCKET_ERROR)
{
printf("bind failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
}

// Listen for client connections. We used a backlog of 5 which is
// normal for many applications.

if (listen(ListeningSocket, 5) == SOCKET_ERROR)
{
printf("listen failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
}

printf("We are awaiting a connection on port %d.\n", Port);

// Accept a new connection when one arrives.

if ((NewConnection = accept(ListeningSocket, (SOCKADDR *) &ClientAddr,
&ClientAddrLen)) == INVALID_SOCKET)
{
printf("accept failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
}


printf("We successfully got a connection from %s:%d.\n",
inet_ntoa(ClientAddr.sin_addr), ntohs(ClientAddr.sin_port));

// At this point you can do two things with these sockets. Await
// for more connections by calling accept again on ListeningSocket
// and start sending or receiving data on NewConnection. For
// simplicity We will stop listening for more connections by closing
// ListeningSocket. We will start sending and receiving data on
// NewConnection.

closesocket(ListeningSocket);

// Start sending and receiving data on NewConnection. For simplicity,
// we will just receive some data and report how many bytes were
// received.

printf("We are waiting to receive data...\n");

if ((Ret = recv(NewConnection, DataBuffer, sizeof(DataBuffer), 0))
== SOCKET_ERROR)
{
printf("recv failed with error %d\n", WSAGetLastError());
closesocket(NewConnection);
WSACleanup();
return;
}

printf("We successfully received %d byte(s).\n", Ret);

// For this application we do not plan to do anything else with the
// connection so we will just close the connection.

printf("We are now going to close the client connection.\n");

closesocket(NewConnection);

// When your application is finished handling the connections
// call WSACleanup.

WSACleanup();
}
...全文
评论 6
收藏 已收藏
分享
返回顶部
kaikai158
网络编程 18,356 成员 64,161 内容