ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying implement a small server for a university work, my server is creating, binding and listening but it don't accept any connection, these are the files I'm using in server:
Code:
// Definition of the Socket class
#ifndef SOCKET_H
#define SOCKET_H
#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;
int accept () 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;
};
#endif
Code:
// Implementation of the Socket class.
#include "Socket.h"
#include "string.h"
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <iostream>
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); //cria o socket
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) //define as opções do socket
return false;
return true;
}
bool Socket::bind (const int port){
if(!is_valid()){
return false;
}
m_addr.sin_family = AF_INET; //define as opções no struct
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;
}
int Socket::accept (void) const{
int addr_length = sizeof ( m_addr );
int new_socket = ::accept ( m_sock, ( sockaddr * ) &m_addr, ( socklen_t * ) &addr_length );
if ( new_socket <= 0 )
return(-1);
else
return(new_socket);
}
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 );
}
What is the value of m_sock just before the ::accept() call? I don't mean, what do you think it is? I mean, if you display that value just before calling ::accept(), what do you see?
When ::accept() returns -1, what is the value of errno?
didn't read the code, but, are you sure you can be routed from outside (iptables, NAT, etc?)
check this with nc(server nc -lnvvp <port goes here> and client nc <ip-here> <port here>)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.