41 #if defined(_MSC_VER) || defined(__MINGW32__) 50 #include "FGfdmSocket.h" 51 #include "string_utilities.h" 60 IDENT(IdSrc,
"$Id: FGfdmSocket.cpp,v 1.31 2015/03/22 12:19:31 bcoconni Exp $");
61 IDENT(IdHdr,ID_FDMSOCKET);
67 #if defined(_MSC_VER) || defined(__MINGW32__) 68 static bool LoadWinSockDLL(
void)
71 if (WSAStartup(MAKEWORD(1, 1), &wsaData)) {
72 cout <<
"Winsock DLL not initialized ..." << endl;
76 cout <<
"Winsock DLL loaded ..." << endl;
81 FGfdmSocket::FGfdmSocket(
const string& address,
int port,
int protocol)
84 Protocol = (ProtocolType)protocol;
87 #if defined(_MSC_VER) || defined(__MINGW32__) 88 if (!LoadWinSockDLL())
return;
91 if (!is_number(address)) {
92 if ((host = gethostbyname(address.c_str())) == NULL) {
93 cout <<
"Could not get host net address by name..." << endl;
97 ip = inet_addr(address.c_str());
98 if ((host = gethostbyaddr((
char*)&ip, address.size(), PF_INET)) == NULL) {
99 cout <<
"Could not get host net address by number..." << endl;
104 if (protocol == ptUDP) {
105 sckt = socket(AF_INET, SOCK_DGRAM, 0);
106 cout <<
"Creating UDP socket on port " << port << endl;
109 sckt = socket(AF_INET, SOCK_STREAM, 0);
110 cout <<
"Creating TCP socket on port " << port << endl;
114 memset(&scktName, 0,
sizeof(
struct sockaddr_in));
115 scktName.sin_family = AF_INET;
116 scktName.sin_port = htons(port);
117 memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
118 int len =
sizeof(
struct sockaddr_in);
119 if (connect(sckt, (
struct sockaddr*)&scktName, len) == 0) {
120 cout <<
"Successfully connected to socket for output ..." << endl;
123 cout <<
"Could not connect to socket for output ..." << endl;
126 cout <<
"Could not create socket for FDM output, error = " << errno << endl;
135 FGfdmSocket::FGfdmSocket(
int port,
int protocol,
int direction)
139 Protocol = (ProtocolType)protocol;
140 Direction = (DirectionType) direction;
142 #if defined(_MSC_VER) || defined(__MINGW32__) 143 if (!LoadWinSockDLL())
return;
146 if (Protocol == ptUDP) {
147 sckt = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
148 #if defined(_MSC_VER) || defined(__MINGW32__) 150 ioctlsocket(sckt, FIONBIO, &NonBlock);
152 fcntl(sckt, F_SETFL, O_NONBLOCK);
154 cout <<
"Creating UDP input socket on port " << port << endl;
158 memset(&scktName, 0,
sizeof(
struct sockaddr_in));
159 scktName.sin_family = AF_INET;
160 scktName.sin_port = htons(port);
161 scktName.sin_addr.s_addr = htonl(INADDR_ANY);
162 int len =
sizeof(
struct sockaddr_in);
163 if (bind(sckt, (
struct sockaddr*)&scktName, len) != -1) {
164 cout <<
"Successfully bound to UDP input socket on port " << port << endl <<endl;
167 cout <<
"Could not bind to UDP input socket, error = " << errno << endl;
170 cout <<
"Could not create socket for UDP input, error = " << errno << endl;
179 FGfdmSocket::FGfdmSocket(
const string& address,
int port)
185 #if defined(_MSC_VER) || defined(__MINGW32__) 186 if (!LoadWinSockDLL())
return;
189 cout <<
"... Socket Configuration Sanity Check ..." << endl;
190 cout <<
"Host name... " << address <<
", Port... " << port <<
"." << endl;
191 cout <<
"Host name... (char) " << address.c_str() <<
"." << endl;
193 if (!is_number(address)) {
194 if ((host = gethostbyname(address.c_str())) == NULL) {
195 cout <<
"Could not get host net address by name..." << endl;
198 if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
199 cout <<
"Could not get host net address by number..." << endl;
204 cout <<
"Got host net address..." << endl;
205 sckt = socket(AF_INET, SOCK_STREAM, 0);
208 memset(&scktName, 0,
sizeof(
struct sockaddr_in));
209 scktName.sin_family = AF_INET;
210 scktName.sin_port = htons(port);
211 memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
212 int len =
sizeof(
struct sockaddr_in);
213 if (connect(sckt, (
struct sockaddr*)&scktName, len) == 0) {
214 cout <<
"Successfully connected to socket for output ..." << endl;
217 cout <<
"Could not connect to socket for output ..." << endl;
220 cout <<
"Could not create socket for FDM output, error = " << errno << endl;
228 FGfdmSocket::FGfdmSocket(
int port)
231 unsigned long NoBlock =
true;
234 #if defined(_MSC_VER) || defined(__MINGW32__) 235 if (!LoadWinSockDLL())
return;
238 sckt = socket(AF_INET, SOCK_STREAM, 0);
241 memset(&scktName, 0,
sizeof(
struct sockaddr_in));
242 scktName.sin_family = AF_INET;
243 scktName.sin_port = htons(port);
244 int len =
sizeof(
struct sockaddr_in);
245 if (bind(sckt, (
struct sockaddr*)&scktName, len) == 0) {
246 cout <<
"Successfully bound to socket for input on port " << port << endl;
247 if (listen(sckt, 5) >= 0) {
248 #if defined(_MSC_VER) || defined(__MINGW32__) 249 ioctlsocket(sckt, FIONBIO, &NoBlock);
250 sckt_in = accept(sckt, (
struct sockaddr*)&scktName, &len);
252 ioctl(sckt, FIONBIO, &NoBlock);
253 sckt_in = accept(sckt, (
struct sockaddr*)&scktName, (socklen_t*)&len);
256 cerr <<
"Could not listen ..." << endl;
260 cerr <<
"Could not bind to socket for input ..." << endl;
263 cerr <<
"Could not create socket for FDM input, error = " << errno << endl;
271 FGfdmSocket::~FGfdmSocket()
273 if (sckt) shutdown(sckt,2);
274 if (sckt_in) shutdown(sckt_in,2);
280 string FGfdmSocket::Receive(
void)
283 int len =
sizeof(
struct sockaddr_in);
285 unsigned long NoBlock =
true;
289 if (sckt_in <= 0 && Protocol == ptTCP) {
290 #if defined(_MSC_VER) || defined(__MINGW32__) 291 sckt_in = accept(sckt, (
struct sockaddr*)&scktName, &len);
293 sckt_in = accept(sckt, (
struct sockaddr*)&scktName, (socklen_t*)&len);
296 #if defined(_MSC_VER) || defined(__MINGW32__) 297 ioctlsocket(sckt_in, FIONBIO,&NoBlock);
299 ioctl(sckt_in, FIONBIO, &NoBlock);
301 send(sckt_in,
"Connected to JSBSim server\nJSBSim> ", 35, 0);
306 while ((num_chars = recv(sckt_in, buf,
sizeof buf, 0)) > 0) {
307 data.append(buf, num_chars);
310 #if defined(_MSC_VER) 313 if (num_chars == 0) {
314 DWORD err = WSAGetLastError ();
315 if (err != WSAEWOULDBLOCK) {
316 printf (
"Socket Closed. back to listening\n");
317 closesocket (sckt_in);
325 if (sckt >= 0 && Protocol == ptUDP) {
326 struct sockaddr addr;
327 socklen_t fromlen =
sizeof addr;
328 num_chars = recvfrom(sckt, buf,
sizeof buf, 0, (
struct sockaddr*)&addr, &fromlen);
329 if (num_chars != -1) data.append(buf, num_chars);
337 int FGfdmSocket::Reply(
const string& text)
339 int num_chars_sent=0;
342 num_chars_sent = send(sckt_in, text.c_str(), text.size(), 0);
343 send(sckt_in,
"JSBSim> ", 8, 0);
345 cerr <<
"Socket reply must be to a valid socket" << endl;
348 return num_chars_sent;
353 void FGfdmSocket::Close(
void)
360 void FGfdmSocket::Clear(
void)
362 buffer.str(
string());
367 void FGfdmSocket::Clear(
const string& s)
375 void FGfdmSocket::Append(
const char* item)
377 if (buffer.tellp() > 0) buffer <<
',';
383 void FGfdmSocket::Append(
double item)
385 if (buffer.tellp() > 0) buffer <<
',';
386 buffer << std::setw(12) << std::setprecision(7) << item;
391 void FGfdmSocket::Append(
long item)
393 if (buffer.tellp() > 0) buffer <<
',';
394 buffer << std::setw(12) << item;
399 void FGfdmSocket::Send(
void)
402 string str = buffer.str();
403 if ((send(sckt,str.c_str(),str.size(),0)) <= 0) {
410 void FGfdmSocket::Send(
const char *data,
int length)
412 if ((send(sckt,data,length,0)) <= 0) {
436 void FGfdmSocket::Debug(
int from)
438 if (debug_lvl <= 0)
return;
444 if (debug_lvl & 2 ) {
445 if (from == 0) cout <<
"Instantiated: FGfdmSocket" << endl;
446 if (from == 1) cout <<
"Destroyed: FGfdmSocket" << endl;
448 if (debug_lvl & 4 ) {
450 if (debug_lvl & 8 ) {
452 if (debug_lvl & 16) {
454 if (debug_lvl & 64) {
456 cout << IdSrc << endl;
457 cout << IdHdr << endl;