33 if(port < 0 || port >= 256*256)
return -1;
34 struct in_addr IpAddress;
37 memset(&IpAddress,0,
sizeof(IpAddress));
40 host = gethostbyname(adr);
44 IpAddress.s_addr = inet_addr(adr);
45 if(IpAddress.s_addr == INADDR_NONE)
47 ::printf(
"rlIpAdr::setAdr() could not gethostbyname(%s)\n",adr);
53 memcpy(&IpAddress,host->h_addr,host->h_length);
57 address.sin_port = htons((
short) port);
78 s = socket(AF_INET,SOCK_DGRAM,0);
82 ::printf(
"rlUdpSocket::rlUdpSocket could not get socket\n");
101 if(
s == -1)
return -1;
104 return setsockopt(
s,SOL_SOCKET,opt,(
const char *) &on,
sizeof(on));
106 return setsockopt(
s,SOL_SOCKET,opt,&on,
sizeof(on));
112 if(
s == -1)
return -1;
113 if(optlen <= 0)
return -1;
116 return setsockopt(
s,level,optname,(
const char *) &optval,optlen);
118 return setsockopt(
s,level,optname,optval,optlen);
124 if(port < 0 || port >= 256*256)
return -1;
128 address.sin_port = htons((
short) port);
129 address.sin_addr.s_addr = htonl(INADDR_ANY);
133 ::printf(
"rlUdpSocket::setAdr() bind() failed port=%d\n", port);
141 if(timeout < 0)
return -1;
142 struct timeval timout;
143 fd_set wset,rset,eset;
152 timout.tv_sec = timeout / 1000;
153 timout.tv_usec = (timeout % 1000) * 1000;
155 ret =
::select(maxfdp1,&rset,&wset,&eset,&timout);
156 if(ret == 0)
return 0;
167 if(ret != 1)
return -1;
173 (
struct sockaddr *) &source->
address, (
int FAR *) &len);
177 (
struct sockaddr *) &source->
address, (socklen_t *) &len);
181 (
struct sockaddr *) &source->
address, (
size_t *) &len);
185 ::printf(
"ERROR: rlUdpSocket::read()\n");
190 unsigned char *cbuf = (
unsigned char *) buf;
191 ::printf(
"rlUdpSocket()::recvfrom() ret=%d data=[0x%x",ret,cbuf[0]);
192 for(
int i=1; i<ret; i++) ::
printf(
",0x%x",cbuf[i]);
202 (
struct sockaddr *) &dest->
address,
sizeof(
struct sockaddr_in));
205 (
struct sockaddr *) &dest->
address,
sizeof(
struct sockaddr_in));
207 if(ret < 0)
::printf(
"ERROR: rlUdpSocket::sendto()\n");
210 unsigned char *cbuf = (
unsigned char *) buf;
211 ::printf(
"rlUdpSocket()::sendto() ret=%d data=[0x%x",ret,cbuf[0]);
212 for(
int i=1; i<ret; i++) ::
printf(
",0x%x",cbuf[i]);
227 if(ret < 0)
return ret;
228 return sendto(message,strlen(message)+1,dest);
234 int main(
int ac,
char **av)
240 dest.
setAdr(
"localhost",5050);
247 ret = udp.
sendto(av[1], strlen(av[1])+1, &dest);
250 printf(
"udp.sendto(%s) ret=%d\n",av[1],ret);
252 else printf(
"udp.sendto() failed\n");
258 ret = udp.
bind(5050);
261 if((ret = udp.
recvfrom(buf,
sizeof(buf), &source, 1000)) > 0)
263 printf(
"udp.recvfrom(%s) ret=%d\n",buf,ret);
264 if(dest == source) printf(
"dest==source\n");
265 else printf(
"dest!=source\n");
267 else printf(
"udp.recvfrom() failed or timeout\n");
int recvfrom(void *buf, int maxlen, rlIpAdr *source, int timeout=-1)
int printf(rlIpAdr *dest, const char *format,...)
int setAdr(const char *adr, int port)
int rlvsnprintf(char *text, int len, const char *format, va_list ap)
int operator==(rlIpAdr &address1)
struct sockaddr_in address
int sendto(const void *buf, int len, rlIpAdr *dest)
struct sockaddr_in address