LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 04-27-2015, 11:25 AM   #1
bill_nimmo
LQ Newbie
 
Registered: Nov 2014
Posts: 20

Rep: Reputation: Disabled
Unhappy c programming - socket 'connect' returns 0 even on invalid port


Greetings All,

This one confuses me. According to documentation, when I try to do a socket 'connect', the call will return 0 if everything went ok (I am assuming that means that it connected to a server or someone listening on that port). I have a program (example below) that attempts to create a connection to a valid IP BUT an invalid port number. Maybe I am missing the point (or doing something stupid in my code)??? In the code below, the call to connect returns 0 (I have tried many different valid and invalid port numbers...it always returns 0):

Code:
static struct sockaddr_in Server;
int connectToServer()
{
   int return_stat;
   int connection;
   int optval;
   int valid_ip;
   int SockFD;
   int SomeGlobalIP = nnn.nnn.nnn.nnn;
   int SomeInvalidPortNumber = 111222333444;

   memset(&Server, 0, sizeof(Server);
   signal(SIGPIPE,SIG_IGN);
   errno=0;
   SockFD = socket(AF_INET, SOCK_STREAM, 0);
   if (SockFD < 0)
      return -1;
   valid_ip = inet_aton(SomeGobalIP, &Server.sin_addr);
   if(valid_ip == 0)
     return -1;
   Server.sin_port = htons((short)SoemInvalidPortNumber);
   setsockopt(SockFD, SO_REUSEADDR, (char*)&optval, sizeof(int));
   connection - connect(SockFD, (struct sockaddr *)&Server, sizeof(Server));
   if (connection < 0)
      return -1;

   return 0;
}

Last edited by bill_nimmo; 04-27-2015 at 01:49 PM.
 
Old 04-27-2015, 12:32 PM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
If you use [code][/code] tags when posting your code will keep it's formatting and be easier to read.

On my system SHRT_MAX = 32767. At the very least it means that you code is invoking undefined behavior when it does:
Code:
Server.sin_port = htons((short)SoemInvalidPortNumber);
with SomeInvalidPortNumber = 111222333444.

Anything is possible strictly speaking, some things being more likely then others.
 
Old 04-27-2015, 01:47 PM   #3
bill_nimmo
LQ Newbie
 
Registered: Nov 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
I apologize for the code being unformatted. I will use those tags next time.

Perhaps the port number that I put in my example was a bad one (ok, it WAS a bad example). To further explain my situation: I have a server application that is going to listen on a specific port number (8225). The server application is NOT running and when I use that port number (instead of SomeInvalidPortNumber) the 'connect' still returns 0. Whether I have the server app running or not, the 'connect' always seems to return 0. I would think that if the server wasn't running, the 'connect' would fail. Am I missing something, or some concept?

thanks,
Bill
 
Old 04-27-2015, 02:17 PM   #4
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
If there's nothing to connect to at that ip/port pair then connect should return -1;
 
Old 04-27-2015, 03:00 PM   #5
bill_nimmo
LQ Newbie
 
Registered: Nov 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
yes, that is what I thought. But, for some reason whatever number I use for the port number the call to connect returns 0. Its gotta be something that I am not doing right or something I am missing. ??? I didn't see anything obvious but I could just be blind.
 
Old 04-27-2015, 03:35 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,783

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Quote:
Originally Posted by bill_nimmo View Post
Code:
   connection - connect(SockFD, (struct sockaddr *)&Server, sizeof(Server));
              ^
Is that just a typo in your post, or is it in the actual code?
 
Old 04-27-2015, 05:05 PM   #7
bill_nimmo
LQ Newbie
 
Registered: Nov 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
My apologies. I fat fingered it. It should be an equal sign

Code:
   connection = connect(SockFD, (struct sockaddr *)&Server, sizeof(Server));
 
Old 04-27-2015, 06:51 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
We can only find problems in what you posted, so you should really post some actual code that you have a problem with. It should be a Short, Self Contained, Correct (Compilable), Example.

Otherwise we can keep playing the is-this-mistake-just-from-posting game, but get no closer to solving anything.
Code:
    int SomeGlobalIP = nnn.nnn.nnn.nnn;
    // should be
    const char* SomeGlobalIP = "nnn.nnn.nnn.nnn";
Code:
   setsockopt(SockFD, SO_REUSEADDR, (char*)&optval, sizeof(int));
You never initialized optval.
 
Old 04-27-2015, 11:43 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
I think it is time to re-quote the actual program, without manual edits.
 
Old 04-28-2015, 07:55 AM   #10
bill_nimmo
LQ Newbie
 
Registered: Nov 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
My apologies to all. The real code actually is written and running in a classified lab; thus, cannot be removed from the lab. I had to type it in from memory. I will re-post with code that compiles somehow. Again, I apologize.

Bill
 
Old 04-28-2015, 09:40 AM   #11
bill_nimmo
LQ Newbie
 
Registered: Nov 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
solved

Thank you all for your responses. I did some more digging and think I figured it out. At least, now I get it to fail with an invalid port, and succeed with a valid port. The piece of code that I was missing was:

Code:
   Server.sin_family = AF_INET;
From what I think I read, the call to connect will return 0 regardless of the port number if it thinks you want a raw socket.

Thank you, again.

Bill
 
Old 04-28-2015, 10:21 AM   #12
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
I think more likely it's this (from the connect(2) man page):

Connectionless sockets may dissolve the association by connecting to an address with the sa_family member of sockaddr set to AF_UNSPEC (supported on Linux since kernel 2.2).
 
Old 04-28-2015, 10:42 AM   #13
bill_nimmo
LQ Newbie
 
Registered: Nov 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
Ahhhh, ok. That's probably it. Thank you all again.

Bill
 
  


Reply



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 programming cannot connect why_so_serious Linux - Newbie 2 10-29-2013 06:04 AM
[SOLVED] How to connect a java client to a BSD server using socket programming sanju.lnt Programming 2 08-06-2011 02:57 PM
RADIUS connection. Socket connect failed. Invalid argument after running AguaGatekeer ukrainet Linux - Newbie 0 11-25-2004 04:02 AM
How to connect server with muliple port via socket husniteja Programming 0 08-21-2004 02:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:10 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration