-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportScanner.cpp
More file actions
36 lines (30 loc) · 873 Bytes
/
Copy pathportScanner.cpp
File metadata and controls
36 lines (30 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
void scan_port(const char* ip, int port) {
int sock;
struct sockaddr_in sa;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
std::cerr << "Socket creation failed." << std::endl;
return;
}
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(ip);
sa.sin_port = htons(port);
int result = connect(sock, (struct sockaddr*)&sa, sizeof(sa));
if (result == 0) {
std::cout << "Port " << port << " is open." << std::endl;
} else {
std::cout << "Port " << port << " is closed." << std::endl;
}
close(sock);
}
int main() {
const char* ip = "127.0.0.1"; // Replace with the IP you want to scan
for (int port = 1; port <= 1024; ++port) {
scan_port(ip, port);
}
return 0;
}