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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #ifndef _SG_SOCKET_UDP_HXX
28 #define _SG_SOCKET_UDP_HXX
32 # error This library requires C++
35 #include <plib/netSocket.h>
37 #include <simgear/compiler.h>
41 #include <simgear/math/sg_types.hxx>
42 #include <simgear/io/iochannel.hxx>
47 * A UDP socket I/O class based on SGIOChannel and plib/net.
49 class SGSocketUDP : public SGIOChannel {
58 char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
61 short unsigned int port;
66 * Create an instance of SGSocketUDP.
68 * When calling the constructor you need to provide a host name, and a
69 * port number. The convention used by the
70 * SGSocketUDP class is that the server side listens and the client
71 * side sends. For a server socket, the host name should be
72 * empty. For a server, the port number is optional, if you do not
73 * specify a port, the system will assign one. For a client
74 * socket, you need to specify both a destination host and
77 * UDP sockets are a lower level protocol than TCP sockets and are
78 * "connectionless" in the sense that either client or server can
79 * exist, or not exist, startup, quit, etc. in any order and
80 * whenever both ends are alive, the communication succeeds. With
81 * UDP sockets, the server end just sits and listens for incoming
82 * packets from anywhere. The client end sends it's message and
83 * forgets about it. It doesn't care if there isn't even a server
84 * out there listening and all the packets are getting
85 * lost. Although systems/networks usually do a pretty good job
86 * (statistically) of getting your UDP packets to their
87 * destination, there is no guarantee that any particular packet
88 * will make it. But, because of this low level implementation and
89 * lack of error checking, UDP packets are much faster and
90 * efficient. UDP packets are good for sending positional
91 * information to synchronize two applications. In this case, you
92 * want the information to arrive as quickly as possible, and if
93 * you lose a packet, you'd rather get new updated information
94 * rather than have the system waste time resending a packet that
95 * is becoming older and older with every retry.
96 * @param host name of host if direction is SG_IO_OUT or SG_IO_BI
97 * @param port port number if we care to choose one.
98 * @param style specify "udp" or "tcp" */
99 SGSocketUDP( const string& host, const string& port );
104 // If specified as a server (in direction for now) open the master
105 // listening socket. If specified as a client (out direction),
106 // open a connection to a server.
107 bool open( const SGProtocolDir d );
109 // read data from socket
110 int read( char *buf, int length );
112 // read data from socket
113 int readline( char *buf, int length );
115 // write data to a socket
116 int write( const char *buf, const int length );
118 // write null terminated string to a socket
119 int writestring( const char *str );
125 * Set blocking true or false
126 * @return success/failure
128 bool setBlocking( bool value );
130 /** @return the remote host name */
131 inline string get_hostname() const { return hostname; }
133 /** @return the port number (in string form) */
134 inline string get_port_str() const { return port_str; }
138 #endif // _SG_SOCKET_UDP_HXX