| |
 |
|
|
Linux Forum Index » Linux Development - Applications » socket programming linux...
Page 1 of 1
|
| Author |
Message |
| ... |
Posted: Fri Jul 04, 2008 1:58 am |
|
|
|
Guest
|
hi people,
I have written a list of C++ programs using the material given on
various websites to create a client server model. following is the
code of a module which is giving error during compilation.
The code is for the implementation of a socket class.
#include "Socket.h"
#include "string.h"
#include <string.h>
#include <errno.h>
#include <fcntl.h>
using namespace std;
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
{
using namespace std;
char buf [ MAXRECV + 1 ];
s = "";
memset ( buf, 0, MAXRECV + 1 );
int status = ::recv ( m_sock, buf, MAXRECV, 0 );
if ( status == -1 )
{
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 );
}
error encountered while using g++ compiler:
in member function int socket::recv ( std::string& s ) const':
Socket.cc:134: 'cout' undeclared first use this function
kindly help me out. i think that there is an error because of the
compiler im using..
thanks
akshay |
|
|
| Back to top |
|
| Jens Thoms Toerring... |
Posted: Fri Jul 04, 2008 8:19 am |
|
|
|
Guest
|
akshay_tyagi007 at (no spam) rediffmail.com wrote:
Quote: I have written a list of C++ programs using the material given on
various websites to create a client server model. following is the
code of a module which is giving error during compilation.
The code is for the implementation of a socket class.
#include "Socket.h"
#include "string.h"
#include <string.h
#include <errno.h
#include <fcntl.h
using namespace std;
error encountered while using g++ compiler:
in member function int socket::recv ( std::string& s ) const':
Socket.cc:134: 'cout' undeclared first use this function
I am not a C++ programmer but I am rather sure you need
#include <iostream>
in order to get 'cout' declared.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt at (no spam) toerring.de
\__________________________ http://toerring.de |
|
|
| Back to top |
|
| Johannes Dohmen... |
Posted: Fri Jul 04, 2008 5:48 pm |
|
|
|
Guest
|
Quote: I have written a list of C++ programs using the material given on
various websites to create a client server model. following is the
code of a module which is giving error during compilation.
The code is for the implementation of a socket class.
#include "Socket.h"
#include "string.h"
#include <string.h
#include <errno.h
#include <fcntl.h
error encountered while using g++ compiler:
in member function int socket::recv ( std::string& s ) const':
Socket.cc:134: 'cout' undeclared first use this function
As Jens said already you need to include <iostream> to have cout (and
cin and cerr) available. But maybe it's included in Socket oder
"string.h" (btw: I would not recommend to use a name of a standard
library header file for your header files).
Quote: kindly help me out. i think that there is an error because of the
compiler im using..
That might be as well, as the C++ Standards Committee once decided to
put virtualy all stl features in namespace std. Since g++ version 3 the
compiler obeys this and in order to access cout you must qualify it with
std-namespace:
std::cout << "Hello world" << std::endl;
Or use a
using namespace std;
declaration.
Read more about the namespaces and the std namespace here:
http://www.winterdom.com/dev/cpp/nspaces.html
Greetings
Johannes |
|
|
| Back to top |
|
| |
|
Page 1 of 1
All times are GMT - 5 Hours
The time now is Fri Sep 05, 2008 10:59 pm
|
|