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 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 General Public License for more
21 You should have received a copy of the GNU 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 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 --------------------------------------------------------------------------------
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 #include "FGfdmSocket.h"
42 static const char *IdSrc = "$Id$";
43 static const char *IdHdr = ID_FDMSOCKET;
45 extern short debug_lvl;
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 FGfdmSocket::FGfdmSocket(string address, int port)
55 #if defined(__BORLANDC__) || defined(_MSC_VER)
57 int PASCAL FAR wsaReturnCode;
58 wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
59 if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
60 else cout << "Winsock DLL not initialized ..." << endl;
63 if (address.find_first_not_of("0123456789.",0) != address.npos) {
64 if ((host = gethostbyname(address.c_str())) == NULL) {
65 cout << "Could not get host net address by name..." << endl;
68 if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
69 cout << "Could not get host net address by number..." << endl;
74 cout << "Got host net address..." << endl;
75 sckt = socket(AF_INET, SOCK_STREAM, 0);
77 if (sckt >= 0) { // successful
78 memset(&scktName, 0, sizeof(struct sockaddr_in));
79 scktName.sin_family = AF_INET;
80 scktName.sin_port = htons(port);
81 memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
82 int len = sizeof(struct sockaddr_in);
83 if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) { // successful
84 cout << "Successfully connected to socket ..." << endl;
85 } else { // unsuccessful
86 cout << "Could not connect to socket ..." << endl;
88 } else { // unsuccessful
89 cout << "Could not create socket for FDM, error = " << errno << endl;
93 if (debug_lvl & 2) cout << "Instantiated: FGfdmSocket" << endl;
96 FGfdmSocket::~FGfdmSocket()
99 if (sckt) shutdown(sckt,2);
107 void FGfdmSocket::Clear(void)
113 void FGfdmSocket::Append(const char* item)
115 if (size == 0) buffer += string(item);
116 else buffer += string(",") + string(item);
120 void FGfdmSocket::Append(float item)
124 sprintf(s,"%12.7f\0",item);
126 if (size == 0) buffer += string(s);
127 else buffer += string(",") + string(s);
131 void FGfdmSocket::Append(long item)
135 sprintf(s,"%12d\0",item);
137 if (size == 0) buffer += string(s);
138 else buffer += string(",") + string(s);
142 void FGfdmSocket::Send(void)
144 buffer += string("\n");
145 if ((send(sckt,buffer.c_str(),buffer.size(),0)) <= 0) {