启动就显示连接错误。不知道怎么回事?
#include <stdio.h>
#include <WinSock2.h>
#include <iostream>
#include <string>
#define debug_my 1
using namespace std;
#pragma comment(lib,"ws2_32.lib")
int main()
{
WORD sockVersion = MAKEWORD(2,2);
WSADATA data;
if (WSAStartup(sockVersion,&data)!= 0)
{
return 0;
}
while(true)
{
SOCKET sclient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (sclient == INVALID_SOCKET)
{
printf("invalid scoket!");
return 0;
}
sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(77777);
serAddr.sin_addr.S_un.S_addr = inet_addr("192.168.6.19");
if (connect(sclient,(sockaddr *)&serAddr,sizeof(serAddr))==SOCKET_ERROR)
{
printf("connect error!!");
closesocket(sclient);
#if debug_my
getchar();
#endif
return 0;
}
string data;
cin >> data;
const char* senddata;
senddata = data.c_str();
send(sclient,senddata,sizeof(senddata),0);
char recvData[255];
int ret = recv(sclient,recvData,255,0);
if (ret>0)
{
recvData[ret] =0X00;
printf(recvData);
}
closesocket(sclient);
}
WSACleanup();
return 0;
}