Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
C
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 )
{
    error("socket()");
}

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
Updated 21-Jul-12 22:06pm
v2
Comments
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'?
[no name] 22-Jul-12 5:47am    
Let's check it sir!!!!
Code-o-mat 22-Jul-12 5:55am    
Huh?
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).
bbirajdar 22-Jul-12 15:18pm    
What ????????????????

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900