connect() fails with ECONNREFUSED

nietod
nietod used Ask the Experts™
on
I am new to programming sockets and new to linex both.

I have code that creates a socket descriptor and tries to connect to perdue.edu's port 13 (or 23) (time or remote login).  I can connect fine to this address and these ports with telnet, but not with my code.  I always get ECONNREFUSED.  Any ideas why?   ( just looking for a few suggestions or pointers or things to test for, I don't need code (probably)).

(Note I did call htonl() to convert the address to the right byte order.  (well, not on my first attempt...))

(Sorry no code to post right now, its too embedded in classes to post a reasonable example, I may have to write a small example and see what happens)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ur connection is refused because the telnet server in ur sytstem is connected to that port. That is ,the port is busy.
Shut down the telent server and try ur code. It should work.
Top Expert 2006
Commented:
from connect man page

 Generally, connection-based protocol sockets may successfully connect only once;

are you using TCP ?? if yes, then may be you had successfully connected once but later attempts are failing ... make sure to close connection before you exit ...

any further guesses would need some code to look at ... then information you have so far provided is not good enough to pin point the problem

since you are new to socket programming, you can start with simpler stand alone examples rather than embedding it in complex code ....

follow the link below for one of the best socket programming tutorials available on the web

http://www.ecst.csuchico.edu/~beej/guide/net/html/

Author

Commented:
>> Ur connection is refused because the telnet server in ur sytstem is connected to
>> that port. That is ,the port is busy.
Its not connected at the time of testing.

>> are you using TCP ?? if yes, then may be you had
>> successfully connected once but later attempts are failing
The first attempt fails.


However, I think I have the solution.  I wasn't switching the port numbers from local to network byte order, just the IP address.  I fixed that and it now _appears_ to be working.  

I'm going to leave this question open a little longer in case it isn't working, then I'll decide what to do to close it

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial