Listener Socket not Listening?

Hello,
I have a strange problem. I recently wrote a simple HTTP listener which worked completely up until I threaded the application using boost. Now, the listener thread is ccreated in a thread pool and seems to be executing properly (it passes all of my output checks and doesnt throw any errors during initialization of the socket, but when I try to connect to the socket (telnet 127.0.0.1 42420) the connection is actively refused as if there isnt a listener there at all. Can anyone tell me what Im missing here?

Source files are attached.

Worth 500 points.

Thanks,
Rick
httpserver.h
httpserver.cpp
richardsimnettAsked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
If it's not properly listening, then you should have gotten an error.

I took the liberty of trying the code you posted, and it works fine here.

So, make sure that the code you posted is the same as the code you're running (a full re-build, and double- and triple-checking everything might be useful). Also make sure that you link to the boost_thread library (and that it's not some ancient version).

If that doesn't help - try running the old code (without threads) again, and see if it has the same problem.
0
 
Infinity08Commented:
What is the exact output you got ?


Btw : are you sure that the bind succeeded ? Because you're not checking the right socket :

>>     if (bind_sockfd < 0)
>>       std::cout << "ERROR binding socket" << std::endl;
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
need your config flie as well.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Kyle AbrahamsSenior .Net DeveloperCommented:
you also have this commented out:

           /* if (httpExecutor.size() < cfg->max_http_threads)
                httpExecutor.create_thread(&httpserver::process);*/

what are you doing to process the thread?  Or are you not evening getting connection accepted?
0
 
richardsimnettAuthor Commented:
ged325,
Here is the output when I run the program.

./basic_http
Config Initialized
config init: 0.01a
HTTP Creating Socket.
HTTP Bind Socket port: 1025
HTTP Set Listen.
HTTP Listener Waiting For Connections.
^C

config is really simple it is this:

config::config()
{
    version = "0.01a";
    max_http_threads=50;
    http_listener_port = 1025;
    exit = false;
   
    //config constructor
    std::cout << "Config Initialized" << std::endl;
}

and I have checked to make sure the right port etc make it through to the initialization part.

I tried changing bind_sockfd to just sockfd and there wasnt a change, no error thrown.

At this point Im not processing the request yet, I am simply trying to confirm everything up to this point works properly since I added the multi-thread support.

Thanks,
Rick
0
 
Infinity08Commented:
>> HTTP Bind Socket port: 1025

>> (telnet 127.0.0.1 42420)

That seems inconsistent.
0
 
richardsimnettAuthor Commented:
infinity08,
You are correct. I was testing different ports to see if it was a firewall issue (shouldnt be) - as I turned off the firewall, and then even went as far as to test as root, to see if it was a permissions issue of some sort. None of the above. Originally the port was 42420, then I changed it to 80 (and ran it as root), then changed it to port 1025 which is what you see now.

rick@rick-desktop:~/NetBeansProjects/basic_http/dist/Debug/GNU-Linux-x86$ telnet 127.0.0.1 1025
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
rick@rick-desktop:~/NetBeansProjects/basic_http/dist/Debug/GNU-Linux-x86$

Thanks,
Rick
0
 
Infinity08Commented:
Well, except for what I've mentioned earlier, the code doesn't seem to have a problem that would cause this.

So, if it doesn't work, there's likely something outside of the code that is interfering.

You are running the client on the same host as the server, right ?
You are sure you're not confusing ports, right ?
You are running the server in the exact same way as you did before adding threads (ie. when it still worked), right ?
0
 
richardsimnettAuthor Commented:
Infinity08,
All the code is the same, except now, instead of the listener running in the main thread (single threaded), it is now running in its own thread in the httpExecutor thread pool. That is the only change.

-I am on the same server (my local machine).
-Not confusing ports.
-all firewalls and anything that could possibly be interfering have been turned off. I am running on ubuntu linux and have total control over that as I have root control over the machine.

I have also done netstat -a (to show all the open listening ports on the machine) and while the program is running it is NOT showing a listener socket on port 1025. So that tells me that the code is at fault, or the connection isnt being properly opened for one reason or another.

Thanks,
Rick


0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Would also recommend cleaning the solution (delete all .exe's) and ENSURE it's a complete build.  Ensure nothing else is running on 1025, and ensure you're connecting from a different port.  
0
 
sarabandeCommented:
i wouldn't use port 1025. the ports 1 to 1023 are reserved ports and 1025 therefore is one of the very first non-reserved ports which pretty well could be already used.

i also had problems with 127.0.0.1 in a computer with multiple network adapters. so if your pc has a local ip address like 192.168.1.1 i would use that instead of 127.0.0.1

Sara

0
 
Infinity08Commented:
If 127.0.0.1 is really giving you problems, then try localhost.
0
 
sarabandeCommented:
if it worked before using boost it should work after as well and the issue neither should caused by  port nor ip. but it seems the questioner has changed multiple things since the working program, so it might be worth to make all things reverse which were independent of the boost code.

Sara
0
 
richardsimnettAuthor Commented:
sarabande,
Yes it seems the issue is not with the boost at all. I disabled the threading (commented out the spawn), and am manually running the listener function, and its not listening. I am going to walk through that segment of code this morning and see if I can pinpoint the issue.

There have been many changes over the code the last few days while I was trying to get boost to work, so its more than likely I myself changed something inadvertently that is causing this.

Thanks!
0
 
Infinity08Commented:
As I said earlier : the code you posted initially here, should work. So why not just use that ?

Once you have got that working, you can add your changes to it one by one, and see where it breaks.
0
 
richardsimnettAuthor Commented:
Ok! All fixed. Stupidest problem ever.

got rid of the use namespace std; in the httpserver.cpp, and presto everything started working. I noticed that the compiler threw a warning when compiling the bind command, sayin gsomething about std::_bind and said wait it shouldnt be std... so I took it out, and the program came to life.

Thanks to everyone for the help!

0
 
Infinity08Commented:
:)

Glad you found it.

There's two lessons in there :

1) never ignore warnings (unless you have verified that the warning is harmless)
2) don't use 'using namespace std;' (but that one is a bit more controversial : see http://www.experts-exchange.com/Programming/Languages/CPP/Q_24646218.html for an interesting, but long, read)
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> don't use 'using namespace std;'

+1
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.