socket tcp clinet最简单测试程序

// testsocketclient.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <winsock2.h>
#include <stdio.h>

#pragma comment(lib, "Ws2_32.lib")
void main()
{
	//加载套接字库,版本协商
	WORD wVersionRequired;
	WSADATA wsaData;
	int err;

	//请求库
	//WSAStartup( WORD wVersionRequired, WSADATA lpWSAData );
	wVersionRequired = MAKEWORD(1, 1);
	err = WSAStartup(wVersionRequired, &wsaData);

	if (err != 0)
	{
		//不等于0就退出
		return;
	}

	//高字节和低字节都不为1
	if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
	{
		WSACleanup();
		return;
	}
	SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
	//设定服务器的地址信息
	SOCKADDR_IN addrSrv;
	addrSrv.sin_addr.S_un.S_addr = inet_addr("10.10.22.105");
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_port = htons(9099);
	connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
    label:
	send(sockClient, "this is qiang ge", strlen("this is qiang ge") + 1, 0);
	while (1)
	{
	char recvBuf[100];
	recv(sockClient, recvBuf, 100, 0);
	printf("%s\n", recvBuf);
	int igoto = 0;
	scanf_s("%d", &igoto);
	if (igoto == 111)
	goto label;
	}
	closesocket(sockClient);
	WSACleanup();
	system("PAUSE");
}

  

上一篇:c++的#和##的用法


下一篇:Appium 从 0 到 1 搭建移动 App 功能自动化测试平台 (1):模拟器中运行 iOS 应用