/[H8]/trunk/docs/Microchip TCP_IP stack/TCPIP Stack/MCHPDetect/MCHPDetect.cpp
ViewVC logotype

Contents of /trunk/docs/Microchip TCP_IP stack/TCPIP Stack/MCHPDetect/MCHPDetect.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (show annotations) (download)
Thu Apr 19 09:01:15 2007 UTC (17 years, 1 month ago) by hedin
File size: 3630 byte(s)
added the TCP/IP stack, source code.
1 /*********************************************************************
2 *
3 * Microchip Announcement Detector
4 *
5 *********************************************************************
6 * FileName: MCHPDetect.cpp
7 * Dependencies: Must be linked with Ws2_32.lib
8 * Processor: x86
9 * Complier: Microsoft Visual C++ 6.0
10 * Borland C++ Builder 6
11 * Company: Microchip Technology, Inc.
12 *
13 * Software License Agreement
14 *
15 * This software is owned by Microchip Technology Inc. ("Microchip")
16 * and is supplied to you for use exclusively as described in the
17 * associated software agreement. This software is protected by
18 * software and other intellectual property laws. Any use in
19 * violation of the software license may subject the user to criminal
20 * sanctions as well as civil liability. Copyright 2006 Microchip
21 * Technology Inc. All rights reserved.
22 *
23 * This software is provided "AS IS." MICROCHIP DISCLAIMS ALL
24 * WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED
25 * TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
26 * INFRINGEMENT. Microchip shall in no event be liable for special,
27 * incidental, or consequential damages.
28 *
29 * Author Date Comment
30 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31 * Howard Schlunder 10/8/04 Original
32 ********************************************************************/
33
34 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
35
36 #define UDPPort 30303
37
38 #include <Winsock2.h>
39 #include <iostream>
40 #include <stdio.h>
41
42
43
44 int main(int argc, char* argv[])
45 {
46 WSADATA WSAData;
47 SOCKET MySocket;
48 char PacketPayload[1500];
49 int ret;
50 sockaddr_in MySocketInfo;
51 sockaddr RemoteAddr;
52 int RemoteAddrSize;
53 unsigned int MessageCount = 1;
54
55
56 if(WSAStartup((short)0x0202, &WSAData) != 0)
57 {
58 printf("WSAStartup failed!\n");
59 return 1;
60 }
61
62
63 MySocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
64
65 if(MySocket == INVALID_SOCKET)
66 {
67 std::cout << "Unable to create a socket.\n";
68 WSACleanup();
69 return 1;
70 }
71
72
73 MySocketInfo.sin_family = AF_INET;
74 MySocketInfo.sin_port = htons(UDPPort);
75 MySocketInfo.sin_addr.S_un.S_addr = htonl(ADDR_ANY);
76 ZeroMemory(&MySocketInfo.sin_zero, sizeof(MySocketInfo.sin_zero));
77
78 ret = bind(MySocket, (const struct sockaddr *)&MySocketInfo, sizeof(MySocketInfo));
79 if(ret == INVALID_SOCKET)
80 {
81 std::cout << "Bind procedure failed.\n";
82 WSACleanup();
83 return 1;
84 }
85
86 RemoteAddrSize = sizeof(RemoteAddr);
87
88 std::cout << "Microchip Announcement Detector\n"
89 "Copyright (c) 2004 Microchip Technology, Inc. Ver. 1.0 (Oct 06 2004)\n"
90 "Written by Howard Henry Schlunder\n\n"
91 "Waiting for data on UDP port " << UDPPort << "...\n\n";
92
93
94 while(1)
95 {
96 ret = recvfrom(MySocket, PacketPayload, sizeof(PacketPayload), 0, &RemoteAddr, &RemoteAddrSize);
97 if(ret == SOCKET_ERROR)
98 {
99 std::cout << "Socket error while trying to receive.\n";
100 WSACleanup();
101 return 1;
102 }
103
104 PacketPayload[ret] = 0; // Null terminate
105
106 std::cout << "Total packets received: " << MessageCount++ << "\n"
107 << "Received " << ret << " bytes of data from "
108 << (unsigned char)RemoteAddr.sa_data[2] + '\0' << '.'
109 << (unsigned char)RemoteAddr.sa_data[3] + '\0' << '.'
110 << (unsigned char)RemoteAddr.sa_data[4] + '\0' << '.'
111 << (unsigned char)RemoteAddr.sa_data[5] + '\0' << '\n'
112 << PacketPayload << "\n\n";
113 }
114
115
116 WSACleanup();
117 return 0;
118 }
119

  ViewVC Help
Powered by ViewVC 1.1.20