A Remote Login Client (Internet Domain, in C)
Most operating systems, supporting internetworking using the Berkeley
sockets API, also provide many functions to facilitate access to many
commonly required resources - such as hostnames, protocol numbers, service
numbers, etc.
In an environment where many computers require access to consistent data,
these API functions, themselves, may be configured to seek their
information via the Internet.
#include <many-header-files.h>
int main(int argc, char *argv[])
{
// LOCATE INFORMATION ABOUT THE REQUIRED SERVICE (PROTOCOL AND PORT)
struct servent *sp = getservbyname("login", "tcp");
if(sp == NULL) {
fprintf(stderr,"rlogin: tcp/login: unknown service\n");
exit(1);
}
// LOCATE INFORMATION ABOUT THE REQUIRED HOST (ITS IP ADDRESS)
struct hostent *hp = gethostbyname(argv[1]);
if(hp == NULL) {
fprintf(stderr,"rlogin: %s: unknown host\n",argv[1]);
exit(2);
}
// ASK OUR OS KERNEL TO ALLOCATE RESOURCES FOR A SOCKET
int sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd < 0) {
perror("rlogin: socket");
exit(3);
}
// INITIALIZE FILEDS OF A STRUCTURE USED TO CONTACT SERVER
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = sp->s_port;
// CONNECT TO SERVER
if(connect(sd, &server, sizeof(server)) < 0) {
perror("rlogin: connect");
exit(4);
}
communicate_with_rlogin_server(sd);
shutdown(sd, SHUT_RDWR);
close(sd);
exit(EXIT_SUCCESS);
}
|
CITS3002 Computer Networks, Lecture 8, Transport layer protocols and APIs, p17, 24th April 2024.
|