이거보다 짧은거 있으면..

#include
#include
#include
#include
#include
#include
#include

int port_scan(char *hostaddr, int cport);
int main(int argc, char *argv[])
{
int cport; // 포트 루프용 변수
if (argc < 2) {
printf("사용 방법 : ./portscan 호스트 IP ");
exit(1);
}
// 1부터 1023번 포트까지 체크 반복합니다.
for (cport = 1;cport < 1024;cport++) {
if (port_scan(argv[1], cport) == 0) {
printf("%5d open ", cport);
}
}
return 0;
}
int port_scan(char *hostaddr, int cport)
{
int sockfd; // 소켓 기술자
struct sockaddr_in destaddr; // 소켓 주소 구조체
int pok;
// 소켓 기술자 초기화
sockfd = socket(AF_INET, SOCK_STREAM, 0);
destaddr.sin_family = AF_INET;
destaddr.sin_addr.s_addr = inet_addr(hostaddr);
destaddr.sin_port = htons(cport);
bzero(&(destaddr.sin_zero), 8);

pok = connect(sockfd, (struct sockaddr *)&destaddr, sizeof(struct sockaddr));
close(sockfd);
// 연결 되면 0을, 아니면 -1을 리턴
if (pok == -1)
return -1;
return 0;
}
컴파일 할땐..: # gcc -o portscan portscan.c

실행 할시..: # ./portscan 호스트 IP
2009/06/18 17:39 2009/06/18 17:39

Trackback Address :: https://youngsam.net/trackback/526