Linux Socket Bind Program Won't show with netstat
The following program runs and compiles after doing the following:
$g++ inetaton.cpp -o inetaton $./inetaton The only problem is that the output doesn't appear when I run netstat in the program. The output is supposed to be the following: tcp 0 0 127.0.0.23:9000 *:* CLOSE 1007/inetaton. Instead I get nothing. Netstat can't find the tcp socket. HERE IS THE CODE: :Pengy: //inetaton.cpp //EXAMPLE USING INET_ATON(3) #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> //THIS FUNCTION REPORTS THE ERROR AND EXITS BACK TO THE SHELL static void bail(const char *on_what) { fputs(on_what, stderr); fputc('\n', stderr); exit(1); } int main(int argc, char **argv) { int z; //STATUS RETURN struct sockaddr_in adr_inet; //AF_INET int len_inet; //LENGTH int sck_inet; //SOCKET //CREATE SOCKET sck_inet = socket(PF_INET, SOCK_STREAM, 0); if( sck_inet == -1 ) bail("socket()"); //ESTABLISH ADDRESS memset(&adr_inet, 0, sizeof adr_inet); adr_inet.sin_family = PF_INET; adr_inet.sin_port = htons(9000); if( !inet_aton("127.0.0.23", &adr_inet.sin_addr) ) bail("bad address"); len_inet = sizeof adr_inet; //BIND ADDRESS TO THE SOCKET z = bind(sck_inet, (struct sockaddr *)&adr_inet, len_inet); if( z == -1 ) bail("bind()"); //DISPLAY OUR SOCKET ADDRESS system("netstat -pa --tcp 2>/dev/null | grep inetaton"); printf("exiting program\n"); return 0; } |
you need to call listen() after bind() to put socket into LISTEN state.
|
thanks.... that worked
the output says: tcp 0 0 127.0.0.23:9000 *:* LISTEN 1007/inetaton. It there, perhaps, anyway to get the output to say close instead of listen? It's not necessary, but just for learning purposes: tcp 0 0 127.0.0.23:9000 *:* CLOSE 1007/inetaton. |
All times are GMT -5. The time now is 04:32 PM. |