Click here to Skip to main content
15,890,527 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: converting TCHAR to CString giving some garbage value Pin
Cedric Moonen11-Feb-09 20:19
Cedric Moonen11-Feb-09 20:19 
GeneralRe: converting TCHAR to CString giving some garbage value Pin
VCProgrammer11-Feb-09 20:51
VCProgrammer11-Feb-09 20:51 
GeneralRe: converting TCHAR to CString giving some garbage value Pin
Cedric Moonen11-Feb-09 20:56
Cedric Moonen11-Feb-09 20:56 
AnswerRe: converting TCHAR to CString giving some garbage value Pin
«_Superman_»11-Feb-09 20:26
professional«_Superman_»11-Feb-09 20:26 
AnswerRe: converting TCHAR to CString giving some garbage value Pin
VC++Maniac11-Feb-09 20:32
VC++Maniac11-Feb-09 20:32 
AnswerRe: converting TCHAR to CString giving some garbage value Pin
CPallini11-Feb-09 23:40
mveCPallini11-Feb-09 23:40 
QuestionRe: converting TCHAR to CString giving some garbage value Pin
David Crow12-Feb-09 3:09
David Crow12-Feb-09 3:09 
QuestionSocket main() working, Class is not Pin
gvanto11-Feb-09 19:28
gvanto11-Feb-09 19:28 
I've been following Beej's Guide to network programming, (which is really great) and when implementing
his streaming socket server ( <a href="http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#simpleserver">http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#simpleserver</a>[<a href="http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#simpleserver" target="_blank" title="New Window">^</a>] )

it works just fine. To test it I opened a Konsole (using Kubuntu) and ran:
<code>
pacific@mainboxUnsure | :~ $ telnet localhost 3490
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hello, world!Connection closed by foreign host.
</code>

However, when trying to make a class out of the above code, as shown below, I get a "accept: Bad file descriptor" error, repeatedly.
Not sure if these two functions (which are not part of the class) have anything to do with it:
void sigchld_handler(int s);
void *get_in_addr(struct sockaddr *sa);

Help on this would be much appreciated !!
Gvanto

Sources also here: <a href="http://www.sharedigest.com/socketserver.tar.gz">http://www.sharedigest.com/socketserver.tar.gz</a>[<a href="http://www.sharedigest.com/socketserver.tar.gz" target="_blank" title="New Window">^</a>]

main (WLFeeder.cpp):
<code>
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

#include "SocketServer.h"

#define DO_MAIN 1

using namespace std;
using namespace wlns;

#ifdef DO_MAIN
int main(int argc, char *argv[]) {

SocketServer *s = new SocketServer();

if( s->initAddrInfo() != SUCCESS ) {
cout << "Error initializing address " << endl;
exit(1);
}

if( s->initBind() != SUCCESS ) {
cout << "Error initializing bind " << endl;
exit(1);
}

if( s->initListen() != SUCCESS ) {
cout << "Error initializing listen " << endl;
exit(1);
}

if( s->clearDeadProcesses() != SUCCESS ) {
cout << "Error clearing dead processes " << endl;
exit(1);
}

if( s->listenForConnections() != SUCCESS ) {
cout << "Error in listening for connections " << endl;
exit(1);
}

return 0;
}
#endif

</code>


SocketServer.h:

<code>
/*
* SocketServer.h
*
* Created on: 11/02/2009
* Author: pacific
*
* Generic, extendable socket-managing class handling
* network communications via sockets
*/

#ifndef SOCKETSERVER_H_
#define SOCKETSERVER_H_

#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>


#define BACKLOG 10 //no. of pending connections the queue will hold
#define PORT "3490"

using namespace std;
namespace wlns {

static int SUCCESS = 1;
static int FAILURE = 0;

class SocketServer {
private:
int sockfd_, new_fd_; //listen on sock_fd_, new connection on new_fd_
struct addrinfo hints_, *servinfo_, *p_; //hints and actual address
struct sockaddr_storage their_addr_; //connector's address information
socklen_t sin_size_;
struct sigaction sa_;
int yes_;
char s_[INET6_ADDRSTRLEN];
int rv_;

//char *remote_host_, *remote_port_;


public:
SocketServer();
virtual ~SocketServer();
int setLocalPort(char *lp);
char *getLocalPort();
int initAddrInfo();
int initBind();
int initListen();
int clearDeadProcesses();
int listenForConnections();


// int getSocketFileDescriptor();
// int setRemoteHostAndPort(char *host, char *port);
// int connectToRemoteHost();
};



}
#endif /* SOCKETSERVER_H_ */

</code>


SocketServer.cpp:
<code>
/*
* SocketServer.cpp
*
* Created on: 11/02/2009
* Author: pacific
*/

#include "SocketServer.h"

using namespace std;

namespace wlns {

void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}

// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

SocketServer::SocketServer() {
yes_ = 1;
memset(&hints_, 0, sizeof hints_);
hints_.ai_family = AF_UNSPEC;
hints_.ai_socktype = SOCK_STREAM;
hints_.ai_flags = AI_PASSIVE; //bind to IP of the host we're running on, p148 Beej's
}

SocketServer::~SocketServer() {
// TODO Auto-generated destructor stub
}



/**
* Populates servinfo with the appropriate
*/
int SocketServer::initAddrInfo() {
cout << "initAddrInfo() :: Initializing address ..." << endl;
if( (rv_ = getaddrinfo(NULL, PORT, &hints_, &servinfo_)) == -1 ) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv_));
return FAILURE;
}
return SUCCESS;
}

