用socket写了一个一个客户端和服务端,但是客户端连接不上,报10060错误,求解,服务器端代码如下:
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include
#include <Winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <WS2tcpip.h>
#include
#include <windows.h>
using namespace std;
SOCKADDR_IN ClientAddr = { 0 };
int ClientAddrLen = sizeof(ClientAddr);
SOCKET ClientSocket[1001];
//DWORD WINAPI InstantChat(LPVOID n);
void InstantChat(int n)
{
char Buff[1024] = { 0 };
int ret = 0;
while (1)
{
ret = recv(ClientSocket[n], Buff, sizeof(Buff), NULL);
if (ret > 0)
{
cout << inet_ntoa(ClientAddr.sin_addr) << "发来的消息:" << Buff;
Buff[ret] = 0;
}
}
}
int main(void)
{
SOCKET ServerSocketFd = 0;
int ret = 0;
sockaddr_in ServerAddr = { 0 };
WSADATA WsaData;
WSAStartup(MAKEWORD(1, 1), &WsaData);
if ((LOBYTE(WsaData.wVersion) != 1) || (HIBYTE(WsaData.wVersion) != 1))
{
cout << "Socket版本确认失败" << endl;
return -1;
}
cout << "Socket版本确认成功" << endl;
ServerSocketFd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == ServerSocketFd)
{
cout << "创建SOCKET套接字失败" << endl;
WSACleanup();
return -1;
}
cout << "socket套接字创建成功" << endl;
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
ServerAddr.sin_port = htons(6000);
ret = bind(ServerSocketFd, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr));
if (0 != ret)
{
cout << "bind绑定失败" << GetLastError() << endl;
closesocket(ServerSocketFd);
WSACleanup();
return -1;
}
cout << "bind绑定成功" << endl;
ret = listen(ServerSocketFd, 10);
if (0 != ret)
{
cout << "listen监听失败" << GetLastError() << endl;
closesocket(ServerSocketFd);
WSACleanup();
return -1;
}
cout << "listen监听成功" << endl;
for (int i = 0; i < 1001; i++)
{
ClientSocket[i] = accept(ServerSocketFd, (SOCKADDR *)&ClientAddr, &ClientAddrLen);
if (-1 == ClientSocket[i])
{
cout << "客户端连接失败" << GetLastError() << endl;
closesocket(ServerSocketFd);
WSACleanup();
return -1;
}
cout << "客户端连接成功" << endl;
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)InstantChat, (LPVOID)i, NULL, NULL);
}
closesocket(ServerSocketFd);
WSACleanup();
while (1);
system("pause");
return 0;
}
客户端的代码如下:
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include
#include <Winsock2.h>
#include <WS2tcpip.h>
#pragma comment (lib, "ws2_32.lib")
#include
#include <windows.h>
using namespace std;
int main(void)
{
SOCKET ClientSocketFd = 0;
SOCKADDR_IN ClientAddr = { 0 };
int ret;
char Buff[1024] = { 0 };
WSADATA WsaData;
WSAStartup(MAKEWORD(1, 1), &WsaData);
if ((LOBYTE(WsaData.wVersion) != 1) || (HIBYTE(WsaData.wVersion) != 1))
{
cout << "确认版本失败" << endl;
return -1;
}
cout << "确认版本成功" << endl;
ClientSocketFd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == ClientSocketFd)
{
cout << "socket创建失败" << GetLastError() << endl;
WSACleanup();
return -1;
}
cout << "socket创建成功" << endl;
ClientAddr.sin_family = AF_INET;
ClientAddr.sin_addr.S_un.S_addr = inet_addr("172.0.0.1");
ClientAddr.sin_port = htons(6000);
ret = connect(ClientSocketFd, (SOCKADDR *)&ClientAddr, sizeof(ClientAddr));
if (-1 == ret)
{
cout << "客户端连接失败" << GetLastError() << endl;
closesocket(ClientSocketFd);
WSACleanup();
return -1;
}
cout << "客户端连接成功" << endl;
while (1)
{
cin >> Buff;
send(ClientSocketFd, Buff, sizeof(Buff), NULL);
}
closesocket(ClientSocketFd);
WSACleanup();
while (1);
system("pause");
return 0;
}
运行结果:
求高手指点一下