The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Bad file descriptor upon connect
Discuss Bad file descriptor upon connect in the C Programming forum on Dev Shed. Bad file descriptor upon connect C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 28th, 2009, 05:52 PM
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 11
Time spent in forums: 2 h 24 m 28 sec
Reputation Power: 0
|
|
Bad file descriptor upon connect
Hello There,
I'm trying to establish a socket connection to a hostname using following code:
Code:
struct hostent *he;
struct sockaddr_in server;
int sockfd;
/* resolve localhost to an IP (should be 127.0.0.1) */
if ((he = gethostbyname("nems")) == NULL) {
puts("error resolving hostname..");
exit(1);
}
/*
* copy the network address part of the structure to the
* sockaddr_in structure which is passed to connect()
*/
memcpy(&server.sin_addr, he->h_addr_list[0], he->h_length);
server.sin_family = AF_INET;
server.sin_port = htons(1514);
/* connect */
if (connect(sockfd, (struct sockaddr *)&server, sizeof(server))) {
printf("error connecting...exit,errno: %s!\n", strerror( errno ));
exit(1);
}
But the problem is, that i get a
error connecting...exit,errno: Bad file descriptor!
and I'm not sure why...  I can succesfully connect using telnet nems 1514
Any hints what my problem may be?
Thank you!
--
roN
|

December 28th, 2009, 06:12 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
You have to create a socket() before you can call connect() on it.
Code:
struct protoent *pr;
pr = getprotobyname("tcp");
if (!pr) {
/* add code to crib about it here */
}
if ( (sockfd = socket(PF_INET, SOCK_STREAM, pr->p_proto)) == -1) {
/* Add more code here */
}
/* Call connect() here */
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

December 29th, 2009, 10:39 AM
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 11
Time spent in forums: 2 h 24 m 28 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Scorpions4ever You have to create a socket() before you can call connect() on it.
Code:
struct protoent *pr;
pr = getprotobyname("tcp");
if (!pr) {
/* add code to crib about it here */
}
if ( (sockfd = socket(PF_INET, SOCK_STREAM, pr->p_proto)) == -1) {
/* Add more code here */
}
/* Call connect() here */
|
edit it actually works now to establish a connection.
Thanks for that! However I can not send of any strings, my code to send looks like this:
Code:
if (m_sock>-1) {
int len=send(m_sock, res, strlen(res), 0);
if (len != strlen(res))
printf("nlog: \"%s\" not sent, strlen(%d) - sent %d\n",res, strlen(res), len);
else
printf("sent %s\n-------------\n", res);
} else
printf("nlog: m_sock:%d",m_sock);
edit
And what I'm getting is that the correct amount of bytes was sent but I don't see anything appearing on the server side and again, if i sent data after establishing a telnet connection telnet nems 1514 it works just fine....
any clues on this please?
Thanks!
Ron
|

December 29th, 2009, 12:29 PM
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 11
Time spent in forums: 2 h 24 m 28 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by cerr edit it actually works now to establish a connection.
Thanks for that! However I can not send of any strings, my code to send looks like this:
Code:
if (m_sock>-1) {
int len=send(m_sock, res, strlen(res), 0);
if (len != strlen(res))
printf("nlog: \"%s\" not sent, strlen(%d) - sent %d\n",res, strlen(res), len);
else
printf("sent %s\n-------------\n", res);
} else
printf("nlog: m_sock:%d",m_sock);
edit
And what I'm getting is that the correct amount of bytes was sent but I don't see anything appearing on the server side and again, if i sent data after establishing a telnet connection telnet nems 1514 it works just fine....
any clues on this please?
Thanks!
Ron |
Ah, got it! I required to strcat() a couple on newlines to the end of my string! Sweet! Awesome! Thanks for your help! 
|

December 29th, 2009, 12:29 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Show us your server side code.
By the way, after your last edit, is it showing that it has successfully sent the data or not? Make sure you're sending the exact characters you're sending in the telnet session (including the \n)
[edit]Looks like you and I posted about the same time. Looks like it was the \n characters  . [/edit]
Last edited by Scorpions4ever : December 29th, 2009 at 12:59 PM.
|

December 29th, 2009, 12:57 PM
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 11
Time spent in forums: 2 h 24 m 28 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Scorpions4ever Show us your server side code.
By the way, after your last edit, is it showing that it has successfully sent the data or not? Make sure you're sending the exact characters you're sending in the telnet session (including the \n) |
Exaactly, the \n was the problem! I unfortunately don't have the server side code handy currently...
Uhm anyways, I would receive LOGOK back and i try to read it like this:
Code:
reclen=send(m_sock, res, sndlen, 0);
//for(s=0;s<10000000;s++);
if (reclen != sndlen) {
if (reclen==-1)
printf("send failed...,errno: %s!\n", strerror( errno ));
printf("nlog: \"%s\" not sent, strlen(%d) - sent %d\n",res, sndlen, reclen);
}
else
printf("sent %s-------------\n", res);
if(recv(m_sock, recbuf, sizeof(recbuf), 0) > -1)
printf("RECEIVED:%s--------------\n",recbuf);
else
printf("recv failed...,errno: %s!\n", strerror( errno ));
but my code seems to be hanging in recv. I never see RECEIVED nor recv failed. What may be the problem here?
Thanks!
Ron
|

December 29th, 2009, 01:17 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Make sure you're sending exactly the # of bytes that were sent by your telnet session. The server may not be sending a response back to you. You could use select() to check if there are any messages in the buffer before you call recv().
Try changing recv() to recv only one byte at a time and see if it returns then. Maybe the other end isn't closing the connection or something. Hard to say without seeing the server code.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting 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
|
|
|
|
|