1 // sg_socket.cxx -- Socket I/O routines
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 # include <simgear_config.h>
27 #include <simgear/compiler.h>
29 #include <simgear/debug/logstream.hxx>
31 #include "sg_socket_udp.hxx"
34 #include <cstdlib> // for atoi
36 SGSocketUDP::SGSocketUDP( const string& host, const string& port ) :
45 SGSocketUDP::~SGSocketUDP() {
49 // If specified as a server (in direction for now) open the master
50 // listening socket. If specified as a client (out direction), open a
51 // connection to a server.
52 bool SGSocketUDP::open( const SGProtocolDir d ) {
55 if ( ! sock.open( false ) ) { // open a UDP socket
56 SG_LOG( SG_IO, SG_ALERT, "error opening socket" );
60 if ( port_str == "" || port_str == "any" ) {
63 port = atoi( port_str.c_str() );
66 // client_connections.clear();
68 if ( get_dir() == SG_IO_IN ) {
72 if ( sock.bind( hostname.c_str(), port ) == -1 ) {
73 SG_LOG( SG_IO, SG_ALERT, "error binding to port" << port_str );
76 } else if ( get_dir() == SG_IO_OUT ) {
80 if ( sock.connect( hostname.c_str(), port ) == -1 ) {
81 SG_LOG( SG_IO, SG_ALERT,
82 "error connecting to " << hostname << port_str );
86 SG_LOG( SG_IO, SG_ALERT,
87 "Error: bidirection mode not available for UDP sockets." );
97 // read data from socket (server)
98 // read a block of data of specified size
99 int SGSocketUDP::read( char *buf, int length ) {
106 if ( (result = sock.recv(buf, SG_IO_MAX_MSG_SIZE, 0)) >= 0 ) {
108 // printf("msg received = %s\n", buf);
115 // read a line of data, length is max size of input buffer
116 int SGSocketUDP::readline( char *buf, int length ) {
121 // cout << "sock = " << sock << endl;
123 char *buf_ptr = save_buf + save_len;
124 int result = sock.recv(buf_ptr, SG_IO_MAX_MSG_SIZE, 0);
125 // printf("msg received = %s\n", buf);
128 // look for the end of line in save_buf
130 for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i );
131 if ( save_buf[i] == '\n' ) {
134 // no end of line yet
135 // cout << "no eol found" << endl;
138 // cout << "line length = " << result << endl;
140 // we found an end of line
142 // copy to external buffer
143 strncpy( buf, save_buf, result );
145 // cout << "sg_socket line = " << buf << endl;
148 for ( i = result; i < save_len; ++i ) {
149 save_buf[ i - result ] = save_buf[i];
157 // write data to socket (client)
158 int SGSocketUDP::write( const char *buf, const int length ) {
163 bool error_condition = false;
165 if ( sock.send( buf, length, 0 ) < 0 ) {
166 SG_LOG( SG_IO, SG_WARN, "Error writing to socket: " << port );
167 error_condition = true;
175 // write null terminated string to socket (server)
176 int SGSocketUDP::writestring( const char *str ) {
181 int length = strlen( str );
182 return write( str, length );
187 bool SGSocketUDP::close() {
198 // configure the socket as non-blocking
199 bool SGSocketUDP::setBlocking( bool value ) {
200 sock.setBlocking( value );