I am having an odd error that I have not come up across before. My code compiles but doesn't link into an executable, whining about multiple definitions of the functions in the Client class. What am I doing wrong?
GeSHi © 2004, Nigel McNie
- // main.cpp
- #include <iostream>
- #include "Client.cpp"
- int main(int argc, char* argv[])
- {
- std::cout<<"Entry point\n";
- Client ircc;
- return 0;
- }
- // Socket.cpp
- #ifndef Socket_class
- #define Socket_class
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <string>
- #include <arpa/inet.h>
- const int MAXHOSTNAME = 200;
- const int MAXCONNECTIONS = 5;
- const int MAXRECV = 500;
- class Socket
- {
- public:
- Socket();
- virtual ~Socket();
- // Server initialization
- bool create();
- bool bind ( const int port );
- bool listen() const;
- bool accept ( Socket& ) const;
- // Client initialization
- bool connect ( const std::string host, const int port );
- // Data Transimission
- bool send ( const std::string ) const;
- int recv ( std::string& ) const;
- void set_non_blocking ( const bool );
- bool is_valid() const { return m_sock != -1; }
- private:
- int m_sock;
- sockaddr_in m_addr;
- };
- // Socket.cpp
- #include <iostream>
- #include <string>
- #include <errno.h>
- #include <fcntl.h>
- #include "Socket.h"
- Socket::Socket() :
- m_sock ( -1 )
- {
- memset ( &m_addr,
- 0,
- sizeof ( m_addr ) );
- }
- Socket::~Socket()
- {
- if ( is_valid() )
- ::close ( m_sock );
- }
- bool Socket::create()
- {
- m_sock = socket ( AF_INET,
- SOCK_STREAM,
- 0 );
- if ( ! is_valid() )
- return false;
- // TIME_WAIT - argh
- int on = 1;
- if ( setsockopt ( m_sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) ) == -1 )
- return false;
- return true;
- }
- bool Socket::bind ( const int port )
- {
- if ( ! is_valid() )
- {
- return false;
- }
- m_addr.sin_family = AF_INET;
- m_addr.sin_addr.s_addr = INADDR_ANY;
- m_addr.sin_port = htons ( port );
- int bind_return = ::bind ( m_sock,
- ( struct sockaddr * ) &m_addr,
- sizeof ( m_addr ) );
- if ( bind_return == -1 )
- {
- return false;
- }
- return true;
- }
- bool Socket::listen() const
- {
- if ( ! is_valid() )
- {
- return false;
- }
- int listen_return = ::listen ( m_sock, MAXCONNECTIONS );
- if ( listen_return == -1 )
- {
- return false;
- }
- return true;
- }
- bool Socket::accept ( Socket& new_socket ) const
- {
- int addr_length = sizeof ( m_addr );
- new_socket.m_sock = ::accept ( m_sock, ( sockaddr * ) &m_addr, ( socklen_t * ) &addr_length );
- if ( new_socket.m_sock <= 0 )
- return false;
- else
- return true;
- }
- bool Socket::send ( const std::string s ) const
- {
- int status = ::send ( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL );
- if ( status == -1 )
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- int Socket::recv ( std::string& s ) const
- {
- char buf [ MAXRECV + 1 ];
- s = "";
- memset ( buf, 0, MAXRECV + 1 );
- int status = ::recv ( m_sock, buf, MAXRECV, 0 );
- if ( status == -1 )
- {
- std::cout << "status == -1 errno == " << errno << " in Socket::recv\n";
- return 0;
- }
- else if ( status == 0 )
- {
- return 0;
- }
- else
- {
- s = buf;
- return status;
- }
- }
- bool Socket::connect ( const std::string host, const int port )
- {
- if ( ! is_valid() ) return false;
- m_addr.sin_family = AF_INET;
- m_addr.sin_port = htons ( port );
- int status = inet_pton ( AF_INET, host.c_str(), &m_addr.sin_addr );
- if ( errno == EAFNOSUPPORT ) return false;
- status = ::connect ( m_sock, ( sockaddr * ) &m_addr, sizeof ( m_addr ) );
- if ( status == 0 )
- return true;
- else
- return false;
- }
- void Socket::set_non_blocking ( const bool b )
- {
- int opts;
- opts = fcntl ( m_sock,
- F_GETFL );
- if ( opts < 0 )
- {
- return;
- }
- if ( b )
- opts = ( opts | O_NONBLOCK );
- else
- opts = ( opts & ~O_NONBLOCK );
- fcntl ( m_sock,
- F_SETFL,opts );
- }
- // client.h
- #ifndef _CLIENT_H_
- #define _CLIENT_H_
- #include "Socket.h"
- class Client : private Socket
- {
- private:
- public:
- Client(int port);
- Client() {};
- virtual ~Client();
- const Client& operator <<(const std::string&) const;
- const Client& operator >>(std::string&) const;
- void accept(Client&);
- };
- // client.cpp
- #include <iostream>
- #include "Client.h"
- Client::Client(int port)
- {
- if(!Socket::create())
- std::cout<<"Could not create socket\n";
- if(!Socket::bind(port) )
- std::cout<<"Could not bind to port.\n";
- if(!Socket::listen())
- std::cout<<"Could not listen to socket.\n";
- }
- Client::~Client()
- {
- }
- const Client& Client::operator <<(const std::string& s) const
- {
- if(!Socket::send(s))
- std::cout<<"Could not write to socket.\n";
- return *this;
- }
- const Client& Client::operator >>(std::string& s) const
- {
- if(!Socket::recv(s))
- std::cout<<"Could not read from socket.\n";
- return *this;
- }
- void Client::accept(Client& sock)
- {
- if (!Socket::accept(sock))
- std::cout<<"Could not accept socket.\n";
- }
Parsed in 0.238 seconds
