LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 11-21-2012, 05:49 AM   #1
daedlock
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 0.0.0.0:2000 0.0.0.0:* LISTEN
There is no firewall. My code is a standart code :

memset(&addr,0,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
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");
printf("port=%d-%d",ntohs(addr.sin_port),addr.sin_port);
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
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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
daedlock
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.
 
  


Reply

Tags
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


All times are GMT -5. The time now is 09:03 PM.

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