代码并不重要,主要看最后的编译命令:
//test.cpp
#include <winsock2.h>
#include <iostream>
using namespace std;
int main()
{
WSADATA ws;
WSAStartup(MAKEWORD(2, 2), &ws);
SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_family = AF_INET;
addrSrv.sin_addr.s_addr = inet_addr("127.0.0.1");
addrSrv.sin_port = htons(12345);
if (connect(s, (SOCKADDR *)&addrSrv, sizeof(SOCKADDR)) == INVALID_SOCKET)
{
cout << "connect() fail" << WSAGetLastError() << endl;
return -1;
}
}
编译命令:g++ .\test.cpp -LD:\application\mingw64\x86_64-w64-mingw32\lib -lws2_32
注意自行修改mingw64所在路径