A UDP socket I/O class based on SGIOChannel and plib/net. More...
#include <sg_socket_udp.hxx>
Public Member Functions | |
SGSocketUDP (const string &host, const string &port) | |
Create an instance of SGSocketUDP. | |
~SGSocketUDP () | |
Destructor. | |
bool | open (const SGProtocolDir d) |
Open a channel. | |
int | read (char *buf, int length) |
The read() method is modeled after the read() Unix system call. | |
int | readline (char *buf, int length) |
The readline() method is similar to read() except that it will stop at the first end of line encountered in the input buffer. | |
int | write (const char *buf, const int length) |
The write() method is modeled after the write() Unix system call and is analogous to the read() method. | |
int | writestring (const char *str) |
The writestring() method is a simple wrapper that will calculate the length of a null terminated character array and write it to the output channel. | |
bool | close () |
The close() method is modeled after the close() Unix system call and will close an open device. | |
bool | setBlocking (bool value) |
Set blocking true or false. | |
string | get_hostname () const |
string | get_port_str () const |
A UDP socket I/O class based on SGIOChannel and plib/net.
Definition at line 49 of file sg_socket_udp.hxx.
SGSocketUDP::SGSocketUDP | ( | const string & | host, | |
const string & | port | |||
) |
Create an instance of SGSocketUDP.
When calling the constructor you need to provide a host name, and a port number. The convention used by the SGSocketUDP class is that the server side listens and the client side sends. For a server socket, the host name should be empty. For a server, the port number is optional, if you do not specify a port, the system will assign one. For a client socket, you need to specify both a destination host and destination port.
UDP sockets are a lower level protocol than TCP sockets and are "connectionless" in the sense that either client or server can exist, or not exist, startup, quit, etc. in any order and whenever both ends are alive, the communication succeeds. With UDP sockets, the server end just sits and listens for incoming packets from anywhere. The client end sends it's message and forgets about it. It doesn't care if there isn't even a server out there listening and all the packets are getting lost. Although systems/networks usually do a pretty good job (statistically) of getting your UDP packets to their destination, there is no guarantee that any particular packet will make it. But, because of this low level implementation and lack of error checking, UDP packets are much faster and efficient. UDP packets are good for sending positional information to synchronize two applications. In this case, you want the information to arrive as quickly as possible, and if you lose a packet, you'd rather get new updated information rather than have the system waste time resending a packet that is becoming older and older with every retry.
host | name of host if direction is SG_IO_OUT or SG_IO_BI | |
port | port number if we care to choose one. | |
style | specify "udp" or "tcp" |
Definition at line 38 of file sg_socket_udp.cxx.
bool SGSocketUDP::close | ( | ) | [virtual] |
The close() method is modeled after the close() Unix system call and will close an open device.
You should call this method when you are done using your IO class, before it is destructed.
Reimplemented from SGIOChannel.
Definition at line 189 of file sg_socket_udp.cxx.
string SGSocketUDP::get_hostname | ( | ) | const [inline] |
Definition at line 131 of file sg_socket_udp.hxx.
string SGSocketUDP::get_port_str | ( | ) | const [inline] |
Definition at line 134 of file sg_socket_udp.hxx.
bool SGSocketUDP::open | ( | const SGProtocolDir | d | ) | [virtual] |
Open a channel.
d | channel communication "direction" Direction can be one of:
|
Reimplemented from SGIOChannel.
Definition at line 54 of file sg_socket_udp.cxx.
int SGSocketUDP::read | ( | char * | buf, | |
int | length | |||
) | [virtual] |
The read() method is modeled after the read() Unix system call.
You must provide a pointer to a character buffer that has enough allocated space for your potential read. You can also specify the maximum number of bytes allowed for this particular read. The actual number of bytes read is returned. You are responsible to ensure that the size of buf is large enough to accomodate your input message
buf | a char pointer to your input buffer | |
length | max number of bytes to read |
Reimplemented from SGIOChannel.
Definition at line 101 of file sg_socket_udp.cxx.
int SGSocketUDP::readline | ( | char * | buf, | |
int | length | |||
) | [virtual] |
The readline() method is similar to read() except that it will stop at the first end of line encountered in the input buffer.
buf | a char pointer to your input buffer | |
length | max number of bytes to read |
Reimplemented from SGIOChannel.
Definition at line 118 of file sg_socket_udp.cxx.
bool SGSocketUDP::setBlocking | ( | bool | value | ) |
Set blocking true or false.
Definition at line 201 of file sg_socket_udp.cxx.
int SGSocketUDP::write | ( | const char * | buf, | |
const int | length | |||
) | [virtual] |
The write() method is modeled after the write() Unix system call and is analogous to the read() method.
You provide a pointer to a buffer of data, and then length of that data to be written out. The number of bytes written is returned.
buf | a char pointer to your output buffer | |
length | number of bytes to write |
Reimplemented from SGIOChannel.
Definition at line 160 of file sg_socket_udp.cxx.
int SGSocketUDP::writestring | ( | const char * | str | ) | [virtual] |
The writestring() method is a simple wrapper that will calculate the length of a null terminated character array and write it to the output channel.
buf | a char pointer to your output buffer |
Reimplemented from SGIOChannel.
Definition at line 178 of file sg_socket_udp.cxx.