[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
int main(int argc, char **argv)
{
struct servent *myserv;
int port;
if (argc < 3) {
puts("Incorrect parameters. Use:");
printf("%s service-port protocol-name", argv[0]);
return EXIT_FAILURE;
}
port = htons(atoi(argv[1]));
if (strcmp(argv[2], "NULL") == 0) {
myserv = getservbyport(port, NULL);
if (myserv == NULL) {
printf("Port \"%s\" not found for protocol \"%s\"\n", argv[1], argv[2]);
exit(EXIT_FAILURE);
}
printf ("Name: %-15s Port: %5d Protocol: %-6s\n", myserv->s_name, ntohs(myserv->s_port), myserv->s_proto);
}
else {
myserv = getservbyport(port, argv[2]);
if (myserv == NULL) {
printf("Port \"%s\" not found for protocol \"%s\"\n", argv[1], argv[2]);
exit(EXIT_FAILURE);
}
printf ("Name: %-15s Port: %5d Protocol: %-6s\n", myserv->s_name, ntohs(myserv->s_port), myserv->s_proto);
}
return 0;
}