Image

Imagelavandergirl wrote in Imagecpp

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
  1. // main.cpp

  2. #include <iostream>

  3. #include "Client.cpp"

  4.  

  5. int main(int argc, char* argv[])


  6. {

  7.   std::cout<<"Entry point\n";

  8.   Client ircc;

  9.  

  10.   return 0;


  11. }

  12.  

  13. // Socket.cpp

  14. #ifndef Socket_class

  15. #define Socket_class

  16.  

  17. #include <sys/types.h>

  18. #include <sys/socket.h>

  19. #include <netinet/in.h>


  20. #include <netdb.h>

  21. #include <unistd.h>

  22. #include <string>

  23. #include <arpa/inet.h>

  24.  

  25. const int MAXHOSTNAME = 200;


  26. const int MAXCONNECTIONS = 5;

  27. const int MAXRECV = 500;

  28.  


  29. class Socket

  30. {

  31.  public:

  32.   Socket();

  33.   virtual ~Socket();


  34.  

  35.   // Server initialization

  36.   bool create();

  37.   bool bind ( const int port );


  38.   bool listen() const;

  39.   bool accept ( Socket& ) const;


  40.  

  41.   // Client initialization

  42.   bool connect ( const std::string host, const int port );


  43.  

  44.   // Data Transimission

  45.   bool send ( const std::string ) const;


  46.   int recv ( std::string& ) const;

  47.  

  48.  

  49.   void set_non_blocking ( const bool );


  50.  

  51.   bool is_valid() const { return m_sock != -1; }

  52.  

  53.  private:


  54.  

  55.   int m_sock;

  56.   sockaddr_in m_addr;

  57.  

  58.  

  59. };

  60.  


  61.  


  62. // Socket.cpp

  63. #include <iostream>

  64. #include <string>

  65. #include <errno.h>

  66. #include <fcntl.h>

  67. #include "Socket.h"


  68.  

  69. Socket::Socket() :

  70.   m_sock ( -1 )

  71. {

  72.  

  73.   memset ( &m_addr,


  74.            0,

  75.            sizeof ( m_addr ) );


  76.  

  77. }

  78.  

  79. Socket::~Socket()

  80. {

  81.   if ( is_valid() )

  82.     ::close ( m_sock );


  83. }

  84.  

  85. bool Socket::create()

  86. {

  87.   m_sock = socket ( AF_INET,

  88.                     SOCK_STREAM,


  89.                     0 );

  90.  

  91.   if ( ! is_valid() )


  92.     return false;

  93.  

  94.  

  95.   // TIME_WAIT - argh

  96.   int on = 1;


  97.   if ( setsockopt ( m_sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) ) == -1 )


  98.     return false;

  99.  

  100.  

  101.   return true;

  102.  

  103. }


  104.  

  105.  

  106.  

  107. bool Socket::bind ( const int port )

  108. {

  109.  


  110.   if ( ! is_valid() )

  111.     {

  112.       return false;


  113.     }

  114.  

  115.  

  116.  

  117.   m_addr.sin_family = AF_INET;

  118.   m_addr.sin_addr.s_addr = INADDR_ANY;


  119.   m_addr.sin_port = htons ( port );

  120.  

  121.   int bind_return = ::bind ( m_sock,


  122.                              ( struct sockaddr * ) &m_addr,


  123.                              sizeof ( m_addr ) );


  124.  

  125.  

  126.   if ( bind_return == -1 )

  127.     {

  128.       return false;


  129.     }

  130.  

  131.   return true;

  132. }

  133.  

  134.  

  135. bool Socket::listen() const


  136. {

  137.   if ( ! is_valid() )

  138.     {

  139.       return false;


  140.     }

  141.  

  142.   int listen_return = ::listen ( m_sock, MAXCONNECTIONS );

  143.  

  144.  

  145.   if ( listen_return == -1 )


  146.     {

  147.       return false;

  148.     }

  149.  

  150.   return true;


  151. }

  152.  

  153.  

  154. bool Socket::accept ( Socket& new_socket ) const

  155. {

  156.   int addr_length = sizeof ( m_addr );


  157.   new_socket.m_sock = ::accept ( m_sock, ( sockaddr * ) &m_addr, ( socklen_t * ) &addr_length );


  158.  

  159.   if ( new_socket.m_sock <= 0 )

  160.     return false;


  161.   else

  162.     return true;

  163. }

  164.  

  165.  

  166. bool Socket::send ( const std::string s ) const


  167. {

  168.   int status = ::send ( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL );

  169.   if ( status == -1 )


  170.     {

  171.       return false;

  172.     }

  173.   else


  174.     {

  175.       return true;

  176.     }

  177. }

  178.  

  179.  


  180. int Socket::recv ( std::string& s ) const

  181. {

  182.   char buf [ MAXRECV + 1 ];


  183.  

  184.   s = "";

  185.  

  186.   memset ( buf, 0, MAXRECV + 1 );


  187.  

  188.   int status = ::recv ( m_sock, buf, MAXRECV, 0 );

  189.  

  190.   if ( status == -1 )


  191.     {

  192.       std::cout << "status == -1   errno == " << errno << "  in Socket::recv\n";


  193.       return 0;

  194.     }

  195.   else if ( status == 0 )


  196.     {

  197.       return 0;

  198.     }

  199.   else


  200.     {

  201.       s = buf;

  202.       return status;

  203.     }


  204. }

  205.  

  206.  

  207.  

  208. bool Socket::connect ( const std::string host, const int port )


  209. {

  210.   if ( ! is_valid() ) return false;

  211.  

  212.   m_addr.sin_family = AF_INET;


  213.   m_addr.sin_port = htons ( port );

  214.  

  215.   int status = inet_pton ( AF_INET, host.c_str(), &m_addr.sin_addr );


  216.  

  217.   if ( errno == EAFNOSUPPORT ) return false;

  218.  

  219.   status = ::connect ( m_sock, ( sockaddr * ) &m_addr, sizeof ( m_addr ) );


  220.  

  221.   if ( status == 0 )

  222.     return true;

  223.   else


  224.     return false;

  225. }

  226.  

  227. void Socket::set_non_blocking ( const bool b )


  228. {

  229.  

  230.   int opts;

  231.  

  232.   opts = fcntl ( m_sock,

  233.                  F_GETFL );


  234.  

  235.   if ( opts < 0 )

  236.     {

  237.       return;


  238.     }

  239.  

  240.   if ( b )

  241.     opts = ( opts | O_NONBLOCK );


  242.   else

  243.     opts = ( opts & ~O_NONBLOCK );

  244.  

  245.   fcntl ( m_sock,


  246.           F_SETFL,opts );

  247.  

  248. }

  249.  

  250. // client.h

  251. #ifndef _CLIENT_H_

  252. #define _CLIENT_H_


  253.  

  254. #include "Socket.h"

  255.  

  256. class Client : private Socket

  257. {

  258.   private:


  259.   public:

  260.     Client(int port);

  261.     Client() {};


  262.     virtual ~Client();

  263.  

  264.     const Client& operator <<(const std::string&) const;


  265.     const Client& operator >>(std::string&) const;

  266.  

  267.     void accept(Client&);


  268. };

  269.  


  270.  

  271. // client.cpp

  272. #include <iostream>

  273. #include "Client.h"

  274.  

  275. Client::Client(int port)


  276. {

  277.   if(!Socket::create())

  278.     std::cout<<"Could not create socket\n";

  279.  

  280.   if(!Socket::bind(port) )


  281.     std::cout<<"Could not bind to port.\n";

  282.  

  283.   if(!Socket::listen())

  284.     std::cout<<"Could not listen to socket.\n";


  285. }

  286.  

  287. Client::~Client()

  288. {

  289. }

  290.  

  291.  

  292. const Client& Client::operator <<(const std::string& s) const


  293. {

  294.   if(!Socket::send(s))

  295.     std::cout<<"Could not write to socket.\n";

  296.    


  297.   return *this;

  298. }

  299.  

  300.  

  301. const Client& Client::operator >>(std::string& s) const


  302. {

  303.   if(!Socket::recv(s))

  304.     std::cout<<"Could not read from socket.\n";

  305.  

  306.   return *this;


  307. }

  308.  

  309. void Client::accept(Client& sock)

  310. {

  311.   if (!Socket::accept(sock))


  312.     std::cout<<"Could not accept socket.\n";

  313. }

  314.  
Parsed in 0.238 seconds
Statistics