C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 28th, 2009, 05:52 PM
cerr cerr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 11 cerr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 24 m 28 sec
Reputation Power: 0
Question 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

Reply With Quote
  #2  
Old December 28th, 2009, 06:12 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is online now
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 32 m 40 sec
Reputation Power: 4080
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

Reply With Quote
  #3  
Old December 29th, 2009, 10:39 AM
cerr cerr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 11 cerr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old December 29th, 2009, 12:29 PM
cerr cerr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 11 cerr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #5  
Old December 29th, 2009, 12:29 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is online now
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 32 m 40 sec
Reputation Power: 4080
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.

Reply With Quote
  #6  
Old December 29th, 2009, 12:57 PM
cerr cerr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 11 cerr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #7  
Old December 29th, 2009, 01:17 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is online now
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 32 m 40 sec
Reputation Power: 4080
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Bad file descriptor upon connect

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap