Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UDP Multicast Listener ... setting ports and groups?

Status
Not open for further replies.

CouponPages

Programmer
Feb 17, 2003
37
US
Based upon some stuff I found on Tek-Tips, I started a project that will involve reading some information from a multicast UDP feed.

The problem is I can't seem to get started on the right foot because there is so little information out there about doing any UDP/TCP/Socket networking out there for FoxPro.

The main objective is pretty simple. I need to set a port and group and then just read strings. I found a lot of things to read from UDP, but nothing about muticast groups.

Can anyone point me to a FAQ or anyplace that can get me started?

Thanks!

 
Here's a C/C++ version... most of it is contained in just a dozen or so lines of code, it's driving me crazy that I can't figure out how to get FoxPro to do the same.

Code:
#include "winsock2.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
//#include "multicast.h"



 main ()
 {

  WSADATA data;
    WORD version; 
    int ret = 0;  

    version = (MAKEWORD(2, 2)); 
    ret = WSAStartup(version, &data); 
    if (ret != 0) 
    {  
        ret = WSAGetLastError(); 
        
        if (ret == WSANOTINITIALISED) 
        {  
            printf("not initialised"); 
        }
    }


SOCKET udp_socket;
struct sockaddr_in sockaddr;
int peerlen;

char recvbuffer[256];
int retval;


udp_socket = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 
       	0, 0, WSA_FLAG_OVERLAPPED | WSA_FLAG_MULTIPOINT_C_LEAF | WSA_FLAG_MULTIPOINT_D_LEAF); 
    
    if (udp_socket == INVALID_SOCKET)  
    { 
        printf ("WSASocket() failed, Err: %d\n", WSAGetLastError()); 
        return FALSE;  
    }  

// Bind address
SOCKADDR_IN sockAddr;
	memset((char *)&sockAddr, 0, sizeof(sockAddr));   //fooling with memory
   	sockAddr.sin_family = AF_INET;
   	sockAddr.sin_port = htons((WORD)(9051));
	sockAddr.sin_addr.s_addr = inet_addr("230.0.1.1");


udp_socket = WSAJoinLeaf(udp_socket, (SOCKADDR *)&sockAddr,
        	sizeof(sockAddr), 0, 0, 0, 0, JL_RECEIVER_ONLY);

	int result = GetLastError();
    if (INVALID_SOCKET == udp_socket)
   	if( bind(udp_socket, (SOCKADDR *)&sockAddr, sizeof(sockAddr)));

	


   //bind to socket
  bind(udp_socket,(struct sockaddr*) &sockaddr,sizeof(sockaddr)); 

peerlen=sizeof(sockAddr);


while(1) {

  retval = recvfrom(udp_socket, recvbuffer, sizeof(recvbuffer), 0, (struct sockaddr *)&sockAddr, &peerlen);

  //recvfrom returns -1 if it didn't receive anything.
  //If retval is NOT -1, then the recvfrom must have worked, so let's display
  //whatever multicast data was sent.

  if (retval != -1) {printf("%s", recvbuffer);}   //to test my output on the screen and make sure it looks ok.

  }
    return 0;   
}
 
For anyone in the future looking to do the same, here's my final solution. I was unable to find a way to call the native libraries myself, and I gave up on trying to encapsulate the C++ solution I described in my last post, so I looked for a third-party library.

I ended up finding a WinSock library from Dart.Com. They have a 30 day trial on the site, so I tried it. The documentation was crude, but ultimately I managed to try the following code and it worked.

From the command window I typed:

Code:
_vfp.AutoYield = .f.

otest=CREATEOBJECT("dart.udp.1")dso=CREATEOBJECT("dart.Dartstream.1")

otest.Open(9051)

otest.MaxSubnets = 3

otest.Join("230.0.1.1")

dso.position = 0

Cnt = otest.Receive(dso)

To confirm it worked I typed

Code:
? cnt
(# bytes received is printed... which means it worked)


To see the string received, I typed:
Code:
dso.position = 0

? dso.readstring()
(the string read from the feed is printed)


It bugs me a bit that I couldn't do this without a third-party library, but as long as it works... I'm happy.

 
Thanks Andy,

I've experimented, but I can't get past the first line:

Code:
oTest = CREATEOBJECT("MSWinSock.WinSock")

Once I create the object, I can't seem to find any properties or methods to support UDP Multicasting. Specifically, I need to join a group, but there is so little written on the subject out there. It's driving me crazy.

This part of my application is pretty simple. I need read a multicast feed of stock quotes, then log them into a database. But without setting the group, I'm stuck.

Do you know how to read such a feed?
 
I have set up a UDP client before by binding to a server IP and port, but I have never heard of UDP Multicasting or joining a group. I'm not sure what that entails. Sorry.

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top