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 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49 FGfdmSocket::FGfdmSocket(string address, int port)
53 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
55 int PASCAL FAR wsaReturnCode;
56 wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
57 if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
58 else cout << "Winsock DLL not initialized ..." << endl;
61 if (address.find_first_not_of("0123456789.",0) != address.npos) {
62 if ((host = gethostbyname(address.c_str())) == NULL) {
63 cout << "Could not get host net address by name..." << endl;
66 if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
67 cout << "Could not get host net address by number..." << endl;
72 cout << "Got host net address..." << endl;
73 sckt = socket(AF_INET, SOCK_STREAM, 0);
75 if (sckt >= 0) { // successful
76 memset(&scktName, 0, sizeof(struct sockaddr_in));
77 scktName.sin_family = AF_INET;
78 scktName.sin_port = htons(port);
79 memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
80 int len = sizeof(struct sockaddr_in);
81 if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) { // successful
82 cout << "Successfully connected to socket ..." << endl;
83 } else { // unsuccessful
84 cout << "Could not connect to socket ..." << endl;
86 } else { // unsuccessful
87 cout << "Could not create socket for FDM, error = " << errno << endl;
91 if (debug_lvl & 2) cout << "Instantiated: FGfdmSocket" << endl;
94 FGfdmSocket::~FGfdmSocket()
97 if (sckt) shutdown(sckt,2);
105 void FGfdmSocket::Clear(void)
111 void FGfdmSocket::Append(const char* item)
113 if (size == 0) buffer += string(item);
114 else buffer += string(",") + string(item);
118 void FGfdmSocket::Append(double item)
122 sprintf(s,"%12.7f\0",item);
124 if (size == 0) buffer += string(s);
125 else buffer += string(",") + string(s);
129 void FGfdmSocket::Append(long item)
133 sprintf(s,"%12d\0",item);
135 if (size == 0) buffer += string(s);
136 else buffer += string(",") + string(s);
140 void FGfdmSocket::Send(void)
142 buffer += string("\n");
143 if ((send(sckt,buffer.c_str(),buffer.size(),0)) <= 0) {