Socket++ library problem.


 
Thread Tools Search this Thread
Top Forums Programming Socket++ library problem.
# 1  
Old 08-10-2009
Socket++ library problem.

Hi,

My name is Daniel and I'm spanish, so I'm sorry if you can't undertand something becouse of my low-level english.

Something stranger is happening to me with socket++ library and I don't know how to work on it.

I has a library called commands.so and the sslclient is and object of that library. sslclient creates an iosockinet object. Just when it creates that object, the program exits with a Segmentation fault error.

I compiled socket++ and my program with debug flag and all I see (and all gdb tells me) is that program recives a SIGSERV when it calls iosockinet constructor.

If I do the same on a separated file (main.cpp) that sslclient does, it works. But the most heavy is that my program runs under valgrind. I'm not an expert valgrind user so I don't know if it's normal (I don't think so).

I'm paranoid about the cause... I think it could happend becouse of the command.so library. If it heps, the iosockinet is created after sslclient object is created.

I'm working under gentoo and I tried the same come in 2 completly different gentoo with the same result, so I figure it's my fault. Once compute information is:

Linux servidor-dissmar 2.6.28-gentoo-r5 #1 SMP Tue Jul 21 12:01:33 CEST 2009 i686 Intel(R) Xeon(R) CPU E5310 @ 1.60GHz GenuineIntel GNU/Linux

g++ (GCC) 4.1.2 (Gentoo 4.1.2 p1.0.2)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

gdb output:
Breakpoint 1, sslclient::execute (this=0x805ec90) at network/sslclient.cpp:68
68 iosockinet sock;
(gdb) s

Program received signal SIGSEGV, Segmentation fault.
0xb7bca40e in ?? () from /lib/libc.so.6

valgrid output (just the point before and after, without any errors):
Proceed command execution...
connect...

sslclient.cpp:

#include <iostream>

#include "sslclient.h"
using namespace std;

//sockbuf *sslclient::sock;

extern "C" command *r_done() {
cout << "No message concordances... null command will be returned." << endl;
return NULL;
}

extern "C" command *sslclient() {
/*cout << "On extern method from sslclient.cpp. Setting observers. Wrong point which would do invoker subtype (like player_control or, in this case, network_control)." << endl;
sslclient::sslclient *client = new sslclient::sslclient;
observer o;
client->add_observer(o);
return client;*/
cout << "Creating sslclient command..." << endl;
return new sslclient::sslclient;
}

sslclient::sslclient() {
/*if (sock == NULL) {
int s = socket(AF_INET, SOCK_STREAM,0);
cout << "created socket: " << s << endl;
sock = new sockbuf(s);
//sock = new sockbuf((const sockbuf::sockdesc)sockbuf::sockdesc(socket(AF_INET, SOCK_STREAM,0)));
sockbuf::sockdesc sd(fd);
sock = new sockbuf(sd);
}*/
// sockbuf::sockdesc sd(socket(AF_INET, SOCK_STREAM,0));
// sock = new sockbuf(sd);
}

void sslclient::send_data(iosockinet *sock) {
cout << "send(): sending command content " << command << endl;
while(sock->write(command, strlen(command)) == 0)
cout << "Communication problem... retrying to send command \"" << command << "\"." << endl;
cout << "send(): data sent." << endl;
}

int sslclient::recive_data(iosockinet *sock) {
cout << "recv(): reciveing data content." << endl;
if (data != NULL)
delete [] data;
data = new char[10000];
//int len = sock.read(data, 10000);
int len;sock->read(data, 10000);
if (data != NULL && strcmp(data, "") != 0) {
char *buff = new char[len+2];
strcpy(buff,"r_");
strcpy((char *)buff+2, data);
//delete [] data; //Si el matem peta... pero qui el peta?
data = buff;
buff = NULL;
new_data = true;
cout << "recv(): stored data content " << data << endl;
}
}

void sslclient::execute() {
iosockinet sock; //here <--------------------------------------------------------------------------------
cout << "connect..." << endl;
sock->connect("dissmar.es",4444);
while (sock->is_writeready(-1,0) == 0)
cout << "Internet connection lost or server not respondig. Check hardware and software connections and if problem persists call your admin." << endl;
cout << "Connection stabilished." << endl;
cout << "Starting data transmission..." << endl;
send_data(&sock);
recive_data(&sock);
cout << "Data transmission finished..." << endl;

cout << "disconnect..." << endl;

if (new_data) {
cout << "Notifying command observers..." << endl;
notify_observers();
new_data = false;
}
}

sslclient::~sslclient() {
// delete sock;
}


Thank you in advance,
Daniel.

Edit: code add, sorry. Includes are in sslclient.h.

Last edited by lock.cda; 08-10-2009 at 05:01 PM..
# 2  
Old 08-10-2009
Please post your code.
# 3  
Old 08-11-2009
Code posted.

