示例#1
0
文件: main.c 项目: cthrax/cs457
int main(int argc, char **argv) {
	char* port = NULL;
	int index;
	int c;
	packet_type type = INVALID_PACKET_TYPE;
	int retstatus = -1;

	// Turn off default error handling for getopt, return '?' instead
	opterr = 0;

	if (argc == 1) {
		fprintf(stderr, "Must specify port and packet type.\n");
		return 1;
	}

	while ((c = getopt(argc, argv, "p:t:")) != -1) {
		switch (c) {
			case 'p':
				if (parsePortNumber(LOWER_IP, UPPER_IP, optarg) == NULL) {
					fprintf(stderr, "Invalid port %s. Must use ephemeral or dynamic port within range of 49152-65535 so as to preven stepping on registered ports.\n", optarg);
					return 1;
				} else {
					port = optarg;
				}
				break;
			case 't':
				//Value ucp or tcp
				type = parsePacketType(optarg);
				break;
			case '?':
				if (optopt == 't') {
					fprintf(stderr, "Option -%c requires an argument. (tcp | udp)\n", optopt);
				} else if (optopt == 'p') {
					fprintf(stderr, "Option -%c requires an argument (-p <port number from 49152-65535>)\n", optopt);
				} else if (isprint(optopt)) {
					fprintf(stderr, "Unknown option `-%c'.\n", optopt);
				} else {
					fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
				}
				return 1;
			default:
				abort();
		}
	}

	for (index = optind; index < argc; index++) {
		printf("Non-option argument %s, ignoring.\n", argv[index]);
	}

	if (type == UDP) {
		retstatus = start_udp_server(port);
	} else if (type == TCP) {
		retstatus = start_tcp_server(port);
	} else {
		retstatus = 2;
		fprintf(stderr, "Invalid type, must be tcp or udp.");
	}

	return retstatus;
}
示例#2
0
main(int argc, char *argv[]){
    if (argc != 2) {
        printf("./a.out s/c \n");
        return 1;
    }
    if (argv[1][0]=='c'){
    	start_udp_client();
    }else{
    	start_udp_server();
    }

}