LinuxQuestions.org
Visit Jeremy's Blog.
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 09-17-2004, 08:04 PM   #1
rnice
LQ Newbie
 
Registered: Sep 2004
Location: Maryland
Distribution: Red Hat 9
Posts: 13

Rep: Reputation: 0
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:

//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;
}
 
Old 09-17-2004, 11:17 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
you need to call listen() after bind() to put socket into LISTEN state.
 
Old 09-18-2004, 09:59 AM   #3
rnice
LQ Newbie
 
Registered: Sep 2004
Location: Maryland
Distribution: Red Hat 9
Posts: 13

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Please Help, Linux mysql nfs : Bind on unix socket: Function not implemented nmotte Linux - Newbie 1 10-29-2005 12:52 PM
Please Help, Linux mysql nfs : Bind on unix socket: Function not implemented nmotte Linux - Networking 1 10-29-2005 07:58 AM
Help me with this socket address bind program. rnice Linux - Networking 0 09-17-2004 07:33 PM
netstat, how to show what address are connected? trey85stang Slackware 2 12-30-2003 02:56 PM
netstat doesn't show tcp sockets in Redhat 7.2 pfpalmer Linux - General 3 10-31-2002 03:04 AM


All times are GMT -5. The time now is 05:11 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration