#include<iostream>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
// Need to link with Ws2_32.lib
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
//1.加载库 --WSAStartup;
WORD wVersionRequested;
WSADATA wsaData;
int err;
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %d\n", err);
return 1;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
return 1;
}
else
printf("The Winsock 2.2 dll was found okay\n");
//2.socket();
SOCKET C_client=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(C_client==INVALID_SOCKET)
{
WSACleanup();
return 1;
}
//3.connect();
sockaddr_in adder;
adder.sin_family=AF_INET;
adder.sin_addr.S_un.S_addr=inet_addr("127.1.1.1");//网址
adder.sin_port=htons(27015);
if(connect(C_client,(const sockaddr *)&adder,sizeof(adder))==SOCKET_ERROR)
{
closesocket(C_client);
WSACleanup();
return 1;
}
//4.send();
char bufz[1024]={0};
cin>>bufz;
send(C_client,bufz,sizeof(bufz),0);
//5.recv();
int RecNum=recv(C_client,bufz,sizeof(bufz),0);
if(RecNum>0)
{
cout<<bufz<<endl;
}
//6.closesocket();
closesocket(C_client);
WSACleanup();
}
05-24