#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
void TCPClient()
{
//創建Socket
SOCKET sockclient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET == sockclient)
{
return;
}
//連接到服務器
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_port = htons(5678);
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
connect(sockclient,(struct sockaddr* )&addr,sizeof(addr));
//數據的收發
getch();
//關閉socket
closesocket(sockclient);
}
int main()
{
//Socket庫初始化
WSADATA wsa = {0};
WSAStartup(MAKEWORD(2,2),&wsa);
//TCP客戶端
TCPClient();
return 0;
}
參考:http://blog.csdn.net/huangxy10/article/details/8489885
http://blog.csdn.net/weiwangchao_/article/details/6996541
http://binge518.blog.51cto.com/1554719/1222251/