#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
int SERVER_PORT= 1;
#define SERVER_IP "127.0.0.1"
#define WRITE_BUF_SIZE 1024
#define SUCCESS 0
#define FAILURE -1
int main(int argc, char **argv)
{
int sfd;
int iReadNum;
char acBuf[WRITE_BUF_SIZE];
struct sockaddr_in saddr;
int socklen;
int iClientLen;
int iReturn;
if(argc!=2)
{
printf("usage:./a.out 127.0.0.1\n");
return FAILURE;
}
printf("scanning port 1-8888...... \n");
while(SERVER_PORT<8889)
{
//socket_init
sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0)
{
perror("socket()");
return FAILURE;
}
//套接口设置
saddr.sin_family = AF_INET;
//inet_pton将点分十进制--》二进制整数
inet_pton(AF_INET, argv[1], &saddr.sin_addr);
saddr.sin_port = htons(SERVER_PORT);
socklen = sizeof(saddr);
//尝试连接自己的地址以及端口
iReturn = connect(sfd, (const struct sockaddr *)&saddr, socklen);
if(iReturn >= 0)
{
sleep(1);
printf("port %d open\n",SERVER_PORT);
}
close(sfd);
SERVER_PORT++;
}
return SUCCESS;
}