#include <WinSock2.h>
#pragma comment(lib,"wsock32.lib") //引入库文件
using namespace std;
WSADATA wsaData;
int retVal;//返回码
int main()
{
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建一个套接字
SOCKADDR_IN servAddr;
servAddr.sin_family = AF_INET;//TCP UDP协议;
servAddr.sin_port = htons((short)4999);//服务器端口
servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");//设置服务器地址
retVal = connect(sHost, (LPSOCKADDR)&servAddr, sizeof(servAddr));//连接服务器
char buf[128];//数据缓冲区
int BUF_SIZE = 128;//接收字节数
ZeroMemory(buf, BUF_SIZE);
strcpy_s(buf, "MyTcp");
retVal = send(sHost, buf, strlen(buf), 0);
closesocket(sHost);
WSACleanup();
return 0;
}
时间: 2024-10-12 13:00:07