|
Boost Users : |
From: Caleb Epstein (caleb.epstein_at_[hidden])
Date: 2004-10-05 14:24:39
On Tue, 5 Oct 2004 12:00:37 -0000 (UTC), dave_at_[hidden]
<dave_at_[hidden]> wrote:
> OK, awake again and looking into the beolow problem more...
> my program pulls a message off of a UDP datagram socket with
>
> int s = socket(AF_INET, SOCK_DGRAM, 0);
> int iVal;
> struct sockaddr_in oFromAddr;
> char msg[1024];
> int iMsgLen;
> socklen_t iFromLen;
>
> oMyAddr.sin_family = AF_INET;
> oMyAddr.sin_addr.s_addr = htonl(INADDR_ANY);
> oMyAddr.sin_port = htons(5060);
> iVal = bind(s, (struct sockaddr *) &oMyAddr, sizeof (oMyAddr));
>
> int iVal = recvfrom(s, (void *)&msg, iMsgLen, 0, (struct sockaddr *)
> &oFromAddr, &iFromLen);
If this is the actual code, iMsgLen is being used uninitialized.
Also, you're passing the address of msg, which is incorrect. Here's
working code:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
using namespace std;
int
main ()
{
int s = socket (AF_INET, SOCK_DGRAM, 0);
if (s < 0)
{
perror ("socket");
return 1;
}
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(5060);
if (bind (s, (struct sockaddr *) &addr, sizeof (addr)) != 0)
{
perror ("bind");
return 1;
}
char msg[1024];
while (1)
{
socklen_t addrlen = sizeof (addr);
int len = recvfrom (s, (void *) msg, sizeof (msg), 0,
(struct sockaddr*) &addr, &addrlen);
if (len <= 0)
{
perror ("recvfrom");
return 1;
}
cout.write (msg, len);
cout.flush ();
}
return 0;
}
-- Caleb Epstein caleb.epstein_at_[hidden]
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net