Dear you,
My ROOT Version 5.34/00.
I use the TUDPSocket class to build a UDP broadcast socket. The example file “testTUDPSocket.C” in the directory $ROOTSYS\tutorials\net is modified. I changed the “localhost” in
to “broadcast”
the IP of “broadcast” is 255.255.255.255 or 90.0.6.255 in the ‘hosts’ file, then I use the “udpserver.c” in the same directory to implement the connection, and the “testTUDPSocket.C” is executed with “.x testTUDPSocket.C” in ROOT.
This method works well with Windows (Windows 7 Pro), however shows an error as following under Linux (Fedora 13 ) when I execute “.x testTUDPSocket.C”,
[color=#0000FF]SysError in TUnixSystem::UnixUdpConnect: connect (localbroad:1500) (Permission denied)
Error in : cannot connect to localhost[/color]
But if I use the standard socket method (socket.h) it works ok! The program is shown as following,
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <arpa/inet.h>
#include <string.h>
using namespace std;
void sig_handle(int signum)
{
return;
}
int main(int argc,char *argv[])
{
char buffer[512];
int bordcast_socket;
int read_count=0;
int addr_len=sizeof(struct sockaddr);
struct sockaddr_in addr_info,addr_net;
int opt=1;
if(argc!=2)
{
printf("Use page boradcast <ip address>\n");
return -1;
}
addr_info.sin_family=AF_INET;
addr_info.sin_addr.s_addr=inet_addr(argv[1]);
addr_info.sin_port=htons(1500);
bordcast_socket=socket(AF_INET,SOCK_DGRAM,0);
if(bordcast_socket<0)
{
perror("bordcast_socket");
return -1;
}
signal(SIGALRM,sig_handle);
setsockopt(bordcast_socket,SOL_SOCKET,SO_BROADCAST,&opt,sizeof(opt));
while(fgets(buffer,sizeof(buffer),stdin)!=NULL)
{
sendto(bordcast_socket,buffer,strlen(buffer),0,(struct sockaddr *)&addr_info,addr_len);
alarm(5);
memset(buffer,0,sizeof(buffer));
// for (; ; )
//{
// read_count=0;//recvfrom(bordcast_socket,buffer,sizeof(buffer),0,(struct sockaddr *)&addr_net,&addr_len);
}
return 0;
}
Any help is appreceated.
Best regards!
Han