Discussion:
mfc socket + udp...
(too old to reply)
Steve
2005-01-27 13:42:37 UTC
Permalink
Hi,

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
Christopher J. Holland...........................................................................................................
2005-01-27 14:32:28 UTC
Permalink
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 Steve
Hi,
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
john
2005-01-27 16:11:41 UTC
Permalink
Post by Steve
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!
here is an url which examplifies how to use mfc's CAsyncSocket with udp:
http://www.cilu.as.ro/calculatoare/sp_asyncsocket.php

johnny
john
2005-01-27 16:46:27 UTC
Permalink
oh, the msdn site contains good info as well!
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_Windows_Sockets_in_MFC.asp
you'll find concrete info and example on how to use a casyncsocket, how to
derive from casyncsocket to receive notifications and so on...

johnny
Dave
2005-01-27 17:05:07 UTC
Permalink
just search your library... MSocUdp.exe Implement UDP Using CAsyncSocket

Q214396

i think there are a couple other client and server samples in there also.
Post by Steve
Hi,
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
Continue reading on narkive:
Loading...