LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2004, 08:33 PM   #1
rnice
LQ Newbie
 
Registered: Sep 2004
Location: Maryland
Distribution: Red Hat 9
Posts: 13

Rep: Reputation: 0
Help me with this socket address bind program.


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;
}
 
  


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
Could not bind listening socket chabotrobert Linux - Networking 7 02-25-2024 05:19 PM
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:443 bruvajon Linux - Software 34 11-30-2011 04:08 PM
"unable to bind socket - Address already in use" engineerwell Linux - General 7 05-12-2010 03:46 AM
cannot bind to socket: address already in use exfacior Linux - Wireless Networking 0 09-27-2004 01:34 PM
Linux Socket Bind Program Won't show with netstat rnice Programming 2 09-18-2004 10:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 07:35 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration