#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MAXLINE 80
int main(void)
{
struct sockaddr_in servaddr;
int sockfd,i,serverport,ret;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
for(i = 0;i < 65535;i++)
{
servaddr.sin_port = htons(i);
ret = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
if(EISCONN == ret)
{
printf("端口 %d被占用",i);
}
else
{
close(sockfd);
}
}
return 0;
}
https://bbs.csdn.net/topics/392031261?page=1