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:
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/types.h>
#include <iostream>
#define PORT 13
#define BUFLEN 1024
using namespace std;
struct sockaddr_in s_add;
int suck_it, bind_me, r_con;
char* p_buff;
char buf[BUFLEN+1];
struct hostent* h_name;
int main(int argc, char *argv[]){
if(argc < 2){
cout << "Missing hostname" << endl;
exit(1);
}
h_name = gethostbyname(argv[1]);
if(!h_name){
cout << "couldn't resolve host name" << endl;
}
memset(&s_add, 0, sizeof(s_add));
s_add.sin_family = AF_INET;
s_add.sin_port = htons(PORT);
memcpy(&s_add.sin_addr.s_addr, h_name->h_addr_list[0], h_name->h_length);
suck_it = socket(AF_INET, SOCK_STREAM, 0);
if(suck_it<=-1){
perror("im your worst nightmare");
}
r_con = connect(suck_it, (struct sockaddr*) &s_add, sizeof(s_add));
if(r_con){
perror("connect");
}
p_buff = buf;
r_con = read(suck_it, p_buff, BUFLEN-(p_buff-buf));
while(r_con){
p_buff += r_con;
}
close(suck_it);
*p_buff = ' ';
cout << "Time: " << buf;
return 0;
}
Help please.