成功实现与XServe的RPC通讯
今天晚上,成功实现与XServe的RPC通讯。采用最底层的通讯方式---Socket通讯方式,所发送的文档也是最原始的XML文档。
关键地方:收发XML Document前的四个字节,表示XML Document的字节个数。
发送顺序:先发送XML Document的字节个数,再发送XML Document本身。
接受顺序:先接受XML Document的字节个数,再接受XML Document本身。
如下:
1 // Defines the entry point for the console application. 2 // 3 #include "stdafx.h" 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <winsock2.h> 7 8 int _tmain(int argc, _TCHAR* argv[]) 9 { 10 11 //---------------------- 12 // Initialize Winsock 13 WSADATA wsaData; 14 int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); 15 if (iResult != NO_ERROR) 16 printf("Error at WSAStartup()\n"); 17 18 //---------------------- 19 // Create a SOCKET for connecting to server 20 SOCKET ConnectSocket; 21 ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 22 if (ConnectSocket == INVALID_SOCKET) 23 { 24 printf("Error at socket(): %ld\n", WSAGetLastError()); 25 WSACleanup(); 26 return 0; 27 } 28 29 //---------------------- 30 // The sockaddr_in structure specifies the address family, 31 // IP address, and port of the server to be connected to. 32 sockaddr_in clientService; 33 clientService.sin_family = AF_INET; 34 clientService.sin_addr.s_addr = inet_addr("192.168.1.98"); 35 clientService.sin_port = htons(9003); 36 37 //---------------------- 38 // Connect to server. 39 if ( connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) 40 { 41 printf( "Failed to connect.\n" ); 42 WSACleanup(); 43 return 0; 44 } 45 46 printf("Connected to server.\n"); 47 //----------------------------------------Request Command----------------------------------------------------------------------------------- 48 // pszXmlDocRequest For XML Request Document 49 // char* pszXmlDocRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"? ><methodCall><methodName>xserve.shutdown</methodName><params><param><value></value></param></params></methodCall>"; 50 // char* pszXmlDocRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>xmesh.actuate</methodName><params><param><value> <struct> <member> <name>destAddress</name> <value><int>6</int></value> </member> <member> <name>groupId</name> <value><int>125</int></value> </member> <member> <name>actDevice</name> <value><int>0</int></value> </member> <member> <name>actState</name> <value><int>2</int></value> </member></struct> </value></param></params></methodCall>"; 51 char* pszXmlDocRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>xmesh.get_config</methodName><params><param><value> <struct> <member> <name>destAddress</name> <value><int>6</int></value> </member> <member> <name>groupId</name> <value><int>125</int></value> </member> </struct> </value></param></params></methodCall>"; 52 printf("Request Xml Doc:\n------------------------\n"); 53 printf("%s", pszXmlDocRequest); 54 int iXmlDocRequestLen = (int)strlen(pszXmlDocRequest); // Request XML Document Length 55 BYTE byLen[4]; 56 ZeroMemory(byLen, sizeof(byLen)); 57 memcpy(byLen, &iXmlDocRequestLen, 4); 58 send(ConnectSocket, (char*)byLen, 4, 0); // First Send Request XML Document Length In 4 Bytes 59 send(ConnectSocket, pszXmlDocRequest, iXmlDocRequestLen, 0); // Then Send Request XML Document 60 61 // --------------------------------------XServe Respond-------------------------------------------------------------------------------------- 62 // First Get XServe Respond Xml Document Length 63 BYTE byRespondLen[4]; 64 ZeroMemory(byRespondLen, sizeof(byRespondLen)); 65 recv(ConnectSocket, (char*)byRespondLen, sizeof(byRespondLen), 0); 66 int iRespondXmlDocLen = 0; // XServe Respond Xml Document Length 67 memcpy(&iRespondXmlDocLen, byRespondLen, sizeof(byRespondLen)); 68 69 char* pszXmlDocRespond = NULL; // For XServe Respond Xml Document 70 if (iRespondXmlDocLen > 0) 71 { 72 pszXmlDocRespond = new char[iRespondXmlDocLen+1]; 73 ZeroMemory(pszXmlDocRespond, iRespondXmlDocLen+1); 74 recv(ConnectSocket, pszXmlDocRespond, iRespondXmlDocLen+1, 0); 75 } 76 //--------------------------------------------End-------------------------------------------------------------------------------------------- 77 WSACleanup(); 78 if (pszXmlDocRespond != NULL) 79 { 80 printf("\n\nXServe Respond Xml Doc:\n--------------------------\n"); 81 printf("%s\n\n", pszXmlDocRespond); 82 delete [] pszXmlDocRespond; 83 } 84 85 system("pause"); 86 return 0; 87 }