Java
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class PortScanner {
private String ip;
private int startPort;
private int endPort;
private ExecutorService executorService;
public PortScanner(String ip, int startPort, int endPort) {
this.ip = ip;
this.startPort = startPort;
this.endPort = endPort;
this.executorService = Executors.newCachedThreadPool();
}
public void startScan() {
for (int port = startPort; port <= endPort; port++) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), 1000);
System.out.println("端口 " + port + " 已打开");
socket.close();
} catch (Exception e) {
System.out.println("端口 " + port + " 未打开");
}
}
};
executorService.execute(runnable);
}
}
public static void main(String[] args) {
PortScanner portScanner = new PortScanner("192.168.1.1", 1, 65535);
portScanner.startScan();
}
}
C
#include
#include
#include
#include
#include
#define PORT_SRC 8000
#define PORT_DEST 8001
int main()
{
int sock_src, sock_dest, new_sock;
int optval = 1;
char buffer[1024] = {0};
struct sockaddr_in src_addr, dest_addr;
int addr_len = sizeof(src_addr);
if ((sock_src = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
src_addr.sin_family = AF_INET;
src_addr.sin_addr.s_addr = INADDR_ANY;
src_addr.sin_port = htons(PORT_SRC);
setsockopt(sock_src, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &optval, sizeof(optval));
if (bind(sock_src, (struct sockaddr *)&src_addr, sizeof(src_addr))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(sock_src, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((sock_dest = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
dest_addr.sin_family = AF_INET;
dest_addr.sin_addr.s_addr = INADDR_ANY;
dest_addr.sin_port = htons(PORT_DEST);
setsockopt(sock_dest, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &optval, sizeof(optval));
if (bind(sock_dest, (struct sockaddr *)&dest_addr, sizeof(dest_addr))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(sock_dest, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_sock = accept(sock_src, (struct sockaddr *)&src_addr,
(socklen_t*)&addr_len))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
while (1)
{
int read_size = read(new_sock, buffer, 1024);
if (read_size == 0)
{
printf("Connection closed\n");
close(new_sock);
break;
}
else
{
send(sock_dest, buffer, read_size, 0);
memset(buffer, 0, 1024);
}
}
return 0;
}
python
import socket
import threading
import time
def port_scan(host, port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
print('[+] ', port, ' open')
s.close()
except:
pass
def main():
host = '127.0.0.1'
port_range = range(1, 65535)
for port in port_range:
t = threading.Thread(target=port_scan, args=(host, port))
t.start()
time.sleep(0.1)
if __name__ == '__main__':
main()
go
package main
import (
"fmt"
"net"
)
func main() {
ip := "192.168.1.1"
for port := 1; port <= 65535; port++ {
address := fmt.Sprintf("%s:%d", ip, port)
conn, err := net.Dial("tcp", address)
if err == nil {
fmt.Printf("%d open\n", port)
conn.Close()
}
}
}