|
Home > Archive > Unix Programming > December 2006 > Connect method fails
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 |
Connect method fails
|
|
|
| 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.
| |
| Barry Margolin 2006-12-24, 7:20 pm |
| 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_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
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 ***
| |
|
|
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
| |
| K-mart Cashier 2006-12-26, 1: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
| |
|
|
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.
| |
|
|
"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_length);
>
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.
| |
|
|
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
| |
|
|
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.
|
|
|
|
|