help pls..."connect: Connection refused"

Reply

Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

help pls..."connect: Connection refused"

 
0
  #1
May 30th, 2007
Hello there, I'm currently practicing network programming under Unix system. I have here a sample code from the tutorial. I compiled it, it has no errors. But when I execute it, it prompts me this "connect: Connection refused". What do you think is the problem. Here's my code:

  1. #include <netinet/in.h>
  2. #include <sys/socket.h>
  3. #include <netdb.h>
  4. #include <sys/types.h>
  5. #include <iostream>
  6. #define PORT 13
  7. #define BUFLEN 1024
  8.  
  9. using namespace std;
  10.  
  11. struct sockaddr_in s_add;
  12. int suck_it, bind_me, r_con;
  13. char* p_buff;
  14. char buf[BUFLEN+1];
  15. struct hostent* h_name;
  16. int main(int argc, char *argv[]){
  17. if(argc < 2){
  18. cout << "Missing hostname" << endl;
  19. exit(1);
  20. }
  21.  
  22. h_name = gethostbyname(argv[1]);
  23.  
  24. if(!h_name){
  25. cout << "couldn't resolve host name" << endl;
  26. }
  27.  
  28. memset(&s_add, 0, sizeof(s_add));
  29.  
  30. s_add.sin_family = AF_INET;
  31. s_add.sin_port = htons(PORT);
  32.  
  33. memcpy(&s_add.sin_addr.s_addr, h_name->h_addr_list[0], h_name->h_length);
  34.  
  35. suck_it = socket(AF_INET, SOCK_STREAM, 0);
  36.  
  37.  
  38. if(suck_it<=-1){
  39. perror("im your worst nightmare");
  40. }
  41.  
  42. r_con = connect(suck_it, (struct sockaddr*) &s_add, sizeof(s_add));
  43.  
  44. if(r_con){
  45. perror("connect");
  46. }
  47.  
  48. p_buff = buf;
  49.  
  50. r_con = read(suck_it, p_buff, BUFLEN-(p_buff-buf));
  51.  
  52. while(r_con){
  53. p_buff += r_con;
  54. }
  55.  
  56. close(suck_it);
  57.  
  58. *p_buff = ' ';
  59. cout << "Time: " << buf;
  60.  
  61. return 0;
  62. }

Help please.
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: help pls..."connect: Connection refused"

 
0
  #2
May 30th, 2007
my guess would be that whatever service you're expecting is not running on the server...
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

Re: help pls..."connect: Connection refused"

 
0
  #3
May 30th, 2007
that's my suspicion too...oh well, i'll just create a server...whatever server that is. lol
Retreat!!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2080 | Replies: 2
Thread Tools Search this Thread



Tag cloud for *nix Software
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC