user-0 提问: 期末设计，手上有两段代码，求大牛帮我解下疑惑

回复:
user: user-1
content: 不知道有多少前人掉在TCP Socket send(人多)send(病少)send(财富) recv(人多病)recv(少财富) 陷阱里面啊！ http://bbs.csdn.net/topics/380167545

user: user-0
content: 引用 2 楼 lgbxyz 的回复:sender可以成功向onenet发送数据了  #include <stdio.h> #include "cJSON.h" #include "winsock2.h"  //socket library #pragma comment(lib, "ws2_32.lib") //#pragma comment(lib, "cJSON.lib")  char *CreateCJSON(cJSON **pJsonRoot); void SendToServer(char *databuf);  int main(void) { 	cJSON *pJsonRoot = NULL; 	char *p = NULL;  	p = CreateCJSON(&pJsonRoot); 	if(NULL == p) { 		printf("CreateCJSON failed\n"); 		return -1; 	} 	 	SendToServer(p);  	if(pJsonRoot != NULL) { 		cJSON_Delete(pJsonRoot); 	}  	return 0; }  char *CreateCJSON(cJSON **pJsonRoot) { 	cJSON *pSubJson1 = NULL; 	cJSON *pArray1 = NULL;     cJSON *pSubJson2  = NULL; 	cJSON *pArray2   = NULL;  	int ledstatus = 0; 	char *p = NULL;  	//input led status and check input 	printf("please input ledstatus(0:close,1:open)\n"); 	scanf("%d", &ledstatus); 	if(!(ledstatus == 0 || ledstatus == 1)) { 		printf("ledstatus error\n");         return NULL; 	}  	//create JSON object 	*pJsonRoot = cJSON_CreateObject(); 	if(NULL == *pJsonRoot) { //error happend here 		printf("cJSON_CreateObject failed\n"); 		return NULL; 	} 	cJSON_AddItemToObject(*pJsonRoot, "datastreams", pArray1 = cJSON_CreateArray()); 	cJSON_AddItemToArray(pArray1, pSubJson1 = cJSON_CreateObject());     cJSON_AddStringToObject(pSubJson1,"id", "ledcontrol");     cJSON_AddItemToObject(pSubJson1, "datapoints", pArray2 = cJSON_CreateArray());     cJSON_AddItemToArray(pArray2, pSubJson2 = cJSON_CreateObject()); 	cJSON_AddNumberToObject(pSubJson2,"value", ledstatus);      	//print json content， just for test 	p = cJSON_PrintUnformatted(*pJsonRoot); 	if(NULL == p) { 	/* convert json list to string faild, exit 	 * because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot,  	 * if you also delete pSubJson, it will coredump, and error is : double free 	 */ 		printf("create object faild, exit 2\n"); 		cJSON_Delete(*pJsonRoot); 		return NULL; 	} 	 	printf("=====  JSON(len=%d):\n%s\n", strlen(p), p); 	return p; }  void ParseRespone(char* response) { 	char* start = NULL; 	char* end = NULL; 	cJSON *pJson = NULL; 	cJSON *pSub = NULL;  	start = strstr(response, "\r\n\r\n"); 	if (NULL == start) { 		printf("No Response Body\n"); 		return; 	}  	end = strchr(start, '}'); 	if (NULL == end) { 		printf("Invalid Json data\n"); 		return; 	}  	end++; 	*end = 0;  	pJson = cJSON_Parse(start); 	if (NULL == pJson) { 		printf("cJSON_Parse failed\n"); 		return; 	} 	// get errno from json 	pSub = cJSON_GetObjectItem(pJson, "errno"); 	if (NULL == pSub) { 		printf("cJSON_GetObjectItem failed\n"); 		return; 	}  	if (pSub->valueint != 0) { 		printf("\n>>>>>>UPLOAD DATA ERROR: %d, ", pSub->valueint); 		pSub = cJSON_GetObjectItem(pJson, "error"); 		if (NULL == pSub) { 			printf("error node not found\n"); 		} 		else { 			printf("%s\n\n", pSub->valuestring); 		} 	} 	else { 		printf("\n>>>>>UPLOAD DATA SUCCESS\n\n"); 	}  	cJSON_Delete(pJson);  }  void SendToServer(char *databuf) { 	char slen[32] = { 0 }; 	SOCKET sSocket = INVALID_SOCKET; 	SOCKADDR_IN stSvrAddrIn = { 0 }; /* 服务器端地址 */ 	char sndBuf[2048] = { 0 }; 	char rcvBuf[10240] = { 0 }; 	char *pRcv = rcvBuf; 	int num = 0; 	int nRet = SOCKET_ERROR; 	int sendlen = 0; 	int nNetTimeout = 1000;//1秒 	WSADATA wsaData;  	/* HTTP 消息构造开始，这是程序的关键之处 */ 	memset(sndBuf, 0x00, sizeof(sndBuf)); 	sprintf(sndBuf, "POST /devices/4604864/datapoints HTTP/1.1\r\n" 		"Host: api.heclouds.comr\r\n" 		"api-key: Wscv0BNmRCZqJesh0R8rkNftTWM=\r\n" 		"Content-Type: application/json\r\n" 		"Content-Length: %d\r\n" 		"Connection: keep-alive\r\n\r\n%s", strlen(databuf), databuf);  	// Initialize Winsock 	nRet = WSAStartup(MAKEWORD(2, 2), &wsaData); 	if (nRet != NO_ERROR) { 		printf("WSAStartup failed: %d\n", nRet); 		return -1; 	}   	// Create a SOCKET for connecting to server 	sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 	if (INVALID_SOCKET == sSocket) { 		printf("socket failed with code %d\n", WSAGetLastError()); 		return -1; 	}  	//fast reuse addr 	BOOL bReuseaddr = TRUE; 	setsockopt(sSocket,SOL_SOCKET ,SO_REUSEADDR,(const char*)&bReuseaddr,sizeof(BOOL));   	// The sockaddr_in structure specifies the address family, 	// IP address, and port of the server to be connected to. 	stSvrAddrIn.sin_family = AF_INET; //ipv4 protocol 	stSvrAddrIn.sin_port = htons(80); //80 port 	stSvrAddrIn.sin_addr.s_addr = inet_addr("183.230.40.33");  	// Connect to server. 	nRet = connect(sSocket, (SOCKADDR*)&stSvrAddrIn, sizeof(SOCKADDR)); 	if (SOCKET_ERROR == nRet) { 		printf("connect failed with code %d\n", WSAGetLastError()); 		closesocket(sSocket); 		WSACleanup(); 		return -1; 	}        	/* send http request */ 	printf("=====  Send Data to Server:\n%s\n", sndBuf); 	sendlen = send(sSocket, (char*)sndBuf, strlen(sndBuf), 0); 	if (SOCKET_ERROR == sendlen) { 		printf("send failed with code %d\n", WSAGetLastError()); 		closesocket(sSocket); 		WSACleanup(); 		return -1; 	} 	 	/* set timeout */ 	nRet = setsockopt(sSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&nNetTimeout, sizeof(int)); 	if (SOCKET_ERROR == nRet) { 		printf("setsockopt failed with code %d\n", WSAGetLastError()); 		//interval error, pass through 	}  	// Receive until the peer closes the connection 	do { 		nRet = recv(sSocket, pRcv, 10240, 0); 		if (nRet > 0) { 			printf("Bytes received: %d\n", nRet); 			pRcv += nRet; 		} 		else if (nRet == 0) { 			printf("Connection closed\n"); 		} 		else { 			printf("recv failed: %d\n", WSAGetLastError()); 		} 	} while (nRet > 0);        	printf("=====  Get Data from Server:\n%s\n", rcvBuf); 	closesocket(sSocket); 	WSACleanup();  	ParseRespone(rcvBuf);   	return 0; }    真的很谢谢你！想了好几天了！

user: user-2
content: receiver可以从onenet上下载数据了，至于led的显示，需要从解析好的数据中拿出ledstatus做相应的硬件控制，这个和硬件有关，不过这2个程序是OK的了，可以向onenet发送数据并成功取回，你可能会遇到cJSON导入的问题， 简单的方法是下载cJSON源文件把cJSON.h 和 cJSON.c导入到VS工程里，当然也可以把cJSON编译成lib库文件。  这个设计需要几点知识： 0.onenet平台上传下载数据相关协议(json格式) 1.json数据的了解 及 使用cJSON做成解析json字符串 2.使用winsock 进行http的GET 和 POST 操作 3.硬件控制（led 温度 风扇 等的控制）   #include <stdio.h> #include "winsock2.h" #include "cJSON.h"   #pragma comment(lib, "ws2_32.lib") //#pragma comment(lib, "cJSON.lib")  int GetDataFromOnenet(char *prcvbuf); char *ParserData(char *prcvbuf); void ParserJSON(char *pdata);  int main(void) { 	char rcvBuf[10240] = {0}; 	char *beginptr = NULL; 	int ret = 0;  	//get data from onenet server 	ret = GetDataFromOnenet(rcvBuf); 	if (ret != 0) { 		return -1; 	} 	 	beginptr = ParserData(rcvBuf); 	if (NULL == beginptr) { 		printf("ParserData failed\n"); 		return -1; 	}  	ParserJSON(beginptr + 4);  	return 0; }  int GetDataFromOnenet(char *prcvbuf) { 	SOCKET sSocket = INVALID_SOCKET; 	SOCKADDR_IN stSvrAddrIn = {0}; /* 服务器端地址 */ 	char sndBuf[2048] = {0};  	char *pRcv = prcvbuf; 	int num = 0; 	int nRet = SOCKET_ERROR; 	int sendlen = 0; 	int nNetTimeout = 1000; //1秒   	WSADATA wsaData;   	/* HTTP 消息构造开始，这是程序的关键之处 */ 	strcpy(sndBuf, "GET /devices/4604864/datastreams HTTP/1.1\r\n" 			"Host: api.heclouds.com\r\n" 			"api-key: Wscv0BNmRCZqJesh0R8rkNftTWM=\r\n" 			"Connection: close\r\n\r\n");  	// Initialize Winsock 	nRet = WSAStartup(MAKEWORD(2, 2), &wsaData); 	if (nRet != NO_ERROR) { 		printf("WSAStartup failed: %d\n", nRet); 		return -1; 	}   	stSvrAddrIn.sin_family = AF_INET; 	stSvrAddrIn.sin_port = htons(80); 	stSvrAddrIn.sin_addr.s_addr = inet_addr("183.230.40.33");   	sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 	if (INVALID_SOCKET == sSocket) { 		printf("socket failed with code %d\n", WSAGetLastError()); 		return -1; 	}  	//fast reuse addr 	BOOL bReuseaddr = TRUE; 	setsockopt(sSocket,SOL_SOCKET ,SO_REUSEADDR,(const char*)&bReuseaddr,sizeof(BOOL));   	 	/* 连接 */ 	nRet = connect(sSocket, (SOCKADDR*)&stSvrAddrIn, sizeof(SOCKADDR)); 	if (SOCKET_ERROR == nRet) { 		printf("connect failed with code %d\n", WSAGetLastError()); 		closesocket(sSocket); 		WSACleanup(); 		return -1; 	}   	/* 发送HTTP请求消息 */ 	printf("strlen(sndBuf)  %d,sndBuf %s\n",strlen(sndBuf),sndBuf); 	sendlen = send(sSocket, (char*)sndBuf, strlen(sndBuf), 0); 	printf("sendlen %d\r\n",sendlen);  	setsockopt(sSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&nNetTimeout, sizeof(int));  	// Receive until the peer closes the connection 	do { 		nRet = recv(sSocket, pRcv, 10240, 0); 		if (nRet > 0) { 			printf("Bytes received: %d\n", nRet); 			pRcv += nRet; 		} 		else if (nRet == 0) { 			printf("Connection closed\n"); 		} 		else { 			printf("recv failed: %d\n", WSAGetLastError()); 		} 	} while (nRet > 0);   	printf("%s\n", prcvbuf);  	closesocket(sSocket); 	WSACleanup();  	return 0; }  char * ParserData(char *prcvbuf) { 	if (prcvbuf == NULL)  		return NULL;  	//get response body 	return strstr(prcvbuf,"\r\n\r\n"); }  void ParserJSON(char *pdata) { 	cJSON *pJson = NULL; 	cJSON *pSub = NULL; 	cJSON *pSubSub = NULL; 	cJSON *taskArry = NULL; 	cJSON *tasklist = NULL; 	int arrySize = 0;  	pJson = cJSON_Parse(pdata); 	if (NULL == pJson) { 		printf("NULL == pJson\r\n"); 		return; }  	// get string from json 	pSub = cJSON_GetObjectItem(pJson, "errno"); 	if (NULL == pSub) { 		printf("NULL == pSub\r\n");   		return; 	} 	printf("obj_1(%s) : %d\n", pSub->string,pSub->valueint);  	taskArry = cJSON_GetObjectItem(pJson,"data"); 	arrySize = cJSON_GetArraySize(taskArry);//数组大小 	printf("arrySize  %d\r\n",arrySize);  	tasklist=taskArry->child;//子对象   	while (tasklist != NULL) { 		printf("%s:%d\n",cJSON_GetObjectItem(tasklist,"id")->valuestring,  			cJSON_GetObjectItem(tasklist,"current_value")->valueint); 		tasklist=tasklist->next; 	} }

