Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-21-2012, 05:49 AM   #1
LQ Newbie
Registered: Jul 2011
Posts: 5

Rep: Reputation: Disabled
Connection refused problem

I am trying to connect a port which has been listenning. I checked with netstat -tan and saw
tcp 0 0* LISTEN
There is no firewall. My code is a standart code :

addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("");
addr.sin_port = htons(2000);

if ((sockfd = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) <0)
printf("ERROR opening socket\n");
strcpy(buffer,"hello world\r\n");
if (connect(sockfd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
printf("con failed %s",strerror(errno));

It gives the error "Connection Refused".
I looked at the wireshark log and saw that it is trying to connect any other port that is not been listenning. I can connect to the port with telnet.
What is wrong with this code?
Old 11-21-2012, 04:17 PM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
If you cannot connect with a known-good client (I prefer netcat, 'nc' for testing such cases), it may be that the server is not accepting connections from localhost. What is the nature of the server?

I tried your code with nc listening as a server on port 2000, and it connected was was able to write to the server.

--- rod.

Last edited by theNbomr; 11-21-2012 at 04:23 PM.
Old 11-22-2012, 03:02 AM   #3
LQ Newbie
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
I found out that it is endian problem htons,htonl functions don't work. I tried manually and it works. The server accepts connections from localhost becaues there is any other clients connecting that way.
But i could not find out why htons/htonl functions don't work.


programing, socket, tcp

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket connection refused problem James_dean Linux - Networking 1 09-23-2005 04:51 PM
Connection Refused Problem sangjin Linux - Newbie 1 12-30-2003 06:43 AM
RH 8.0 Xvfb connection refused problem. jol Linux - Distributions 2 12-14-2002 02:20 PM
connection refused problem herambshembekar Linux - Networking 2 04-08-2002 07:48 AM
connection refused problem. jdevanand Linux - Networking 4 03-04-2002 07:30 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:08 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration