LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Socket error on Connect (https://www.linuxquestions.org/questions/programming-9/socket-error-on-connect-336743/)

mdimanna 06-24-2005 08:23 AM

Socket error on Connect
 
I am an experienced socket programmer in older versions of Unix and am now writing a socket based server in Linux. I have written what one would assume to be a hellow world server (socket based) in Linux, but my client always errors out on connect with perror returning "Connection refused". I have turned off the firewall but to no avail. I cannot find anywhere on the web or man pages about this error. Any thoughts???

itsme86 06-24-2005 09:23 AM

It's hard to tell without seeing some code, but I'd suggest checking the return value of all the socket operations to make sure there's no errors.

The order of the socket operations should go:
socket()
** Probably wise to use setsockopt() to set the SO_REUSEADDR flag here **
bind()
listen()
accept()

I'd also suggest maybe just using telnet to try connecting to the server to make sure that side of it works before trying to connect with your client.

mdimanna 06-24-2005 10:28 AM

DUH. Thanks for the advice
 
As I was adding code to check return values on bind, I realized I was using htons on the long parameter for server_addr.sin_addr.s_addr. Thanks for the slap on the head. Sorry.


All times are GMT -5. The time now is 02:38 AM.