c programming - socket 'connect' returns 0 even on invalid port
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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.
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.
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.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.