在vs2015编译socket客户端程序时候返回-1 5
#include<winsock2.h>#include<stdio.h>#include<WS2tcpip.h>#pragmacomment(lib,"WS2_32.l...
#include<winsock2.h>
#include<stdio.h>
#include<WS2tcpip.h>
#pragma comment(lib,"WS2_32.lib")
int main()
{
WSADATA data;
WORD w = MAKEWORD(2, 2);
WSAStartup(w, &data);
SOCKET s;
char text[10] = { 0 };
s = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = (4999);
addr.sin_addr.S_un.S_addr = inet_pton(AF_INET, "127.0.0.1", &addr);
printf("客户端已启动!\r\n");
if (connect(s, (sockaddr*)&addr, sizeof(addr))<0)
{
printf("connect failed!:%d\r", WSAGetLastError);
return(-1);
}
recv(s, text, sizeof(text), 0);
printf("%s\r\n", text);
closesocket(s);
WSACleanup();
return(0);
}
就是老是返回-1秒退啊~ 展开
#include<stdio.h>
#include<WS2tcpip.h>
#pragma comment(lib,"WS2_32.lib")
int main()
{
WSADATA data;
WORD w = MAKEWORD(2, 2);
WSAStartup(w, &data);
SOCKET s;
char text[10] = { 0 };
s = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = (4999);
addr.sin_addr.S_un.S_addr = inet_pton(AF_INET, "127.0.0.1", &addr);
printf("客户端已启动!\r\n");
if (connect(s, (sockaddr*)&addr, sizeof(addr))<0)
{
printf("connect failed!:%d\r", WSAGetLastError);
return(-1);
}
recv(s, text, sizeof(text), 0);
printf("%s\r\n", text);
closesocket(s);
WSACleanup();
return(0);
}
就是老是返回-1秒退啊~ 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询