Unix Programming - Error in bind() after new

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2004 > Error in bind() after new





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Error in bind() after new
raju321-nospam@yahoo.com

2004-10-02, 9:13 pm

Hi,
I'm writing this simple program on a Linux machine using a g++ version
3.3.3 20040412 (Red Hat Linux 3.3.3-7).
[snip]

#include <iostream>
#include <string>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
using namespace std;
class obj1 {
int *p;
int sockfd;
sockaddr_in adr;
public :
obj1();
int createsock();
};

obj1::obj1() {
p=new int[2];
cout<<"port:"<<createsock();
delete [] p;
}
int obj1::createsock() {
int port1=-1;
// int reuse_addr=1;
memset((void *)&adr,0,sizeof(sockaddr_in));

adr.sin_family = AF_INET; // family
adr.sin_addr.s_addr = htonl(INADDR_ANY);// pick the current IP
address
adr.sin_port = htons(0); // pick any available port
//memset(&(adr.sin_zero), '\0', 8);

socklen_t dummyLen;
// lets start socket prging
sockfd=socket (AF_INET,SOCK_STREAM,0); // creates a socket
if(sockfd==-1)
cout<<"Problem creating socket";
if(bind (sockfd,(sockaddr *)(&adr),sizeof(sockaddr))<0)
cout<<"Error binding on socket";
if(getsockname(sockfd,(sockaddr *)(&adr),&dummyLen)<0)
cout<<"Couldnt retrieve port no";
if(listen(sockfd,2)==-1)
cout<<"Problem in listening"; // start listening man
port1=ntohs(adr.sin_port); // Set the port
return port1; // return the port! phew!
}
main()
{
obj1 n;
}
[/snip]
When I compile and run it I get

-- output------------
Couldnt retrieve port noport:0
-- end of output ----

(Error signaled is 12)

1. The funny thing is if I make the two functions as a single function
it works!
2. Also, if I remove the new and delete it works! I don't understand
how this is happening?
3. If I modify the "cout<<"port:"<<createsock();" line to
cout<<"port:"<<createsock()<<endl; Then again I get the port assigned
properly.


Why is this behavior so erratic? What mistakes am I doing?
Please help!
Thanks,
Rajat.
James Antill

2004-10-02, 9:13 pm

On Fri, 01 Oct 2004 23:55:25 -0700, raju321-nospam@yahoo.com wrote:

> Hi,
> I'm writing this simple program on a Linux machine using a g++ version
> 3.3.3 20040412 (Red Hat Linux 3.3.3-7).
> [snip]
> socklen_t dummyLen;
> // lets start socket prging
> sockfd=socket (AF_INET,SOCK_STREAM,0); // creates a socket
> if(sockfd==-1)
> cout<<"Problem creating socket";
> if(bind (sockfd,(sockaddr *)(&adr),sizeof(sockaddr))<0)
> cout<<"Error binding on socket";
> if(getsockname(sockfd,(sockaddr *)(&adr),&dummyLen)<0)
> cout<<"Couldnt retrieve port no";


This is a common mistake with many socket calls (usually accept),
"dummyLen" is both an input and an output. So you want something like...

dummyLen = sizeof(struct sockaddr_in);
if(getsockname(sockfd,(sockaddr *)(&adr),&dummyLen)<0)
cout<<"Couldnt retrieve port no";

--
James Antill -- james@and.org
Need an efficient and powerful string library for C?
http://www.and.org/vstr/

raju321-nospam@yahoo.com

2004-10-03, 7:47 am

On 2004-10-02 15:47:32 PST James Antill wrote -
> This is a common mistake with many socket calls (usually accept),
> "dummyLen" is both an input and an output. So you want something like...
>
> dummyLen = sizeof(struct sockaddr_in);
> if(getsockname(sockfd,(sockaddr *)(&adr),&dummyLen)<0)
> cout<<"Couldnt retrieve port no";


Thanks James your solution was spot on!

-Rajat.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com