📄 tftptest.c
字号:
/* The beginnings of a tftp client (enough to use for grading P3) */#include <stdio.h> /* standard C i/o facilities */#include <stdlib.h> /* needed for atoi() */#include <unistd.h> /* defines STDIN_FILENO, system calls,etc */#include <sys/types.h> /* system data type definitions */#include <sys/socket.h> /* socket specific definitions */#include <netinet/in.h> /* INET constants and stuff */#include <arpa/inet.h> /* IP address conversion stuff */#include <netdb.h> /* gethostbyname */void tests(int,struct sockaddr_in *, int subtest);/* client program: The following must passed in on the command line: hostname of the server (argv[1]) port number of the server (argv[2])*/int main( int argc, char **argv ) { int sk, subtest; struct sockaddr_in server; struct hostent *hp; /* Make sure we have the right number of command line args */ if (argc < 3 || argc > 4) { printf ("Usage: %s <server name> <port number> [sub-test number (0/1/2/3)]\n",argv[0]); exit(0); } /* create a socket IP protocol family (PF_INET) UDP (SOCK_DGRAM) */ if ((sk = socket( PF_INET, SOCK_DGRAM, 0 )) < 0) { printf("Problem creating socket\n"); exit(1); } /* now create a sockaddr that will be used to contact the server fill in an address structure that will be used to specify the address of the server we want to connect to address family is IP (AF_INET) server IP address is found by calling gethostbyname with the name of the server (entered on the command line) server port number is argv[2] (entered on the command line) */ server.sin_family = AF_INET; if ((hp = gethostbyname(argv[1]))==0) { printf("Invalid or unknown host\n"); exit(1); } /* copy the IP address into the sockaddr It is already in network byte order */ memcpy( &server.sin_addr.s_addr, hp->h_addr, hp->h_length); /* establish the server port number - we must use network byte order! */ server.sin_port = htons(atoi(argv[2])); subtest = argc == 3 ? 0 : atoi(argv[3]); if (subtest < 0 || subtest > 3) { printf ("Usage: %s <server name> <port number> [sub-test number (0/1/2/3)]\n",argv[0]); exit(0); } tests(sk,&server,subtest); return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -