#include <rludpsocket.h>

Public Member Functions | |
| rlUdpSocket (int debug=0) | |
| virtual | ~rlUdpSocket () |
| int | setSockopt (int opt) |
| int | setSockopt (int level, int optname, void *optval, int optlen) |
| int | bind (int port) |
| int | select (int timeout) |
| int | recvfrom (void *buf, int maxlen, rlIpAdr *source, int timeout=-1) |
| int | sendto (const void *buf, int len, rlIpAdr *dest) |
| int | printf (rlIpAdr *dest, const char *format,...) |
Public Attributes | |
| int | debug |
| int | readflag |
| int | writeflag |
Private Attributes | |
| struct sockaddr_in | address |
| int | s |
class for encapsulating UDP socket calls
Definition at line 53 of file rludpsocket.h.
| rlUdpSocket::rlUdpSocket | ( | int | debug = 0 |
) |
Definition at line 72 of file rludpsocket.cpp.
00073 { 00074 debug = _debug; 00075 if(debug) ::printf("rlUdpSocket() constructor\n"); 00076 rlwsa(); // init sockets on windows 00077 readflag = writeflag = 0; 00078 s = socket(AF_INET,SOCK_DGRAM,0); 00079 if(s < 0) 00080 { 00081 s = -1; 00082 ::printf("rlUdpSocket::rlUdpSocket could not get socket\n"); 00083 } 00084 }
| rlUdpSocket::~rlUdpSocket | ( | ) | [virtual] |
Definition at line 86 of file rludpsocket.cpp.
| int rlUdpSocket::bind | ( | int | port | ) |
return > 0 -> socket else error
Definition at line 122 of file rludpsocket.cpp.
00123 { 00124 if(port < 0 || port >= 256*256) return -1; 00125 00126 memset(&address,0,sizeof(address)); 00127 address.sin_family = AF_INET; 00128 address.sin_port = htons((short) port); 00129 address.sin_addr.s_addr = htonl(INADDR_ANY); 00130 // bind socket 00131 if(::bind(s, (sockaddr *) &address, sizeof(address)) < 0) 00132 { 00133 ::printf("rlUdpSocket::setAdr() bind() failed port=%d\n", port); 00134 return -1; 00135 } 00136 return 0; 00137 }
| int rlUdpSocket::printf | ( | rlIpAdr * | dest, | |
| const char * | format, | |||
| ... | ||||
| ) |
return >= 0 -> number of bytes written else error
Definition at line 218 of file rludpsocket.cpp.
00219 { 00220 int ret; 00221 char message[rl_PRINTF_LENGTH]; // should be big enough 00222 00223 va_list ap; 00224 va_start(ap,format); 00225 ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap); 00226 va_end(ap); 00227 if(ret < 0) return ret; 00228 return sendto(message,strlen(message)+1,dest); 00229 }
| int rlUdpSocket::recvfrom | ( | void * | buf, | |
| int | maxlen, | |||
| rlIpAdr * | source, | |||
| int | timeout = -1 | |||
| ) |
return > 0 -> number of bytes read return == 0 -> timeout else error
Definition at line 160 of file rludpsocket.cpp.
00161 { 00162 int ret, len; 00163 00164 if(timeout >= 0) 00165 { 00166 ret = select(timeout); 00167 if(ret != 1) return -1; // timeout 00168 } 00169 if(debug) ::printf("rlUdpSocket()::recvfrom() ...\n"); 00170 len = sizeof(source->address); 00171 #ifdef RLWIN32 00172 ret = ::recvfrom(s, (char *) buf, maxlen, readflag, 00173 (struct sockaddr *) &source->address, (int FAR *) &len); 00174 #endif 00175 #ifdef RLUNIX 00176 ret = ::recvfrom(s, buf, maxlen, readflag, 00177 (struct sockaddr *) &source->address, (socklen_t *) &len); 00178 #endif 00179 #ifdef __VMS 00180 ret = ::recvfrom(s, buf, maxlen, readflag, 00181 (struct sockaddr *) &source->address, (size_t *) &len); 00182 #endif 00183 if(ret < 0) 00184 { 00185 ::printf("ERROR: rlUdpSocket::read()\n"); 00186 return -2; 00187 } 00188 if(debug) 00189 { 00190 unsigned char *cbuf = (unsigned char *) buf; 00191 ::printf("rlUdpSocket()::recvfrom() ret=%d data=[0x%x",ret,cbuf[0]); 00192 for(int i=1; i<ret; i++) ::printf(",0x%x",cbuf[i]); 00193 ::printf("]\n"); 00194 } 00195 return ret; 00196 }
| int rlUdpSocket::select | ( | int | timeout | ) |
return == 0 -> timeout
Definition at line 139 of file rludpsocket.cpp.
00140 { 00141 if(timeout < 0) return -1; 00142 struct timeval timout; 00143 fd_set wset,rset,eset; 00144 int ret,maxfdp1; 00145 00146 /* setup sockets to read */ 00147 maxfdp1 = s+1; 00148 FD_ZERO(&rset); 00149 FD_SET (s,&rset); 00150 FD_ZERO(&wset); 00151 FD_ZERO(&eset); 00152 timout.tv_sec = timeout / 1000; 00153 timout.tv_usec = (timeout % 1000) * 1000; 00154 00155 ret = ::select(maxfdp1,&rset,&wset,&eset,&timout); 00156 if(ret == 0) return 0; /* timeout */ 00157 return 1; 00158 }
| int rlUdpSocket::sendto | ( | const void * | buf, | |
| int | len, | |||
| rlIpAdr * | dest | |||
| ) |
return >= 0 -> number of bytes written else error
Definition at line 198 of file rludpsocket.cpp.
00199 { 00200 #ifdef RLWIN32 00201 int ret = ::sendto(s, (const char *) buf, len, writeflag, 00202 (struct sockaddr *) &dest->address, sizeof(struct sockaddr_in)); 00203 #else 00204 int ret = ::sendto(s, buf, len, writeflag, 00205 (struct sockaddr *) &dest->address, sizeof(struct sockaddr_in)); 00206 #endif 00207 if(ret < 0) ::printf("ERROR: rlUdpSocket::sendto()\n"); 00208 if(debug) 00209 { 00210 unsigned char *cbuf = (unsigned char *) buf; 00211 ::printf("rlUdpSocket()::sendto() ret=%d data=[0x%x",ret,cbuf[0]); 00212 for(int i=1; i<ret; i++) ::printf(",0x%x",cbuf[i]); 00213 ::printf("]\n"); 00214 } 00215 return ret; 00216 }
| int rlUdpSocket::setSockopt | ( | int | level, | |
| int | optname, | |||
| void * | optval, | |||
| int | optlen | |||
| ) |
setsocketopt with full control
Definition at line 110 of file rludpsocket.cpp.
| int rlUdpSocket::setSockopt | ( | int | opt | ) |
setsocketopt for SOL_SOCKET level
Definition at line 98 of file rludpsocket.cpp.
struct sockaddr_in rlUdpSocket::address [private] |
Definition at line 97 of file rludpsocket.h.
Reimplemented in rlEIBnetIP.
Definition at line 94 of file rludpsocket.h.
Definition at line 94 of file rludpsocket.h.
int rlUdpSocket::s [private] |
Definition at line 98 of file rludpsocket.h.
Definition at line 94 of file rludpsocket.h.
1.6.3