1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGfdmSocket.cpp
6 Purpose: Encapsulates a socket
7 Called by: FGOutput, et. al.
9 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU Lesser General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 You should have received a copy of the GNU Lesser General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place - Suite 330, Boston, MA 02111-1307, USA.
25 Further information about the GNU Lesser General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class excapsulates a socket for simple data writing
33 --------------------------------------------------------------------------------
35 11/08/07 HDW Added Generic Socket Send
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 #include "FGfdmSocket.h"
45 #include "string_utilities.h"
54 static const char *IdSrc = "$Id$";
55 static const char *IdHdr = ID_FDMSOCKET;
57 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
61 FGfdmSocket::FGfdmSocket(const string& address, int port, int protocol)
66 #if defined(_MSC_VER) || defined(__MINGW32__)
69 wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
70 if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
71 else cout << "Winsock DLL not initialized ..." << endl;
74 if (!is_number(address)) {
75 if ((host = gethostbyname(address.c_str())) == NULL) {
76 cout << "Could not get host net address by name..." << endl;
79 if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
80 cout << "Could not get host net address by number..." << endl;
85 if (protocol == ptUDP) { //use udp protocol
86 sckt = socket(AF_INET, SOCK_DGRAM, 0);
87 cout << "Creating UDP socket on port " << port << endl;
89 else { //use tcp protocol
90 sckt = socket(AF_INET, SOCK_STREAM, 0);
91 cout << "Creating TCP socket on port " << port << endl;
94 if (sckt >= 0) { // successful
95 memset(&scktName, 0, sizeof(struct sockaddr_in));
96 scktName.sin_family = AF_INET;
97 scktName.sin_port = htons(port);
98 memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
99 int len = sizeof(struct sockaddr_in);
100 if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) { // successful
101 cout << "Successfully connected to socket for output ..." << endl;
103 } else { // unsuccessful
104 cout << "Could not connect to socket for output ..." << endl;
106 } else { // unsuccessful
107 cout << "Could not create socket for FDM output, error = " << errno << endl;
113 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 FGfdmSocket::FGfdmSocket(const string& address, int port)
120 #if defined(_MSC_VER) || defined(__MINGW32__)
123 wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
124 if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
125 else cout << "Winsock DLL not initialized ..." << endl;
128 cout << "... Socket Configuration Sanity Check ..." << endl;
129 cout << "Host name... " << address << ", Port... " << port << "." << endl;
130 cout << "Host name... (char) " << address.c_str() << "." << endl;
132 if (!is_number(address)) {
133 if ((host = gethostbyname(address.c_str())) == NULL) {
134 cout << "Could not get host net address by name..." << endl;
137 if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
138 cout << "Could not get host net address by number..." << endl;
143 cout << "Got host net address..." << endl;
144 sckt = socket(AF_INET, SOCK_STREAM, 0);
146 if (sckt >= 0) { // successful
147 memset(&scktName, 0, sizeof(struct sockaddr_in));
148 scktName.sin_family = AF_INET;
149 scktName.sin_port = htons(port);
150 memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
151 int len = sizeof(struct sockaddr_in);
152 if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) { // successful
153 cout << "Successfully connected to socket for output ..." << endl;
155 } else { // unsuccessful
156 cout << "Could not connect to socket for output ..." << endl;
158 } else { // unsuccessful
159 cout << "Could not create socket for FDM output, error = " << errno << endl;
165 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 FGfdmSocket::FGfdmSocket(int port)
170 unsigned long NoBlock = true;
172 #if defined(_MSC_VER) || defined(__MINGW32__)
175 wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
176 if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
177 else cerr << "Winsock DLL not initialized ..." << endl;
180 sckt = socket(AF_INET, SOCK_STREAM, 0);
182 if (sckt >= 0) { // successful
183 memset(&scktName, 0, sizeof(struct sockaddr_in));
184 scktName.sin_family = AF_INET;
185 scktName.sin_port = htons(port);
186 int len = sizeof(struct sockaddr_in);
187 if (bind(sckt, (struct sockaddr*)&scktName, len) == 0) { // successful
188 cout << "Successfully bound to socket for input on port " << port << endl;
189 if (listen(sckt, 5) >= 0) { // successful listen()
190 #if defined(_MSC_VER) || defined(__MINGW32__)
191 ioctlsocket(sckt, FIONBIO, &NoBlock);
192 sckt_in = accept(sckt, (struct sockaddr*)&scktName, &len);
194 ioctl(sckt, FIONBIO, &NoBlock);
195 sckt_in = accept(sckt, (struct sockaddr*)&scktName, (socklen_t*)&len);
198 cerr << "Could not listen ..." << endl;
201 } else { // unsuccessful
202 cerr << "Could not bind to socket for input ..." << endl;
204 } else { // unsuccessful
205 cerr << "Could not create socket for FDM input, error = " << errno << endl;
211 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213 FGfdmSocket::~FGfdmSocket()
215 if (sckt) shutdown(sckt,2);
216 if (sckt_in) shutdown(sckt_in,2);
220 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 string FGfdmSocket::Receive(void)
225 int len = sizeof(struct sockaddr_in);
227 unsigned long NoBlock = true;
228 string data; // todo: should allocate this with a standard size as a
229 // class attribute and pass as a reference?
232 #if defined(_MSC_VER) || defined(__MINGW32__)
233 sckt_in = accept(sckt, (struct sockaddr*)&scktName, &len);
235 sckt_in = accept(sckt, (struct sockaddr*)&scktName, (socklen_t*)&len);
238 #if defined(_MSC_VER) || defined(__MINGW32__)
239 ioctlsocket(sckt_in, FIONBIO,&NoBlock);
241 ioctl(sckt_in, FIONBIO, &NoBlock);
243 send(sckt_in, "Connected to JSBSim server\nJSBSim> ", 35, 0);
248 while ((num_chars = recv(sckt_in, buf, sizeof buf, 0)) > 0) {
249 data.append(buf, num_chars);
252 #if defined(_MSC_VER)
253 // when nothing received and the error isn't "would block"
254 // then assume that the client has closed the socket.
255 if (num_chars == 0) {
256 DWORD err = WSAGetLastError ();
257 if (err != WSAEWOULDBLOCK) {
258 printf ("Socket Closed. back to listening\n");
259 closesocket (sckt_in);
269 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 int FGfdmSocket::Reply(const string& text)
273 int num_chars_sent=0;
276 num_chars_sent = send(sckt_in, text.c_str(), text.size(), 0);
277 send(sckt_in, "JSBSim> ", 8, 0);
279 cerr << "Socket reply must be to a valid socket" << endl;
282 return num_chars_sent;
285 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 void FGfdmSocket::Close(void)
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 void FGfdmSocket::Clear(void)
296 buffer.str(string());
299 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 void FGfdmSocket::Clear(const string& s)
307 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 void FGfdmSocket::Append(const char* item)
311 if (buffer.tellp() > 0) buffer << ',';
315 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 void FGfdmSocket::Append(double item)
319 if (buffer.tellp() > 0) buffer << ',';
320 buffer << std::setw(12) << std::setprecision(7) << item;
323 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325 void FGfdmSocket::Append(long item)
327 if (buffer.tellp() > 0) buffer << ',';
328 buffer << std::setw(12) << item;
331 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333 void FGfdmSocket::Send(void)
336 string str = buffer.str();
337 if ((send(sckt,str.c_str(),str.size(),0)) <= 0) {
342 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 void FGfdmSocket::Send(const char *data, int length)
346 if ((send(sckt,data,length,0)) <= 0) {
351 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 // The bitmasked value choices are as follows:
353 // unset: In this case (the default) JSBSim would only print
354 // out the normally expected messages, essentially echoing
355 // the config files as they are read. If the environment
356 // variable is not set, debug_lvl is set to 1 internally
357 // 0: This requests JSBSim not to output any messages
359 // 1: This value explicity requests the normal JSBSim
361 // 2: This value asks for a message to be printed out when
362 // a class is instantiated
363 // 4: When this value is set, a message is displayed when a
364 // FGModel object executes its Run() method
365 // 8: When this value is set, various runtime state variables
366 // are printed out periodically
367 // 16: When set various parameters are sanity checked and
368 // a message is printed out when they go out of bounds
370 void FGfdmSocket::Debug(int from)
372 if (debug_lvl <= 0) return;
374 if (debug_lvl & 1) { // Standard console startup message output
375 if (from == 0) { // Constructor
378 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
379 if (from == 0) cout << "Instantiated: FGfdmSocket" << endl;
380 if (from == 1) cout << "Destroyed: FGfdmSocket" << endl;
382 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
384 if (debug_lvl & 8 ) { // Runtime state variables
386 if (debug_lvl & 16) { // Sanity checking
388 if (debug_lvl & 64) {
389 if (from == 0) { // Constructor
390 cout << IdSrc << endl;
391 cout << IdHdr << endl;