The code is posted.

---------- Post updated at 04:04 PM ---------- Previous update was at 03:28 PM ----------

It's compiled with:
g++ -ggdb -fPIC -c network/sslclient.cpp -o network/sslclient.o

And linked to it's library with:
g++ -ggdb -ldl network/sslclient.o (some other objects) -fPIC -lsocket++ -shared -o ../libs/commands.so

---------- Post updated at 05:09 PM ---------- Previous update was at 04:04 PM ----------

Using valgrind without tool (--tool=none) I got a more specific error detail:

==20937==
==20937== Process terminating with default action of signal 11 (SIGSEGV)
==20937== Access not within mapped region at address 0x74736978
==20937== at 0x437A40E: (within /lib/libc-2.8.so)
==20937== by 0x437C73E: (within /lib/libc-2.8.so)
==20937== by 0x437E3EA: malloc (in /lib/libc-2.8.so)
==20937== by 0x42A96D8: operator new(unsigned int) (in /usr/lib/gcc/i686-pc-linux-gnu/4.1.2/libstdc++.so.6.0.8)
==20937== by 0x42A980C: operator new[](unsigned int) (in /usr/lib/gcc/i686-pc-linux-gnu/4.1.2/libstdc++.so.6.0.8)
==20937== by 0x4464C3A: sockbuf::sockbuf(int, sockbuf::type, int) (in /usr/lib/libsocket++.so.1.0.2)
==20937== by 0x4042199: network_control::invoke(char*) (network_control.cpp:58)
==20937== by 0x804E199: main (main.cpp:12)

It seems that when it creates an int (which would must be a copy of socket descriptor) it try to create the int in none process memory. I still can't find why.

Any help will be grateful.

---------- Post updated 08-11-09 at 06:26 AM ---------- Previous update was 08-10-09 at 05:09 PM ----------

Al warnings generated compiling with -Wall -Werror are corrected.

If more details are needed, that's how a sslclient is created and the method called:

main has an invoker_factory.
invoker_factory creates the invoker that will call the method.
the invoker calls to command_factory.
command_factory creates a command and returns it to invoker.
invoker calls the execute method from command and program segfaults.

Invoker and command are abstract classes. Subclasses are into a dynamic library (called invokers.so and commands.so respectively) and the objects instances are created by dlopen.

I tried giving more memory space at sockbuf objects and it didn't work (as I expected). I also tried it:

#include "network/sslclient.h"

int main() {
sslclient *s = new sslclient;
s->set_command("foo");
s->execute();
return 0;
};

And it works perfectly. And doing exactly the same into invokers code instead of main, it segfaults. Invokers and commands are created by a dynamic libs and that could be a possible cause. Any suggestions?

Thank you so much in advance.

---------- Post updated at 06:33 AM ---------- Previous update was at 06:26 AM ----------

That's the way that command_factory uses to create commands:

void *hndl = dlopen("../libs/commands.so", RTLD_NOW | RTLD_LOCAL | RTLD_LAZY);
if(hndl == NULL)
cout << "Library commands.so is missing." << endl;
command* (*create)() = (command* (*)())dlsym(hndl, method);
command * cmd = (command *)create();

And the same way is used by invoker_factory, but the library is ../libs/invokers.so.

Thank you again.

Last edited by lock.cda; 08-11-2009 at 04:09 AM..
# 4  
Old 08-11-2009
Does it happen in a different thread?
# 5  
Old 08-12-2009
No. And it's comfirmed by the error which isn't a bad permission, it's becouse of bad memory access.

I think that maybe when the library is compiled the compiler don't let the iosockinet memory space to the library an then it hasn't space enogh. I'll try with gcc3.

Any suggestion is greateful!

---------- Post updated at 11:41 AM ---------- Previous update was at 10:52 AM ----------

Compiling with gcc3 I got that error when make generates the binnary:

