Click here to Skip to main content
13,193,048 members (84,735 online)
Rate this:
Please Sign up or sign in to vote.
See more:
int sock ;  // Socket
int c ;    //  condition
struct sockaddr_in serveraddr ;  //AF_INET
unsigned short Portnumber = 3306 ; //Port#

sock = socket(AF_INET , SOCK_STREAM , 0) ;
if ( sock == -1 )
memset(&serveraddr , 0 , sizeof serveraddr);

serveraddr.sin_family = AF_INET ;
serveraddr.sin_port   = htons(Portnumber);
serveraddr.sin_addr.s_addr  = ntohl(INADDR_ANY);
bind(sock , (struct sockaddr *)&serveraddr , sizeof serveraddr);

system("netstat -pa --tcp 2>/dev/null ");

when i run this program i cant see my bound socket in output!!
please help,
i want to write a program that listen on a specific port and each client can connect to it.
Posted 21-Jul-12 21:45pm
Updated 21-Jul-12 22:06pm
Mehdi Gholam345.4K
Code-o-mat 22-Jul-12 5:43am
Not sure if this might help anything, but shouldn't you specify PF_INET for the third parameter of the socket call (where you have zero now)? Otherwise, this might be a synchronization-kind of problem, like, you do the bind call and then right after you do the netstat call, maybe the system didn't have time yet to completely register your socket as bound? Or maybe your socket will be only listed if you also call 'accept'?
Bathooman 22-Jul-12 5:47am
Let's check it sir!!!!
Code-o-mat 22-Jul-12 5:55am
aspnet_regiis 22-Jul-12 15:18pm
What ????????????????
Sergey Alexandrovich Kryukov 22-Jul-12 18:32pm
What do you mean by "see a socket"? If you need to implement communication through sockets, implement it. What's the question, exactly?
pasztorpisti 23-Jul-12 18:45pm
Well, the question isn't written down but reading the post reminds me a very common probelm I encountered when I developed tcp socket applications. If you start a server for debugging, and you just kill the application then you can not restart your server for some time (about 1-2minutes) because the listen port is bound (bind returns with error) despite the fact that your server program is already dead and isn't running. This happens because the operating system keeps your socket object alive even after you closed it because it isn't sure that every opeartion on the socket has already completed (read/write/handshake/shutdown sequence). This feature is called LINGER. its usually turned on by default. To avoid this behavior in the operating system you should make sure that this feature is turned on and the linger timeout is set to zero. Check out setsockopt() and SO_LINGER (and the windows specific SO_DONTLINGER) if your problem is what I described above.
EDIT: sorry, I totally misunderstood the question. :D I think it depends on the network stack implementation when the socket actually gets registered into specific tables. I'm not sure if your bound socket pops up in the netstat list before you call listen() on it - that is, you put it into work.
pasztorpisti 23-Jul-12 18:53pm
If I remember right AF_INET and PF_INET are exchangeable. The third parameter is usually IPPROTO_TCP but I have seen zero there in a lot of examples (especially on linux).
James R. Twine 25-Jul-12 13:43pm
I thought you would only see active sockets in NETSTAT, so you would have to either called a listen on it, or have it connected to something.
pasztorpisti 25-Jul-12 19:06pm
Your assumption is probably true, I'm just unsure about the implementation of bind() function on different platforms. The bind() function can return WSAEADDRINUSE this is the reason for me to suspect that bind() already puts the socket somewhere so it can find out if bound addresses are unique.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171017.2 | Last Updated 25 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100