user: user-2
content: sender可以成功向onenet发送数据了  #include <stdio.h> #include "cJSON.h" #include "winsock2.h"  //socket library #pragma comment(lib, "ws2_32.lib") //#pragma comment(lib, "cJSON.lib")  char *CreateCJSON(cJSON **pJsonRoot); void SendToServer(char *databuf);  int main(void) { 	cJSON *pJsonRoot = NULL; 	char *p = NULL;  	p = CreateCJSON(&pJsonRoot); 	if(NULL == p) { 		printf("CreateCJSON failed\n"); 		return -1; 	} 	 	SendToServer(p);  	if(pJsonRoot != NULL) { 		cJSON_Delete(pJsonRoot); 	}  	return 0; }  char *CreateCJSON(cJSON **pJsonRoot) { 	cJSON *pSubJson1 = NULL; 	cJSON *pArray1 = NULL;     cJSON *pSubJson2  = NULL; 	cJSON *pArray2   = NULL;  	int ledstatus = 0; 	char *p = NULL;  	//input led status and check input 	printf("please input ledstatus(0:close,1:open)\n"); 	scanf("%d", &ledstatus); 	if(!(ledstatus == 0 || ledstatus == 1)) { 		printf("ledstatus error\n");         return NULL; 	}  	//create JSON object 	*pJsonRoot = cJSON_CreateObject(); 	if(NULL == *pJsonRoot) { //error happend here 		printf("cJSON_CreateObject failed\n"); 		return NULL; 	} 	cJSON_AddItemToObject(*pJsonRoot, "datastreams", pArray1 = cJSON_CreateArray()); 	cJSON_AddItemToArray(pArray1, pSubJson1 = cJSON_CreateObject());     cJSON_AddStringToObject(pSubJson1,"id", "ledcontrol");     cJSON_AddItemToObject(pSubJson1, "datapoints", pArray2 = cJSON_CreateArray());     cJSON_AddItemToArray(pArray2, pSubJson2 = cJSON_CreateObject()); 	cJSON_AddNumberToObject(pSubJson2,"value", ledstatus);      	//print json content， just for test 	p = cJSON_PrintUnformatted(*pJsonRoot); 	if(NULL == p) { 	/* convert json list to string faild, exit 	 * because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot,  	 * if you also delete pSubJson, it will coredump, and error is : double free 	 */ 		printf("create object faild, exit 2\n"); 		cJSON_Delete(*pJsonRoot); 		return NULL; 	} 	 	printf("=====  JSON(len=%d):\n%s\n", strlen(p), p); 	return p; }  void ParseRespone(char* response) { 	char* start = NULL; 	char* end = NULL; 	cJSON *pJson = NULL; 	cJSON *pSub = NULL;  	start = strstr(response, "\r\n\r\n"); 	if (NULL == start) { 		printf("No Response Body\n"); 		return; 	}  	end = strchr(start, '}'); 	if (NULL == end) { 		printf("Invalid Json data\n"); 		return; 	}  	end++; 	*end = 0;  	pJson = cJSON_Parse(start); 	if (NULL == pJson) { 		printf("cJSON_Parse failed\n"); 		return; 	} 	// get errno from json 	pSub = cJSON_GetObjectItem(pJson, "errno"); 	if (NULL == pSub) { 		printf("cJSON_GetObjectItem failed\n"); 		return; 	}  	if (pSub->valueint != 0) { 		printf("\n>>>>>>UPLOAD DATA ERROR: %d, ", pSub->valueint); 		pSub = cJSON_GetObjectItem(pJson, "error"); 		if (NULL == pSub) { 			printf("error node not found\n"); 		} 		else { 			printf("%s\n\n", pSub->valuestring); 		} 	} 	else { 		printf("\n>>>>>UPLOAD DATA SUCCESS\n\n"); 	}  	cJSON_Delete(pJson);  }  void SendToServer(char *databuf) { 	char slen[32] = { 0 }; 	SOCKET sSocket = INVALID_SOCKET; 	SOCKADDR_IN stSvrAddrIn = { 0 }; /* 服务器端地址 */ 	char sndBuf[2048] = { 0 }; 	char rcvBuf[10240] = { 0 }; 	char *pRcv = rcvBuf; 	int num = 0; 	int nRet = SOCKET_ERROR; 	int sendlen = 0; 	int nNetTimeout = 1000;//1秒 	WSADATA wsaData;  	/* HTTP 消息构造开始，这是程序的关键之处 */ 	memset(sndBuf, 0x00, sizeof(sndBuf)); 	sprintf(sndBuf, "POST /devices/4604864/datapoints HTTP/1.1\r\n" 		"Host: api.heclouds.comr\r\n" 		"api-key: Wscv0BNmRCZqJesh0R8rkNftTWM=\r\n" 		"Content-Type: application/json\r\n" 		"Content-Length: %d\r\n" 		"Connection: keep-alive\r\n\r\n%s", strlen(databuf), databuf);  	// Initialize Winsock 	nRet = WSAStartup(MAKEWORD(2, 2), &wsaData); 	if (nRet != NO_ERROR) { 		printf("WSAStartup failed: %d\n", nRet); 		return -1; 	}   	// Create a SOCKET for connecting to server 	sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 	if (INVALID_SOCKET == sSocket) { 		printf("socket failed with code %d\n", WSAGetLastError()); 		return -1; 	}  	//fast reuse addr 	BOOL bReuseaddr = TRUE; 	setsockopt(sSocket,SOL_SOCKET ,SO_REUSEADDR,(const char*)&bReuseaddr,sizeof(BOOL));   	// The sockaddr_in structure specifies the address family, 	// IP address, and port of the server to be connected to. 	stSvrAddrIn.sin_family = AF_INET; //ipv4 protocol 	stSvrAddrIn.sin_port = htons(80); //80 port 	stSvrAddrIn.sin_addr.s_addr = inet_addr("183.230.40.33");  	// Connect to server. 	nRet = connect(sSocket, (SOCKADDR*)&stSvrAddrIn, sizeof(SOCKADDR)); 	if (SOCKET_ERROR == nRet) { 		printf("connect failed with code %d\n", WSAGetLastError()); 		closesocket(sSocket); 		WSACleanup(); 		return -1; 	}        	/* send http request */ 	printf("=====  Send Data to Server:\n%s\n", sndBuf); 	sendlen = send(sSocket, (char*)sndBuf, strlen(sndBuf), 0); 	if (SOCKET_ERROR == sendlen) { 		printf("send failed with code %d\n", WSAGetLastError()); 		closesocket(sSocket); 		WSACleanup(); 		return -1; 	} 	 	/* set timeout */ 	nRet = setsockopt(sSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&nNetTimeout, sizeof(int)); 	if (SOCKET_ERROR == nRet) { 		printf("setsockopt failed with code %d\n", WSAGetLastError()); 		//interval error, pass through 	}  	// Receive until the peer closes the connection 	do { 		nRet = recv(sSocket, pRcv, 10240, 0); 		if (nRet > 0) { 			printf("Bytes received: %d\n", nRet); 			pRcv += nRet; 		} 		else if (nRet == 0) { 			printf("Connection closed\n"); 		} 		else { 			printf("recv failed: %d\n", WSAGetLastError()); 		} 	} while (nRet > 0);        	printf("=====  Get Data from Server:\n%s\n", rcvBuf); 	closesocket(sSocket); 	WSACleanup();  	ParseRespone(rcvBuf);   	return 0; }

