Hello.
I am writing my first program with sockets and I am trying to write a client
that uses the whois tcp/ip service.
But after connect (last line), desc=-1 and I get “Error: Connection refused”.
I am using QNX 6.2.1 SE, inetd is running and the service whois in uncommented.
What could be happend?
If I change the line sp= getservbyname(“whois”,“tcp”); to
sp= getservbyname(“ftp”,“tcp”), I get no error.
Thank you
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int s;
int desc;
int len;
struct sockaddr_in sa;
struct hostent *hp;
struct servent *sp;
char buf[BUFSIZ+1];
char *myname;
char *host;
char *user;
myname= argv[0];
host= argv[1]; // NAME OF THE HOST (1st argument)
user= argv[2]; // NAME OF THE USER (2nd argument)
hp= gethostbyname(host);
strncpy( &sa.sin_addr, (hp->h_addr), hp->h_length);
sa.sin_family= hp->h_addrtype;
sp= getservbyname("whois","tcp");
sa.sin_port= sp->s_port;
s= socket(hp->h_addrtype, SOCK_STREAM, 0);
desc= connect(s, (struct sockaddr *) &sa, sizeof(sa));
}