#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <iostream>
#include <algorithm>
using namespace std;
char *strset(char *string,int strlen,char chr){
int pt;
for(pt=0;pt<strlen;pt++)string[pt]=chr;
}
int main (int argc,char *argv[])
{
int sockfd;
char buffer[32];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber=0,portend=0xffff,nbytes,j;
if(argc==1){
fprintf(stderr,"Usage:%s <-i|--iplist> [-p|--portlist]\a\n",argv[0]);
exit(1);
}
if(argc!=1) for(j=1;j<argc;j++){
if(!strcmp(argv[j],"-i")||!strcmp(argv[j],"--iplist")){
host=gethostbyname(argv[j+1]);
}
if(!strcmp(argv[j],"-p")||!strcmp(argv[j],"--portlist")){
portnumber=atoi(argv[j+1]);
portend=atoi(argv[j+1]);
}
}
for(;portnumber<=portend;portnumber++){
/*create socket*/
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
fprintf(stderr,"Create socket error:%s \a\n",strerror(errno));
exit(1);
}
/*create sockaddr_in*/
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnumber);
server_addr.sin_addr=*((struct in_addr *)host->h_addr);
/*connect to host*/
//fprintf(stderr,"%d|",ntohs(server_addr.sin_port));
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1){
//fprintf(stderr,"Connect Error:%s \a\n",strerror(errno));
//exit(1);
}
/*Read data from host*/
else {
printf("%s#%d>>>: ",inet_ntoa(server_addr.sin_addr),ntohs(server_addr.sin_port));
sleep(1);
strset(buffer,32,' ');
fcntl(sockfd,F_SETFL,O_NONBLOCK);
if((nbytes=read(sockfd,buffer,32))==-1){
//fprintf(stderr,"Read Error:%s \a\n",strerror(errno));
//exit(1);
printf("\a\n");
}else{
buffer[nbytes]='\0';
printf("%s",buffer);
if(!strchr(buffer,'\n'))printf("\a\n");
}
}
close(sockfd);
}
exit(0);
return 0;
}
kaelzhan
- 粉丝: 6
- 资源: 6
最新资源
- C语言-leetcode题解之70-climbing-stairs.c
- C语言-leetcode题解之68-text-justification.c
- C语言-leetcode题解之66-plus-one.c
- C语言-leetcode题解之64-minimum-path-sum.c
- C语言-leetcode题解之63-unique-paths-ii.c
- C语言-leetcode题解之62-unique-paths.c
- C语言-leetcode题解之61-rotate-list.c
- C语言-leetcode题解之59-spiral-matrix-ii.c
- C语言-leetcode题解之58-length-of-last-word.c
- 计算机编程课程设计基础教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