Connect method fails
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Connect method fails




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Connect method fails  
Karim


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-25-06 12:20 AM

Hi,
I am trying to make a simple client/server aplication under unix. and
the connect() call always fails for some reason. The client and server
are running on the same machine. Connection to the server via telnet
works perfectly.

Here is a piece of my code:

int main (int argc,char * argv[])
{
int i, s, len;
struct sockaddr_in serverAddr = {0};
struct hostent* pHost =0;

struct hostent *host;
struct in_addr addr;
inet_aton("127.0.0.1", &addr);
pHost = gethostbyaddr(&addr, sizeof(addr), PF_INET);
//pHost =  gethostbyname("localhost") ; //this one fails too
if (!pHost)
{
fprintf(stderr,"pHost \n");
fflush(stderr);
return OK;
}

if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
fprintf(stderr,"socket \n");
fflush(stderr);
return OK;
}

serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
memcpy(&serverAddr.sin_addr.s_addr,pHost->h_addr_list[0],pHost->h_length
);

if (serverAddr.sin_addr.s_addr)
{
fprintf (stderr,"connect args: %d , %u , %lu
",s,serverAddr.sin_port,serverAddr.sin_addr.s_addr);
fflush(stderr);
}
else
{
fprintf(stderr,"its null\n");
fflush(stderr);
}

if (connect(s, (struct sockaddr *)&serverAddr,sizeof(serverAddr) ) ==
-1) {
fprintf(stderr,"connect failed again \n");
fflush(stderr);
return OK;

}

the output is always : connect args: 3 , 47635 , 16777343 connect
failed again


I tried using gethostByAddr for 127.0.0.1 and it also failed. Also I
tried gethostname and passing the result to gethostbyname and it also
failed.

Any advices are greately appreciated.

ps. this is a line from my ./etc/hosts

127.0.0.1               localhost.localdomain localhost

Thanks alot.






[ Post a follow-up to this message ]



    Re: Connect method fails  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-25-06 12:20 AM

In article <1167003180.859784.206760@i12g2000cwa.googlegroups.com>,
"Karim" <karim.elsaid@gmail.com> wrote:

> Hi,
> I am trying to make a simple client/server aplication under unix. and
> the connect() call always fails for some reason. The client and server
> are running on the same machine. Connection to the server via telnet
> works perfectly.
>
> Here is a piece of my code:
>
> int main (int argc,char * argv[])
> {
>     int i, s, len;
>     struct sockaddr_in serverAddr = {0};
>     struct hostent* pHost =0;
>
>     struct hostent *host;
>     struct in_addr addr;
>     inet_aton("127.0.0.1", &addr);
>     pHost = gethostbyaddr(&addr, sizeof(addr), PF_INET);
>     //pHost =  gethostbyname("localhost") ; //this one fails too
> if (!pHost)
> {
>       fprintf(stderr,"pHost \n");
>       fflush(stderr);
>      return OK;
> }
>
>    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
>           fprintf(stderr,"socket \n");
>          fflush(stderr);
>         return OK;
>    }
>
> serverAddr.sin_family = AF_INET;
> serverAddr.sin_port = htons(PORT);
> memcpy(&serverAddr.sin_addr.s_addr,pHost->h_addr_list[0],pHost->h_leng
th);
>
> if (serverAddr.sin_addr.s_addr)
> {
>     fprintf (stderr,"connect args: %d , %u , %lu
> ",s,serverAddr.sin_port,serverAddr.sin_addr.s_addr);
>     fflush(stderr);
> }
> else
> {
>        fprintf(stderr,"its null\n");
>       fflush(stderr);
> }
>
> if (connect(s, (struct sockaddr *)&serverAddr,sizeof(serverAddr) ) ==
> -1) {
>     fprintf(stderr,"connect failed again \n");
>     fflush(stderr);
>     return OK;
>
> }
>
> the output is always : connect args: 3 , 47635 , 16777343 connect
> failed again

What's in errno when it fails?  Why are you using fprintf() instead of
perror()?

BTW, you don't need to fflush() stderr, it defaults to unbuffered.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***





[ Post a follow-up to this message ]



    Re: Connect method fails  
Karim


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-26-06 06:37 AM


Barry Margolin wrote:
> In article <1167003180.859784.206760@i12g2000cwa.googlegroups.com>,
>  "Karim" <karim.elsaid@gmail.com> wrote:
> 
>
> What's in errno when it fails?  Why are you using fprintf() instead of
> perror()?
>
> BTW, you don't need to fflush() stderr, it defaults to unbuffered.
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***

I get errno 22. anyone knows what that means?

Thanks






[ Post a follow-up to this message ]



    Re: Connect method fails  
K-mart Cashier


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-26-06 06:37 AM


Karim wrote:
> Barry Margolin wrote: 
[vbcol=seagreen]
> I get errno 22. anyone knows what that means?
>
> Thanks

Let me go out on a limb here....

shouldn't
inet_aton("127.0.0.1", &addr);

actually be:
inet_aton("127.0.0.1", &addr.sin_addr);

Perhaps the best way to find out what errno is 22 is to go like:

perror("Error is: ");

If I remember right, perror() will return the error message
corresponding the the error number.

Chad






[ Post a follow-up to this message ]



    Re: Connect method fails  
Karim


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-26-06 06:37 AM


K-mart Cashier wrote:
> Karim wrote: 
> 
>
> Let me go out on a limb here....
>
> shouldn't
> inet_aton("127.0.0.1", &addr);
>
> actually be:
> inet_aton("127.0.0.1", &addr.sin_addr);
>
> Perhaps the best way to find out what errno is 22 is to go like:
>
> perror("Error is: ");
>
> If I remember right, perror() will return the error message
> corresponding the the error number.
>
> Chad

I tried this line
inet_aton("127.0.0.1", &addr.sin_addr); and the code still fails with
error 22 which means invalid args.






[ Post a follow-up to this message ]



    Re: Connect method fails  
yfyg


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-26-06 12:21 PM


"Karim =D0=B4=B5=C0=A3=BA
"
> Hi,
> I am trying to make a simple client/server aplication under unix. and
> the connect() call always fails for some reason. The client and server
> are running on the same machine. Connection to the server via telnet
> works perfectly.
>
> Here is a piece of my code:
>
> int main (int argc,char * argv[])
> {
>     int i, s, len;
>     struct sockaddr_in serverAddr =3D {0};
>     struct hostent* pHost =3D0;
>
>     struct hostent *host;
>     struct in_addr addr;
>     inet_aton("127.0.0.1", &addr);
>     pHost =3D gethostbyaddr(&addr, sizeof(addr), PF_INET);
>     //pHost =3D  gethostbyname("localhost") ; //this one fails too
> if (!pHost)
> {
>       fprintf(stderr,"pHost \n");
>       fflush(stderr);
>      return OK;
> }
>
>    if ((s =3D socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
>           fprintf(stderr,"socket \n");
>          fflush(stderr);
>         return OK;
>    }
>

The socket you create is an unix domain socket .

> serverAddr.sin_family =3D AF_INET;
> serverAddr.sin_port =3D htons(PORT);
> memcpy(&serverAddr.sin_addr.s_addr,pHost->h_addr_list[0],pHost->h_leng
th);
>

but you initialize the unix domian socket address as an  ipv4 protocol
socket address


> if (serverAddr.sin_addr.s_addr)
> {
>     fprintf (stderr,"connect args: %d , %u , %lu
> ",s,serverAddr.sin_port,serverAddr.sin_addr.s_addr);
>     fflush(stderr);
> }
> else
> {
>        fprintf(stderr,"its null\n");
>       fflush(stderr);
> }
>
> if (connect(s, (struct sockaddr *)&serverAddr,sizeof(serverAddr) ) =3D=3D
> -1) {
>     fprintf(stderr,"connect failed again \n");
>     fflush(stderr);
>     return OK;
>
> }
>
> the output is always : connect args: 3 , 47635 , 16777343 connect
> failed again
>
>
> I tried using gethostByAddr for 127.0.0.1 and it also failed. Also I
> tried gethostname and passing the result to gethostbyname and it also
> failed.
>
> Any advices are greately appreciated.
>
> ps. this is a line from my ./etc/hosts
>
> 127.0.0.1               localhost.localdomain localhost
>=20
> Thanks alot.






[ Post a follow-up to this message ]



    Re: Connect method fails  
Karim


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-27-06 12:20 AM


yfyg wrote:[vbcol=seagreen]
> "Karim =D0=B4=B5=C0=A3=BA
> " 
>
> The socket you create is an unix domain socket .
> 
h);[vbcol=seagreen] 
>
> but you initialize the unix domian socket address as an  ipv4 protocol
> socket address
>
> 
=3D[vbcol=seagreen] 


could you please illustrate more? could you supply the line of code for
initialization that i should use?

Thanks






[ Post a follow-up to this message ]



    Re: Connect method fails  
Karim


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-27-06 12:20 AM


Karim wrote:
> yfyg wrote: 
gth);[vbcol=seagreen] 
=3D=3D[vbcol=seagreen] 
>
>
> could you please illustrate more? could you supply the line of code for
> initialization that i should use?
>
> Thanks

I changed AF_UNIX for socket constructor to AF_INET and it worked.

Thanks alot.






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:31 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register