/usr/lib/libsocket++.so.1: undefined reference to `__cxa_get_exception_ptr@CXXABI_1.3.1'

After recompile socket++ with gcc3 the compilation error disappears but the segmentation fault persists and valgrind gives exactly the same message. It's not becouse of compiler, it must be an error using the library. But I don't find in official documentation that using socket++ with other libraries could create conflicts. Maybe it's a bad understanding concept about c++ and libraries but as far as I can see I'm using dynamic libraries correctly.

Please, any help will be really greateful!

---------- Post updated 08-12-09 at 06:06 AM ---------- Previous update was 08-11-09 at 11:41 AM ----------

Now I'm trying another way. My software has different command types and some of them needs read and write to the same socket. That's why I created a static class named connection which uses a C socket. That's the code:

connection.h:
#ifndef TAD_CONNECTION_H
#define TAD_CONNECTION_H
#include <netdb.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <unistd.h>

class connection {
private:
static int sock;
static struct sockaddr_in Direccion;
//static struct hostent *Host;
public:
int static make_connect();
int static send(const char* cmd);
int static recv(char *data, int len);
};
#endif

connection.cpp:
#include <iostream>
#include "connection.h"
using namespace std;

int connection::sock = -1;
struct sockaddr_in connection:Smilieireccion;
//struct hostent *connection::Host;

int connection::make_connect() {
//connection::Host = gethostbyname ("dissmar.es");
//cout << "Host: " << Host << endl;
if (sock == -1) {
sock = socket (AF_INET, SOCK_STREAM, 0);
cout << sock << endl;
Direccion.sin_family = AF_INET;
Direccion.sin_addr.s_addr = inet_addr( "127.0.0.1" );
//((struct in_addr*)(Host->h_addr))->s_addr;
Direccion.sin_port = 4442;
}
int i;
return i = connect(sock,(struct sockaddr*)&Direccion,sizeof(struct sockaddr));
}

int connection::send(const char* cmd){
int i = write(sock, cmd, strlen(cmd));
return i;
}

int connection::recv(char *data, int len){
return read(sock, data, len);
}


But it never connects and errno says that connection is refused. Server is running and other applications are making petitions on it.

Is it possible that a static member hasn't privileges enough to call connect? Or maybe a static member cannot call connect function becouse of other reasons? I've been looking in man pages and other official documentation and I couldn't find anything about conflicts between C socket and static members.

If I use the connection class on a separated main file connect fails too.

Thankyou so much again.
Daniel.

---------- Post updated at 11:25 AM ---------- Previous update was at 06:06 AM ----------

More information that I forgot:

connect() function sets errno with Connection refused but tcpdump don't show me any packet sent.

Any suggestions? I really need it! Thank you.

Last edited by lock.cda; 08-11-2009 at 01:13 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

AIX full path to socket library

Can somebody help me too identify full path to socket library on AIX? Cannot find anything Thanks for contribution (2 Replies)
Discussion started by: digioleg54
2 Replies

2. IP Networking

Execution Problem with socket

Hi My socket program is to communicate between the two systems connected with lan. Always i am getting an error saying bind value is -1 or not connected. ..If i run both the server and client programs in the same machine in two diff terminals they are working but they are not working between two... (3 Replies)
Discussion started by: Gurvareddy
3 Replies

3. Linux

./configure problem for libsf library due to apparently missing libdb library.

Hello, ./configure script fails to configure libsf. Please check the following last few lines of configure script error. checking for db1/db.h... no checking for db.h... yes checking for dbopen in -ldb1... no configure: error: No libdb? No libsf. But find command shows the following; ... (4 Replies)
Discussion started by: vectrum
4 Replies

4. Programming

Problem with Socket program..

I wrote a program which will send a message to multiple clients(i.e, broadcasting) that are connected to a server.Once when the client receives a message from the server ,the client should read a file in the server and display it in the client.The client which responds (i.e, client wants all the... (3 Replies)
Discussion started by: vigneshinbox
3 Replies

5. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

6. Programming

and again, socket() related problem...

Dear All, I've searched many topics and googled many web-pages, but still I didn't found solution to this problem. I want to set timeout for connect(). The thing is, that my code works only on BSD, on Linux (tested on SuSE box) it freezes at connect() call :( bool SomeFunc(std::string... (1 Reply)
Discussion started by: sggkxv
1 Replies

7. UNIX for Advanced & Expert Users

problem with socket reading

I am not able to receive the message on socket in the current process when its waiting for its child to exit. code looks something like below //in one thread of the current process //thread 1 =============================================== int numBytes = read(sockid,buf,SIZE); //Now the... (2 Replies)
Discussion started by: swap007
2 Replies

8. Programming

Using Cygwin, how do I make eclipse recognize C's Socket library?

Hi, I am trying to develop a simple program that uses Sockets. I have a windows machine and MUST use C++. I found out that I can use C's Socket (#include <sys/socket.h>) API calls, but this is only possible on a unix machine. So I installed Cygwin to imitate a unix environment on my windows... (4 Replies)
Discussion started by: wannabeTekkie
4 Replies

9. Programming

Problem Connecting to Socket

Can anyone help? I'm trying to write a program which will write to a socket. I can get the server to run, but always get an error when I try to connect. It gives me an error at the "connect" command. It's probably a simple error, but I can't seem to find it. #include <sys/socket.h>... (6 Replies)
Discussion started by: Stevhp
6 Replies

10. Programming

Socket Problem

Hi all, I have developed server/client application (using C) and tested it on the same machine .. but when I deploy them on different machines I get connection timeout. Well .. server machine and client machine exists on different network segments, so there is a linux firewall box to route... (3 Replies)
Discussion started by: Agent007
3 Replies
Login or Register to Ask a Question