免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2695 | 回复: 3
打印 上一主题 下一主题

请教一socket扫描主机端口的问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-10-21 10:25 |只看该作者 |倒序浏览
大家好,我写了一个用于扫描主机端口的socket程序,编译可以通过,但是运行的时候有问题,socket创建不了,提示如下:请问应该怎么解决?
错误信息:
error :create socket!!!
: Invalid or incomplete multibyte or wide character
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <errno.h>
  5. #include <netdb.h>  
  6. #include <sys/types.h>   
  7. #include <stdio.h>   
  8. #include <errno.h>   
  9. #include <sys/ioctl.h>   
  10. #include <net/if.h>   

  11. #include <netinet/ip.h>   
  12. #include <netinet/in.h>   
  13. #include <string.h>   
  14. #include <arpa/inet.h>   
  15. #include <netinet/tcp.h>   
  16. #include <pthread.h>     
  17. #include <signal.h>

  18. void scan(char *ip,int minport,int maxport)
  19. {
  20.         printf("您输入的信息是:ip:%s,port:%d,port:%d\n",ip,minport,maxport);

  21.         int i = 0;
  22.         printf("开始扫描.......操\n");
  23.         for(i=minport;i<maxport;i++)
  24.         {
  25.         struct sockaddr_in clientaddr;
  26.         struct hostent *pDest;
  27.         clientaddr.sin_family = AF_INET;
  28.         clientaddr.sin_addr.s_addr = inet_addr(ip);
  29.         clientaddr.sin_port = htons(i);

  30.         int net = socket(AF_INET,SOCK_STREAM,0);

  31.         printf("%d,\n",net);
  32.         if ( net < 0 );
  33.         {
  34.         perror("error :create socket!!!\n");
  35.         return;
  36.         }

  37.         int err = connect( net,(struct sockaddr*)&clientaddr,sizeof(clientaddr) );
  38.         if (err < 0)
  39.         {
  40.         printf("端口:%5d | 状态:关闭!!!\n",i);
  41.         fflush(stdout);
  42.         }

  43.         else
  44.         {
  45.         struct servent* sptr;
  46.         if((sptr=getservbyport(htons(i),"tcp"))!=NULL)
  47.         {
  48.         printf("端口:%5d | 服务:%s | 状态:open!!!\n",i,sptr->s_name);
  49.         }
  50.         else
  51.                 printf("端口:%5d | 状态:开启!!!\n",i);


  52.         }
  53.         close(net);
  54.     }
  55. }

  56. int main(int argc,char **argv)
  57. {
  58.         while(1)
  59.         {
  60.         printf("请输入相应的指令!");
  61.         char command[20];
  62.         scanf("%s",&command);
  63.        
  64.         if(strcmp(command,"scan")==0)
  65.         {
  66.         printf("please input the ip ");
  67.         char ip[20];
  68.         scanf("%s",ip);
  69.        
  70.         printf("please input the minport ");
  71.         int minport;
  72.         scanf("%d",&minport);

  73.         printf("please input the maxport ");
  74.         int maxport;
  75.         scanf("%d",&maxport);

  76.         scan(ip,minport,maxport);
  77.         }
  78.        
  79.         else
  80.         {
  81.                 printf("您输入的格式误\n");
  82.         }

  83. }




  84. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2010-10-21 11:01 |只看该作者
没人帮忙吗?

论坛徽章:
0
3 [报告]
发表于 2010-10-21 11:39 |只看该作者
回复 1# killerezero


你的源代码37行多了一个分号。
这种bug很折磨人滴,要有耐心。

论坛徽章:
0
4 [报告]
发表于 2010-10-21 11:48 |只看该作者
本帖最后由 雨夜流星 于 2010-10-21 11:50 编辑

这不是提示已经很明显了么
error :create socket!!!
# if ( net < 0 );
#         {
#         perror("error :create socket!!!\n");
#         return;
#         }

二楼眼神不错,一下就看到了,呵呵
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP