/////////////////////////////////////////////////////////////////////////////// // // (c) Digi International Inc. 2002. All Rights Reserved // // Digi provides this document "as is," without warranty of any kind, // either expressed or implied, including, but not limited to, the implied // warranties of fitness or merchantability for a particular purpose. Digi // may make improvements and/or changes in this document or in the product(s) // and/or the program(s) described in this document at any time. // // This document could include technical inaccuracies or typographical errors. // Changes are periodically made to the information herein; these changes may // be incorporated in new editions of the publication. // // Digi International Inc. // 11001 Bren Road East // Minnetonka, MN 55343, USA // Tel: +1 (952) 912-3444 // Fax: +1 (952) 912-4952 // http://www.digi.com/ // /////////////////////////////////////////////////////////////////////////////// // UDPSerial.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "HexDumpData.h" #include "MySocket.h" int main(int argc, char* argv[]) { printf("Start\n"); WSADATA WSAData = {0}; WSAStartup(WINSOCK_VERSION, &WSAData); printf("Creating socket ...\n"); SOCKET MySocket = MySocketOpen(SOCK_DGRAM, 7777); if (MySocket != INVALID_SOCKET) { SOCKADDR SockAddr = {0}; int SockAddrLength = sizeof(SockAddr); getsockname(MySocket, &SockAddr, &SockAddrLength); char LocalAddress[32] = {0}; DWORD LocalAddressLength = sizeof(LocalAddress); WSAAddressToString(&SockAddr, SockAddrLength, NULL, LocalAddress, &LocalAddressLength); printf("Socket created on %s\n", LocalAddress); // IMPORTANT! // // This is the IP Address and port number of the device to // which data will be sent. Modify these values accordingly // to match you device configuration. char * AddressToSendTo = "10.0.0.1"; // Change this to be the IP address for your device WORD PortToSendTo = 2101; // Default base port # for serial port 1 BYTE SendData[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', }; printf("Sending:\n"); HexDumpData(SendData, sizeof(SendData)); MySocketSendTo(MySocket, SendData, sizeof(SendData), AddressToSendTo, PortToSendTo); BYTE RecvData[256] = {0}; DWORD BytesReceived = sizeof(RecvData); char AddressReceivedFrom[32]; DWORD AddressReceivedFromLength = sizeof(AddressReceivedFrom); if (MySocketRecvFrom(MySocket, RecvData, &BytesReceived, AddressReceivedFrom, &AddressReceivedFromLength) == 0) { printf("Received data from %s:\n", AddressReceivedFrom); HexDumpData(RecvData, BytesReceived); } printf("Closing socket\n"); MySocketClose(MySocket); printf("Finish\n"); } else { printf("Socket creation failed\n"); } WSACleanup(); return 0; }