1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGfdmSocket.cpp
6 Purpose: Encapsulates a socket
7 Called by: FGOutput, et. al.
9 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41 #include "FGfdmSocket.h"
46 static const char *IdSrc = "$Id$";
47 static const char *IdHdr = ID_FDMSOCKET;
49 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53 FGfdmSocket::FGfdmSocket(string address, int port, int protocol)
55 sckt = sckt_in = size = 0;
58 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
61 wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
62 if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
63 else cout << "Winsock DLL not initialized ..." << endl;
66 if (address.find_first_not_of("0123456789.",0) != address.npos) {
67 if ((host = gethostbyname(address.c_str())) == NULL) {
68 cout << "Could not get host net address by name..." << endl;
71 if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
72 cout << "Could not get host net address by number..." << endl;
77 if (protocol == ptUDP) { //use udp protocol
78 sckt = socket(AF_INET, SOCK_DGRAM, 0);
79 cout << "Creating UDP socket on port " << port << endl;
81 else { //use tcp protocol
82 sckt = socket(AF_INET, SOCK_STREAM, 0);
83 cout << "Creating TCP socket on port " << port << endl;
86 if (sckt >= 0) { // successful
87 memset(&scktName, 0, sizeof(struct sockaddr_in));
88 scktName.sin_family = AF_INET;
89 scktName.sin_port = htons(port);
90 memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
91 int len = sizeof(struct sockaddr_in);
92 if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) { // successful
93 cout << "Successfully connected to socket for output ..." << endl;
95 } else { // unsuccessful
96 cout << "Could not connect to socket for output ..." << endl;
98 } else { // unsuccessful
99 cout << "Could not create socket for FDM output, error = " << errno << endl;
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 FGfdmSocket::FGfdmSocket(string address, int port)
109 sckt = sckt_in = size = 0;
112 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
115 wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
116 if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
117 else cout << "Winsock DLL not initialized ..." << endl;
120 cout << "... Socket Configuration Sanity Check ..." << endl;
121 cout << "Host name... " << address << ", Port... " << port << "." << endl;
122 cout << "Host name... (char) " << address.c_str() << "." << endl;
124 if (address.find_first_not_of("0123456789.",0) != address.npos) {
125 if ((host = gethostbyname(address.c_str())) == NULL) {
126 cout << "Could not get host net address by name..." << endl;
129 if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
130 cout << "Could not get host net address by number..." << endl;
135 cout << "Got host net address..." << endl;
136 sckt = socket(AF_INET, SOCK_STREAM, 0);
138 if (sckt >= 0) { // successful
139 memset(&scktName, 0, sizeof(struct sockaddr_in));
140 scktName.sin_family = AF_INET;
141 scktName.sin_port = htons(port);
142 memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
143 int len = sizeof(struct sockaddr_in);
144 if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) { // successful
145 cout << "Successfully connected to socket for output ..." << endl;
147 } else { // unsuccessful
148 cout << "Could not connect to socket for output ..." << endl;
150 } else { // unsuccessful
151 cout << "Could not create socket for FDM output, error = " << errno << endl;
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 FGfdmSocket::FGfdmSocket(int port)
163 unsigned long NoBlock = true;
165 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
168 wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
169 if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
170 else cerr << "Winsock DLL not initialized ..." << endl;
173 sckt = socket(AF_INET, SOCK_STREAM, 0);
175 if (sckt >= 0) { // successful
176 memset(&scktName, 0, sizeof(struct sockaddr_in));
177 scktName.sin_family = AF_INET;
178 scktName.sin_port = htons(port);
179 int len = sizeof(struct sockaddr_in);
180 if (bind(sckt, (struct sockaddr*)&scktName, len) == 0) { // successful
181 cout << "Successfully bound to socket for input on port " << port << endl;
182 if (listen(sckt, 5) >= 0) { // successful listen()
183 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
184 ioctlsocket(sckt, FIONBIO, &NoBlock);
185 sckt_in = accept(sckt, (struct sockaddr*)&scktName, &len);
187 ioctl(sckt, FIONBIO, &NoBlock);
188 sckt_in = accept(sckt, (struct sockaddr*)&scktName, (socklen_t*)&len);
191 cerr << "Could not listen ..." << endl;
194 } else { // unsuccessful
195 cerr << "Could not bind to socket for input ..." << endl;
197 } else { // unsuccessful
198 cerr << "Could not create socket for FDM input, error = " << errno << endl;
204 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206 FGfdmSocket::~FGfdmSocket()
208 if (sckt) shutdown(sckt,2);
209 if (sckt_in) shutdown(sckt_in,2);
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 string FGfdmSocket::Receive(void)
222 int len = sizeof(struct sockaddr_in);
225 unsigned long NoBlock = true;
226 string data = ""; // todo: should allocate this with a standard size as a
227 // class attribute and pass as a reference?
230 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
231 sckt_in = accept(sckt, (struct sockaddr*)&scktName, &len);
233 sckt_in = accept(sckt, (struct sockaddr*)&scktName, (socklen_t*)&len);
236 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
237 ioctlsocket(sckt_in, FIONBIO,&NoBlock);
239 ioctl(sckt_in, FIONBIO, &NoBlock);
241 send(sckt_in, "Connected to JSBSim server\nJSBSim> ", 35, 0);
246 while ((num_chars = recv(sckt_in, buf, 1024, 0)) > 0) {
247 data += string(buf).substr(0,num_chars);
248 total_chars += num_chars;
251 #if defined(_MSC_VER)
252 // when nothing received and the error isn't "would block"
253 // then assume that the client has closed the socket.
254 if (num_chars == 0) {
255 DWORD err = WSAGetLastError ();
256 if (err != WSAEWOULDBLOCK) {
257 printf ("Socket Closed. back to listening\n");
258 closesocket (sckt_in);
265 return data.substr(0, total_chars);
268 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 int FGfdmSocket::Reply(string text)
272 int num_chars_sent=0;
275 num_chars_sent = send(sckt_in, text.c_str(), text.size(), 0);
276 send(sckt_in, "JSBSim> ", 8, 0);
278 cerr << "Socket reply must be to a valid socket" << endl;
281 return num_chars_sent;
284 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 void FGfdmSocket::Close(void)
291 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 void FGfdmSocket::Clear(void)
299 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 void FGfdmSocket::Clear(string s)
304 size = buffer.size();
307 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 void FGfdmSocket::Append(const char* item)
311 if (size == 0) buffer += string(item);
312 else buffer += string(",") + string(item);
316 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318 void FGfdmSocket::Append(double item)
322 sprintf(s,"%12.7f",item);
324 if (size == 0) buffer += string(s);
325 else buffer += string(",") + string(s);
329 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331 void FGfdmSocket::Append(long item)
335 sprintf(s,"%12ld",item);
337 if (size == 0) buffer += string(s);
338 else buffer += string(",") + string(s);
342 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 void FGfdmSocket::Send(void)
346 buffer += string("\n");
347 if ((send(sckt,buffer.c_str(),buffer.size(),0)) <= 0) {
353 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
355 void FGfdmSocket::Send(char *data, int length)
357 if ((send(sckt,data,length,0)) <= 0) {
363 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364 // The bitmasked value choices are as follows:
365 // unset: In this case (the default) JSBSim would only print
366 // out the normally expected messages, essentially echoing
367 // the config files as they are read. If the environment
368 // variable is not set, debug_lvl is set to 1 internally
369 // 0: This requests JSBSim not to output any messages
371 // 1: This value explicity requests the normal JSBSim
373 // 2: This value asks for a message to be printed out when
374 // a class is instantiated
375 // 4: When this value is set, a message is displayed when a
376 // FGModel object executes its Run() method
377 // 8: When this value is set, various runtime state variables
378 // are printed out periodically
379 // 16: When set various parameters are sanity checked and
380 // a message is printed out when they go out of bounds
382 void FGfdmSocket::Debug(int from)
384 if (debug_lvl <= 0) return;
386 if (debug_lvl & 1) { // Standard console startup message output
387 if (from == 0) { // Constructor
390 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
391 if (from == 0) cout << "Instantiated: FGfdmSocket" << endl;
392 if (from == 1) cout << "Destroyed: FGfdmSocket" << endl;
394 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
396 if (debug_lvl & 8 ) { // Runtime state variables
398 if (debug_lvl & 16) { // Sanity checking
400 if (debug_lvl & 64) {
401 if (from == 0) { // Constructor
402 cout << IdSrc << endl;
403 cout << IdHdr << endl;