#include
<stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
int
inet_connect(char *host, int port) {
int s;
struct sockaddr_in addr;
struct hostent *hostp;
if(NULL == (hostp = gethostbyname(host))) {
fprintf(stderr, "Error looking up host %s.\n", host);
exit(2);
}
memset(&addr,0,sizeof(addr));
memcpy((char *)&addr.sin_addr,hostp->h_addr,hostp->h_length);
addr.sin_family = hostp->h_addrtype;
addr.sin_port = htons((u_short)port);
if((s=socket(PF_INET, SOCK_STREAM, 6)) < 0) {
fprintf(stderr, "Error creating socket.\n");
exit(3);
}
if(connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
{
close(s);
fprintf(stderr, "Error connecting to %s:%d\n", host, port);
exit(4);
}
return s;
}
/* This
function returns a file descriptor suitable for read() and
write(). fdopen() will convert it into a FILE * for use with fgets(),
fprintf(), etc. */
|