- 论坛徽章:
- 0
|
在读取文本文件时遇到的怪问题
//这是我编的完整的程序,用于测试指定机器端口的打开情况,大家来找找这个//问题,我用的是sco,cc编译器
#include<stdio.h>;
#include<string.h>;
#include <sys/types.h>;
#include <sys/socket.h>;
#include <netinet/in.h>;
#include <errno.h>;
#include <netdb.h>;
#include <signal.h>;
struct def
{char offinum[10];
char ip[20];
char port[5];
char type[2];
}filedef[100];
int port_scan(struct def *p)
{
int probeport = 0;
struct hostent *host;
int err, net;
struct sockaddr_in sa;
strncpy((char *)&sa,"",sizeof sa);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(p->;ip);
sa.sin_port = htons(atoi(p->;port));
net = socket(AF_INET,SOCK_STREAM,0);
if(net<0)
{
perror("\nsocket" ;
exit(2);
}
err = connect(net,(struct sockaddr *) &sa,sizeof sa);
if(err<0)
{
printf("\r" ;
fflush(stdout);
printf("%s %s %s %s refused. \n",p->;offinum,p->;ip,p->;port,p->;type);
}
else
{
printf("%s %s %s %s accept. \n",*p->;offinum,p->;ip,p->;port,p->;type);
if(shutdown(net,2)<0)
{
perror("\nshutdown" ;
exit(2);
}
}
close(net);
//}
printf("\r" ;
fflush(stdout);
return(0);
}
int readconf()
{
FILE *fp=NULL;
int num=0;
char *p;
char buf[255];
char filename[40];
sprintf(filename,"/study/study2/checkport" ;
if ((fp=fopen(filename,"r" )==NULL)
{ printf("read error" ;
return -1;
};
while((p=fgets(buf,255,fp))!=NULL)
{
sscanf(buf,"%s %s %s %s",filedef[num].offinum,filedef[num].ip,filedef[num].port,filedef[num].type);
printf("%s %s %s %s",filedef[num].offinum,filedef[num].ip,filedef[num].port,filedef[num].type);
num++;
}
fclose(fp);
return num;
}
void main()
{
int num,i;
num=readconf();
if (num==-1)
exit(0);
for(i=0;i<num;i++)
port_scan(&filedef);
}
/*checkport 的内容为:
jia 172.16.9.40 7007 T
lei 172.16.9.42 7007 T*/ |
|