C++构建TCP客户端

//TCP
#include 
#include 
#pragma comment(lib,"ws2_32.lib")

//初始化Socket资源
if(WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
     WSACleanup();
     return;
}

//定义一个socket
mSocket1 = socket(AF_INET,SOCK_STREAM,0);

//将ip地址192.168.0.1转换成字符串
inet_pton(AF_INET, "192.168.0.1", &addrSrv1.sin_addr.S_un.S_addr);

//绑定ip
addrSrv1.sin_family = AF_INET;
//绑定端口号
addrSrv1.sin_port = htons(2000);

//连接
::connect(mSocket1, (SOCKADDR*)&addrSrv1, sizeof(SOCKADDR));

//设置socket为无延迟
BOOL bValue = TRUE;
setsockopt(mSocket1, IPPROTO_TCP, TCP_NODELAY, (const char*)&bValue, sizeof(BOOL));

//发送信息
int res1 = send(mSocket1,str_reset.c_str(),str_reset.length(),0);

 

你可能感兴趣的:(C++,TCP)