Use the is_numeric function to check if optarg contains only digits before calling atoi; if not, print "port must be a valid positive number" and exit. This prevents segmentation faults by ensuring optarg is valid before converting it to an integer.