943,972 Members | Top Members by Rank

Ad:
May 30th, 2007
0

help pls..."connect: Connection refused"

Expand Post »
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:

c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 32
Solved Threads: 4
Practically a Master Poster
jaepi is offline Offline
647 posts
since Jul 2006
May 30th, 2007
0

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

my guess would be that whatever service you're expecting is not running on the server...
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
May 30th, 2007
0

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

that's my suspicion too...oh well, i'll just create a server...whatever server that is. lol
Reputation Points: 32
Solved Threads: 4
Practically a Master Poster
jaepi is offline Offline
647 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Linux Applications and Software Forum Timeline: [Samba] How do I set specific UIDs for users?
Next Thread in Linux Applications and Software Forum Timeline: Email ticketing software





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC