// phpsocket.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#pragma comment(lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
//初始化套接字版本
WSAData wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
//创建Socket
SOCKET Socket_Server;
Socket_Server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (Socket_Server == INVALID_SOCKET)
{
printf("Socket error");
return 0;
}
//初始化地址
sockaddr_in ClientAddr;
ClientAddr.sin_family = AF_INET;
ClientAddr.sin_port = htons(8080);
ClientAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
//循环连接
while (true)
{
//连接
if (connect(Socket_Server,(LPSOCKADDR)&ClientAddr,sizeof(ClientAddr)) == SOCKET_ERROR)
{
printf("connect error\n");
Sleep(3000);
continue;
}
printf("连接成功");
break;
}
//发送
if (send(Socket_Server,"online",sizeof("online"),0) == SOCKET_ERROR)
{
printf("send error");
return 0;
}
char revData[1024] = {0};
int ret = recv(Socket_Server,revData,1024,0);
if (ret > 0)
{
MessageBoxA(0,revData,0,0);
}
//关闭Socket
closesocket(Socket_Server);
WSACleanup();
return 0;
}