Here is some code for a UDP Client.
It's in C, but you could probably put it in an MFC app.
I built a UDP Server with a uController so, I have no leads as far as that
goes.
/* Copyright (c) Gary T. Desrosiers, 1999. All Rights Reserved. */
#include <winsock.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
short command;
struct sockaddr_in sin;
WSADATA wsaData;
SOCKET s;
if(argc < 2)
{
printf("Syntax:\tcontroller on\n");
printf("or\n");
printf("\tcontroller off\n");
return(0);
}
if(stricmp(argv[1],"on") == 0)
command = htons(0); // Make sure command is in network byte order (big
Endian)
if(stricmp(argv[1],"off") == 0)
command = htons(1); // Same
if (WSAStartup(0x101,&wsaData) == SOCKET_ERROR)
{
printf("WSAStartup() failed, rc=%d\n",WSAGetLastError());
WSACleanup();
return -1;
}
sin.sin_addr.S_un.S_addr = inet_addr("192.168.0.2"); // Embedded Ethernet's
IP address
sin.sin_family = AF_INET;
sin.sin_port = htons(1000); // The port that the Basic Stamp program is
listening on.
s = socket(AF_INET,SOCK_DGRAM,0);
if(s<0)
{
printf("Couldn't get a socket, rc=%d\n",WSAGetLastError());
WSACleanup();
return -1;
}
if(connect(s,(struct sockaddr*)&sin,sizeof(sin)) == SOCKET_ERROR)
{
printf("connect() failed, rc=%d\n",WSAGetLastError());
WSACleanup();
return -1;
}
send(s,(const char *)&command,2,0); // Send the two byte (16 bit) value to
the Basic Stamp
closesocket(s);
WSACleanup();
return(0);
}
Good Luck,
--
Christopher J. Holland [!MVP]
http://www.mvps.org/vcfaq/
http://www.codeguru.com
http://www.codeproject.com
http://www.naughter.com/
http://support.microsoft.com/default.aspx
http://msdn.microsoft.com/howto/
http://msdn.microsoft.com/library/
www.flounder.com/mvp_tips.htm
Post by SteveHi,
I want to quickly setup a simple UDP client and a simple UDP server to be
run as two different MFC applications.
There must be loads of examples how to do that, but I just cannot find any
of it!?!?!?
I'd be very glad if some kind soul could point me to an URL, magazine
article or a book for a good UDP MFC Socket example!
Thanks in advance!
Steve