user: user-2
content: 发送代码中有很多错误我修改了， 与onenet的协议你得具体JSON格式你得具体一下，这个代码中发送给onenet的json， server认为是无效的。  #include <stdio.h> #include "cJSON.h" #include "winsock2.h"  //socket library #pragma comment(lib, "ws2_32.lib") //#pragma comment(lib, "cJSON.lib")  char *CreateCJSON(cJSON **pJsonRoot); void SendToServer(char *databuf);  int main(void) { 	cJSON *pJsonRoot = NULL; 	char *p = NULL;  	p = CreateCJSON(&pJsonRoot); 	if(NULL == p) { 		printf("CreateCJSON failed\n"); 		return -1; 	} 	 	SendToServer(p);  	if(pJsonRoot != NULL) { 		cJSON_Delete(pJsonRoot); 	}  	return 0; }  char *CreateCJSON(cJSON **pJsonRoot) { 	cJSON *pSubJson1 = NULL; 	cJSON *pArray1 = NULL;     cJSON *pSubJson2  = NULL; 	cJSON *pArray2   = NULL;  	int ledstatus = 0; 	char *p = NULL;  	//input led status and check input 	printf("please input ledstatus(0:close,1:open)\n"); 	scanf("%d", &ledstatus); 	if(!(ledstatus == 0 || ledstatus == 1)) { 		printf("ledstatus error\n");         return NULL; 	}  	//create JSON object 	*pJsonRoot = cJSON_CreateObject(); 	if(NULL == *pJsonRoot) { //error happend here 		printf("cJSON_CreateObject failed\n"); 		return NULL; 	} 	cJSON_AddItemToObject(*pJsonRoot, "datastreams", pArray1 = cJSON_CreateArray()); 	cJSON_AddItemToArray(pArray1, pSubJson1 = cJSON_CreateObject());     cJSON_AddStringToObject(pSubJson1,"id", "ledcontrol");     cJSON_AddItemToObject(pSubJson1, "datapoints", pArray2 = cJSON_CreateArray());     cJSON_AddItemToArray(pArray2, pSubJson2 = cJSON_CreateObject()); 	cJSON_AddNumberToObject(pSubJson2,"value", ledstatus);      	//print json content， just for test 	p = cJSON_Print(*pJsonRoot); 	if(NULL == p) { 	/* convert json list to string faild, exit 	 * because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot,  	 * if you also delete pSubJson, it will coredump, and error is : double free 	 */ 		printf("create object faild, exit 2\n"); 		cJSON_Delete(*pJsonRoot); 		return NULL; 	} 	 	printf("=====  JSON(len=%d):\n%s\n", strlen(p), p); 	return p; }  void SendToServer(char *databuf) { 	char slen[32] = { 0 }; 	SOCKET sSocket = INVALID_SOCKET; 	SOCKADDR_IN stSvrAddrIn = { 0 }; /* 服务器端地址 */ 	char sndBuf[2048] = { 0 }; 	char rcvBuf[10240] = { 0 }; 	char *pRcv = rcvBuf; 	int num = 0; 	int nRet = SOCKET_ERROR; 	int sendlen = 0; 	int nNetTimeout = 1000;//1秒 	WSADATA wsaData;  	/* HTTP 消息构造开始，这是程序的关键之处 */ 	memset(sndBuf, 0x00, sizeof(sndBuf)); 	sprintf(sndBuf, "POST /devices/4604864/datapoints HTTP/1.1\r\n"); 	strcat(sndBuf, "api-key: Wscv0BNmRCZqJesh0R8rkNftTWM=\r\n"); 	strcat(sndBuf, "Content-Type: application/x-www-form-urlencoded; charset=GBK\r\n"); 	strcat(sndBuf, "Host: api.clouds.com\r\n"); 	sprintf(slen, "Content-Length: %d\r\n", strlen(databuf)); //post body length 	strcat(sndBuf, slen); 	strcat(sndBuf, "\r\n\r\n"); //end of http header 	strcat(sndBuf, databuf); //http request body  	/* initiates use of the Winsock DLL by a process */ 	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { 		printf("WSAStartup failed\n"); 		return -1; 	}   	stSvrAddrIn.sin_family = AF_INET; //ipv4 protocol 	stSvrAddrIn.sin_port = htons(80); //80 port 	stSvrAddrIn.sin_addr.s_addr = inet_addr("183.230.40.33");//real server to connect  	//create a TCP stream socket 	sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 	if (INVALID_SOCKET == sSocket) { 		printf("socket failed with code %d\n", WSAGetLastError()); 		return -1; 	}   	/* connect to server, until timeout */ 	nRet = connect(sSocket, (SOCKADDR*)&stSvrAddrIn, sizeof(SOCKADDR)); 	if (SOCKET_ERROR == nRet) { 		printf("connect failed with code %d\n", WSAGetLastError()); 		return -1; 	}        	/* send http request */ 	printf("=====  Send Data to Server:\n%s\n", sndBuf); 	sendlen = send(sSocket, (char*)sndBuf, strlen(sndBuf), 0); 	if (SOCKET_ERROR == sendlen) { 		printf("send failed with code %d\n", WSAGetLastError()); 		return -1; 	} 	 	/* set timeout */ 	nRet = setsockopt(sSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&nNetTimeout, sizeof(int)); 	if (SOCKET_ERROR == nRet) { 		printf("setsockopt failed with code %d\n", WSAGetLastError()); 		//interval error, pass through 	}     	/* 接收HTTP响应消息 */ 	while (1) { 		num = recv(sSocket, pRcv, 10240, 0); 		if (num > 0) {//receive data 			pRcv += num; 		} 		else if (0 == num) {//the connection has been gracefully closed 			break; 		} 		else {//SOCKET_ERROR 			printf("recv failed with code %d\n", WSAGetLastError()); 			break; 		} 	}        	printf("=====  Get Data from Server:\n%s\n", rcvBuf); 	close(sSocket); 	WSACleanup();   	return 0; }