2 * \file sg_socket_udp.hxx
3 * UDP Socket I/O routines.
6 // Written by Curtis Olson, started November 2001.
8 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #ifndef _SG_SOCKET_UDP_HXX
28 #define _SG_SOCKET_UDP_HXX
31 #include <simgear/compiler.h>
35 #include <simgear/math/sg_types.hxx>
36 #include <simgear/io/iochannel.hxx>
37 #include <simgear/io/raw_socket.hxx>
40 * A UDP socket I/O class based on SGIOChannel and plib/net.
42 class SGSocketUDP : public SGIOChannel {
51 char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
54 short unsigned int port;
59 * Create an instance of SGSocketUDP.
61 * When calling the constructor you need to provide a host name, and a
62 * port number. The convention used by the
63 * SGSocketUDP class is that the server side listens and the client
64 * side sends. For a server socket, the host name should be
65 * empty. For a server, the port number is optional, if you do not
66 * specify a port, the system will assign one. For a client
67 * socket, you need to specify both a destination host and
70 * UDP sockets are a lower level protocol than TCP sockets and are
71 * "connectionless" in the sense that either client or server can
72 * exist, or not exist, startup, quit, etc. in any order and
73 * whenever both ends are alive, the communication succeeds. With
74 * UDP sockets, the server end just sits and listens for incoming
75 * packets from anywhere. The client end sends it's message and
76 * forgets about it. It doesn't care if there isn't even a server
77 * out there listening and all the packets are getting
78 * lost. Although systems/networks usually do a pretty good job
79 * (statistically) of getting your UDP packets to their
80 * destination, there is no guarantee that any particular packet
81 * will make it. But, because of this low level implementation and
82 * lack of error checking, UDP packets are much faster and
83 * efficient. UDP packets are good for sending positional
84 * information to synchronize two applications. In this case, you
85 * want the information to arrive as quickly as possible, and if
86 * you lose a packet, you'd rather get new updated information
87 * rather than have the system waste time resending a packet that
88 * is becoming older and older with every retry.
89 * @param host name of host if direction is SG_IO_OUT or SG_IO_BI
90 * @param port port number if we care to choose one.
91 * @param style specify "udp" or "tcp" */
92 SGSocketUDP( const std::string& host, const std::string& port );
97 // If specified as a server (in direction for now) open the master
98 // listening socket. If specified as a client (out direction),
99 // open a connection to a server.
100 bool open( const SGProtocolDir d );
102 // read data from socket
103 int read( char *buf, int length );
105 // read data from socket
106 int readline( char *buf, int length );
108 // write data to a socket
109 int write( const char *buf, const int length );
111 // write null terminated string to a socket
112 int writestring( const char *str );
118 * Set blocking true or false
119 * @return success/failure
121 bool setBlocking( bool value );
123 /** @return the remote host name */
124 inline std::string get_hostname() const { return hostname; }
126 /** @return the port number (in string form) */
127 inline std::string get_port_str() const { return port_str; }
131 #endif // _SG_SOCKET_UDP_HXX