int SocketServer::initBind() {
cout << "bindFirstPossible() :: ..." << endl;

for(p_ = servinfo_; p_ != NULL; p_ = p_->ai_next) {
if( (sockfd_ = socket(p_->ai_family, p_->ai_socktype, p_->ai_protocol)) == -1) {
perror("server: socket");
continue;
}

if( setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR, &yes_, sizeof(int)) == -1 ) {
perror("setsockopt");
return FAILURE;
}

if (bind(sockfd_, p_->ai_addr, p_->ai_addrlen) == -1) {
close(sockfd_);
perror("server: bind");
continue;
}

break;
}

freeaddrinfo(servinfo_); //done with this struct

if(p_ == NULL) {
fprintf(stderr, "server failed to bind\n");
return FAILURE;
}

return SUCCESS;
}

int SocketServer::initListen() {
cout << "initListen() :: ..." << endl;
if( listen(sockfd_, BACKLOG) == -1 ) {
perror("listen");
return FAILURE;
}
return SUCCESS;
}

int SocketServer::clearDeadProcesses() {
cout << "clearDeadProcesses() :: ..." << endl;
sa_.sa_handler = sigchld_handler; //assign function pointer - not sure if it matters if its not a class method ?
sigemptyset(&sa_.sa_mask);
sa_.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa_, NULL) == -1) {
perror("sigaction");
return FAILURE;
}
return SUCCESS;
}

int SocketServer::listenForConnections() {
cout << "listenForConnections() :: Listening..." << endl;
int rc = SUCCESS;
while(1) {
sin_size_ = sizeof their_addr_;
cout << "waiting to accept " << endl;
new_fd_ = accept(sockfd_, (struct sockaddr *)&their_addr_, &sin_size_);
if(new_fd_ == -1) {
perror("accept");
rc = FAILURE;
continue;
}
inet_ntop(their_addr_.ss_family, get_in_addr((struct sockaddr *)&their_addr_), s_, sizeof s_);
printf("SocketServer: got connection from %s\n", s_);

if(!fork()) { //this is child process
close(sockfd_);
if (send(new_fd_, "Hello, world!", 13, 0) == -1) {
perror("send");
rc = FAILURE;
}
close(new_fd_);
}
close(new_fd_); //parent doesnt need this
}
return rc;
}




} //end ns
</code>


OUTPUT:
<code>
pacific@mainboxUnsure | :~ /workspace/WLFeeder/Debug$ ./WLFeeder
initAddrInfo() :: Initializing address ...
bindFirstPossible() :: ...
initListen() :: ...
clearDeadProcesses() :: ...
listenForConnections() :: Listening...
waiting to accept //Here I entered the telnet command again, in a seperate Konsole terminal ...
SocketServer: got connection from ::ffff:127.0.0.1 //works once
waiting to accept
accept: Bad file descriptor // then keeps giving errors ...
waiting to accept
accept: Bad file descriptor
waiting to accept
accept: Bad file descriptor
waiting to accept
accept: Bad file descriptor
waiting to accept
</code>

Find Your Dream Job in Australia, Free!
http://www.WebCV.com.au
AnswerRe: Socket main() working, Class is not Pin
Stuart Dootson11-Feb-09 22:03
professionalStuart Dootson11-Feb-09 22:03 
AnswerRe: Socket main() working, Class is not Pin
gvanto11-Feb-09 23:49
gvanto11-Feb-09 23:49 
GeneralRe: Socket main() working, Class is not Pin
Stuart Dootson12-Feb-09 0:14
professionalStuart Dootson12-Feb-09 0:14 
GeneralRe: Socket main() working, Class is not Pin
gvanto12-Feb-09 0:59
gvanto12-Feb-09 0:59 
QuestionMFC multilingual Application Pin
Varun Bhatt11-Feb-09 18:44
Varun Bhatt11-Feb-09 18:44 
AnswerRe: MFC multilingual Application Pin
Sarath C11-Feb-09 18:57
Sarath C11-Feb-09 18:57 
GeneralRe: MFC multilingual Application Pin
Varun Bhatt11-Feb-09 19:15
Varun Bhatt11-Feb-09 19:15 
GeneralRe: MFC multilingual Application Pin
Sarath C11-Feb-09 23:09
Sarath C11-Feb-09 23:09 
AnswerRe: MFC multilingual Application Pin
Hamid_RT11-Feb-09 19:15
Hamid_RT11-Feb-09 19:15 
QuestionSplitter window problem Pin
ATM@CodeProject11-Feb-09 18:41
ATM@CodeProject11-Feb-09 18:41 
AnswerRe: Splitter window problem Pin
«_Superman_»11-Feb-09 20:23
professional«_Superman_»11-Feb-09 20:23 
GeneralRe: Splitter window problem Pin
ATM@CodeProject11-Feb-09 21:22
ATM@CodeProject11-Feb-09 21:22 
QuestionType casting Pin
Krishnakumartg11-Feb-09 18:09
Krishnakumartg11-Feb-09 18:09 
AnswerRe: Type casting Pin
«_Superman_»11-Feb-09 18:22
professional«_Superman_»11-Feb-09 18:22 
GeneralRe: Type casting Pin
Krishnakumartg11-Feb-09 18:25
Krishnakumartg11-Feb-09 18:25 
GeneralRe: Type casting Pin
«_Superman_»11-Feb-09 18:27
professional«_Superman_»11-Feb-09 18:27 
GeneralRe: Type casting Pin
Krishnakumartg11-Feb-09 18:37
Krishnakumartg11-Feb-09 18:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.