Hej.
Jeg har lavet et program der kan logge nogle data ned.
[main.cpp]
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "log.h"
using namespace std;
int main(int argc, char ** argv)
{
unsigned short port = 1234;
int sock, bytesRead;
char recvBuf[1024];
struct sockaddr_in addr;
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sock == 0)
{
cout << "Kunne ikke oprette socket!" << endl;
return 0;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr= INADDR_ANY;
if(bind(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) != 0)
{
cout << "Kunne ikke binde socket!" << endl;
close(sock);
return 0;
}
logging::beginLog("identifa.xml");
while(true)
{
socklen_t fromLen = sizeof(sockaddr_in);
bytesRead = recvfrom(sock, recvBuf, 1024, 0, (struct sockaddr*)&addr, &fromLen);
string IP = inet_ntoa(addr.sin_addr);
cout << "[" << IP << "] ";
cout.write(recvBuf, bytesRead);
cout << endl;
char sendBuf[1024] = "Ingen funktioner er implementeret";
sendto(sock, sendBuf, strlen(sendBuf), 0, (struct sockaddr*)&addr, fromLen);
logging::dataLog(IP, recvBuf, sendBuf);
}
logging::endLog();
close(sock);
return 0;
}
[log.cpp]
#include "log.h"
int logging::beginLog(const char *OutputFile)
{
logging::OutFile = OutputFile;
std::ofstream out(logging::OutFile);
out << "<?xml version='1.0' encoding='iso-8889-1'?>" << std::endl;
out << "<log>" << std::endl;
out.close();
}
int logging::endLog()
{
std::ofstream out(logging::OutFile);
out << "</log>" << std::endl;
out.close();
}
int logging::dataLog(std::string &adr, const char* Request, const char* Answer)
{
std::ofstream out(logging::OutFile);
out << " <event>" << std::endl;
out << " <ip>" << adr << "</ip>" << std::endl;
out << " <request>" << Request << "</request>" << std::endl;
out << " <answer>" << Answer << "</answer>" << std::endl;
out << " </event>" << std::endl;
out.close();
}
[log.h]
#if !defined(LOG_H)
#define LOG_H
#include <iostream>
#include <string>
#include <fstream>
namespace logging
{
const char* OutFile;
int beginLog(const char* OutputFile);
int endLog();
int dataLog(std::string &adr, const char* Request, const char* Answer);
}
#endif
Jeg får følgende fejl:
/tmp/ccBe3jpS.o (.bss+0x0): multiple defination af 'logging:
uTFile'
/tmp/ccGaJSvN.o(.bss+0x0): first defined here
collect2: ld returned 1 exit status