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 /*******************************************************************************
43 ************************************ CODE **************************************
44 *******************************************************************************/
46 FGfdmSocket::FGfdmSocket(string address, int port)
50 #ifdef __BORLANDC__ || _MSC_VER
52 int PASCAL FAR wsaReturnCode;
53 wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
54 if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
55 else cout << "Winsock DLL not initialized ..." << endl;
58 if (address.find_first_not_of("0123456789.",0) != address.npos) {
59 if ((host = gethostbyname(address.c_str())) == NULL) {
60 cout << "Could not get host net address by name..." << endl;
63 if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
64 cout << "Could not get host net address by number..." << endl;
69 cout << "Got host net address..." << endl;
70 sckt = socket(AF_INET, SOCK_STREAM, 0);
72 if (sckt >= 0) { // successful
73 memset(&scktName, 0, sizeof(struct sockaddr_in));
74 scktName.sin_family = AF_INET;
75 scktName.sin_port = htons(port);
76 memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
77 int len = sizeof(struct sockaddr_in);
78 if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) { // successful
79 cout << "Successfully connected to socket ..." << endl;
80 } else { // unsuccessful
81 cout << "Could not connect to socket ..." << endl;
83 } else { // unsuccessful
84 cout << "Could not create socket for FDM, error = " << errno << endl;
89 FGfdmSocket::~FGfdmSocket(void)
91 if (sckt) shutdown(sckt,2);
97 void FGfdmSocket::Clear(void)
103 void FGfdmSocket::Append(const char* item)
105 if (size == 0) buffer += string(item);
106 else buffer += string(",") + string(item);
110 void FGfdmSocket::Append(float item)
114 sprintf(s,"%12.7f\0",item);
116 if (size == 0) buffer += string(s);
117 else buffer += string(",") + string(s);
121 void FGfdmSocket::Append(long item)
125 sprintf(s,"%12d\0",item);
127 if (size == 0) buffer += string(s);
128 else buffer += string(",") + string(s);
132 void FGfdmSocket::Send(void)
136 buffer += string("\n");
137 if ((len = send(sckt,buffer.c_str(),buffer.size(),0)) <= 0) {