📄 在evc下通过wap发送数据.txt
字号:
#include <windows.h>
#include <wap.h>
//#include "filehandle.h"//walterm; 2003-07-11; disabled 2003-07-16
// Message waiting timeout in seconds
#define TIMEOUT 300
// Number to send/receive data
// REPLACE WITH THE SMS NUMBER OF THE DEVICE RUNNING THE SAMPLE
const TCHAR c_szAddress[] = TEXT("+8613916084056");//("+8613800210500");//walterm; 2003-07-11
// Data to send
//const TCHAR c_szData[] = TEXT("This is a demo of using WDP/SMS to transport data.");//walterm; disabled to send file data now; 2003-07-11
// String appended to outputed data
const TCHAR c_szHeader[] = TEXT("PIC VSMS: "); //walterm; 2003-07-16
int SendData();
void PreparePrimitive( WDP_UNITDATA* const pPrimitive,
const WAP_ADDRESS waSender, const DWORD dwSenderPort,
const WAP_ADDRESS waReceiver, const DWORD dwReceiverPort,
const BYTE* pbUserData, const DWORD dwUserDataSize )
{
ASSERT(pPrimitive && pbUserData);
pPrimitive->wpiPrimitiveID = WAP_PRIMITIVE_ID_T_DUNITDATA;
pPrimitive->wptPrimitiveType = WAP_PRIMITIVE_TYPE_REQUEST;
pPrimitive->dwValidFields = WDP_FIELD_SOURCEADDRESS |
WDP_FIELD_SOURCEPORT |
WDP_FIELD_DESTINATIONADDRESS |
WDP_FIELD_DESTINATIONPORT |
WDP_FIELD_USERDATA;
pPrimitive->waSourceAddress = waSender;
pPrimitive->dwSourcePort = dwSenderPort;
pPrimitive->waDestinationAddress = waReceiver;
pPrimitive->dwDestinationPort = dwReceiverPort;
pPrimitive->pbUserData = pbUserData;
pPrimitive->dwUserDataSize = dwUserDataSize;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// This program demonstrates how to send data via WDP/SMS. It creates two WDP clients on the same
// device ( thus same sms address ) but different port. One client sends data to the other one.
//
// Return: 0 for success, -1 for failure
//////////////////////////////////////////////////////////////////////////////////////////////////
int SendData()
{
// char file_data[30];//walterm; 2003-07-11; disabled 2003-07-16;
WAP_HANDLE whSender, whReceiver;
HANDLE hSenderMsgEvent, hReceiverMsgEvent;
DWORD dwSenderPort = 21;//9168;//1234;
DWORD dwReceiverPort = 23;//9169;//1235;
BOOL fOK = TRUE;
// DWORD dwDataSize = sizeof(file_data);//walterm; 2003-07-11; disabled 2003-07-16;
WDP_UNITDATA wuToSend;
HANDLE hFile;//walterm; 2003-07-16
DWORD dwBytesRead;
char buff[160];
FILE *debug_output;
debug_output = fopen ("\\Temp\\debug_output.txt", "wt"); //walterm; 2003-07-17
// Sender and receiver's address (hard-coded)
WAP_ADDRESS waAddress;
waAddress.watAddressType = WAP_ADDRESS_TYPE_GSM_SMS;
_tcscpy(waAddress.ptsAddress, c_szAddress); //phonebum 13916084056
fwrite("WDP_SMS data sending and receiving demo\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("WDP_SMS data sending and receiving demo"));
// Create two WDP clients on different ports
fwrite("Creating sender and receiver...\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Creating sender and receiver..."));
// Create sender
if (SUCCEEDED(WapOpen(WAP_LAYER_WDP, dwSenderPort, &whSender, &hSenderMsgEvent)))
{
fprintf ( debug_output, "\n%s%i%s\n", "Sender created on port", dwSenderPort, "."); //walterm; 2003-07-17
// DebugOutput(_T("Sender created on port %d."), dwSenderPort);
// Create receiver
if(SUCCEEDED(WapOpen(WAP_LAYER_WDP, dwReceiverPort, &whReceiver, &hReceiverMsgEvent)))
{
fprintf ( debug_output, "\n%s%i%s\n", "Receiver created on port", dwReceiverPort, "."); //walterm; 2003-07-17
// DebugOutput(_T("Receiver created on port %d."), dwReceiverPort);
}
else
{
fwrite("Error! Fail to create receiver!\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Error! Fail to create receiver!"));
WapClose(whSender);
return -1;
}
fprintf(debug_output, "\n%s%i\n", "Sender and receiver's address:", waAddress.ptsAddress); //walterm; 2003-07-17
// DebugOutput(_T("Sender and receiver's address: %s"), waAddress.ptsAddress);
}
else
{
fwrite("Error! Fail to create sender!\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Error! Fail to create sender!"));
return -1;
}
hFile = CreateFile (TEXT("\\Temp\\test.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);//walterm; 2003-07-16
ReadFile (hFile, buff, 160, &dwBytesRead, NULL);//walterm; 2003-07-17
// Prepare a WDP primitive for sender to send
// DebugOutput(_T("Date to be sent from sender to receiver: %s"), c_szData);
fprintf ( debug_output, "\n%s%i%s\n", "Size of the data:", dwBytesRead, "bytes"); //walterm; 2003-07-17
// DebugOutput(_T("Size of the data: %d bytes"), dwBytesRead);//sizeof((char*)hFile));//walterm; 2003-07-16
//fileInput(file_data); //walterm; 2003-07-11; disabled 2003-07-16;
// Prepare the wdp primitive
PreparePrimitive(&wuToSend, waAddress, dwSenderPort, waAddress, dwReceiverPort, (BYTE *) hFile, dwBytesRead);//dwDataSize);//walterm; 2003-07-11
// Send the primitive to receiver
fwrite("Sender sending data to receiver.\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Sender sending data to receiver."));
if (SUCCEEDED(WapSend(whSender, (WAP_PRIMITIVE_BASE *) &wuToSend)))
{
fwrite("Sending OK.", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Sending OK."));
// Receiver waits for incoming message
fprintf ( debug_output, "\n%s%i%s\n", "Receiver waiting", TIMEOUT, "secs for message..."); //walterm; 2003-07-17
// DebugOutput(_T("Receiver waiting %u secs for message..." ), TIMEOUT );
if (WAIT_OBJECT_0 == WaitForSingleObject(hReceiverMsgEvent, TIMEOUT * 1000))
{
fwrite("Receiver got message\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Receiver got message"));
// Get primitive size in order to read it
DWORD dwSize = 0;
if (SUCCEEDED(WapGetNextPrimitiveSize(whReceiver, &dwSize)) && dwSize)
{
fprintf ( debug_output, "\n%s%i%s\n", "Get primitive size:", dwSize); //walterm; 2003-07-17
// DebugOutput(_T("Get primitive size: %u"), dwSize);
// Allocate buffer to hold incoming message
BYTE *pbBuffer = new BYTE[dwSize];
if (pbBuffer)
{
// Read the primitive
if (SUCCEEDED(WapRead(whReceiver, (WAP_PRIMITIVE_BASE *) pbBuffer, dwSize)))
{
// Successfully read the primitive
fwrite("Receiver successfully read the primitive.\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Receiver successfully read the primitive."));
WDP_UNITDATA *pReceived = (WDP_UNITDATA *) pbBuffer;
// Now validate the primitive
if ((WAP_PRIMITIVE_ID_T_DUNITDATA == pReceived->wpiPrimitiveID) && (WAP_PRIMITIVE_TYPE_INDICATION == pReceived->wptPrimitiveType))
{
// Print sources address
if (WTP_FIELD_SOURCEADDRESS & pReceived->dwValidFields)
{
TCHAR ptsAddress[MAX_WAP_ADDRESS_LENGTH];
_tcsncpy(ptsAddress, pReceived->waSourceAddress.ptsAddress,MAX_WAP_ADDRESS_LENGTH);
ptsAddress[MAX_WAP_ADDRESS_LENGTH - 1] = _T('\0'); // force it to be NULL-terminated
fprintf ( debug_output, "\n%s%i%s\n", "Primitive's source address:", ptsAddress); //walterm; 2003-07-17
// DebugOutput(_T("Primitive's source address: %s"), ptsAddress);
}
else
{
fwrite("Error! Missing source address.\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Error! Missing source address."));
fOK = FALSE;
}
// Compare received data with sent data
if ((WDP_FIELD_USERDATA & pReceived->dwValidFields) && (pReceived->pbUserData))
{
// Compare data size
fprintf ( debug_output, "\n%s%i%s\n", "Received user data size:", pReceived->dwUserDataSize); //walterm; 2003-07-17
// DebugOutput(_T("Received user data size: %d"), pReceived->dwUserDataSize);
if (pReceived->dwUserDataSize == sizeof((char*)hFile))//walterm; 2003-07-16
{
fwrite("Data size comparison succeeded.\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Data size comparison succeeded."));
// Compare data content
if (!memcmp((BYTE *) hFile, pReceived->pbUserData, sizeof((char*)hFile)))//walterm; 2003-07-16
{
// Print received user data
fprintf ( debug_output, "\n%s%i%s\n", "Received data:", pReceived->pbUserData); //walterm; 2003-07-17
// DebugOutput(_T("Received data: %s"), pReceived->pbUserData);
fwrite("Data content succeeded.\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Data content comparison succeeded"));
}
else
{
fwrite("Error! Data content comparison failed.\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Error! Data content comparison failed."));
fOK = FALSE;
}
}
else
{
fwrite("Error! Data size comparison failed.\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Error! Data size comparison failed"));
fOK = FALSE;
}
}
else
{
fwrite("Error! Missing user data.\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Error! Missing user data."));
fOK = FALSE;
}
}
else
{
fwrite("Error! Invalid primitive ID or primitive type.\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Error! Invalid primitive ID or primitive type."));
fOK = FALSE;
}
}
else
{
fwrite("Error! Receiver failed to read primitive\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Error! Receiver failed to read primitive"));
fOK = FALSE;
}
delete [] pbBuffer;
}
else
{
fwrite("Error! Receiver fail to allocate buffer to hold incoming message.\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Error! Receiver fail to allocate buffer to hold incoming message."));
fOK = FALSE;
}
}
else
{
fwrite("Error! Receiver failed to get primitive size\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T( "Error! Receiver failed to get primitive size" ) );
fOK = FALSE;
}
}
else
{
fwrite("Warning! Receiver timed out waiting for message\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Warning! Receiver timed out waiting for message" ) );
fOK = FALSE;
}
}
else
{
fwrite("Error! Fail to send data!\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Error! Fail to send data!"));
fOK = FALSE;
}
// Clean up
WapClose(whSender);
WapClose(whReceiver);
if (fOK == FALSE)
fwrite("Send and receive failed.\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
else
fwrite("Send and receive succeeded.\n", sizeof (char), 30, debug_output); //walterm; 2003-07-17
// DebugOutput(_T("Send and receive %s. Exit."), ((fOK) ? _T("succeeded") : _T("failed")));
fclose(debug_output);//walterm; 2003-07-17
return ((fOK) ? 0 : -1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -