- 论坛徽章:
- 0
|
大家好,我写了一个用于扫描主机端口的socket程序,编译可以通过,但是运行的时候有问题,socket创建不了,提示如下:请问应该怎么解决?
错误信息:
error :create socket!!!
: Invalid or incomplete multibyte or wide character- #include <stdio.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <errno.h>
- #include <netdb.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <errno.h>
- #include <sys/ioctl.h>
- #include <net/if.h>
- #include <netinet/ip.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include <netinet/tcp.h>
- #include <pthread.h>
- #include <signal.h>
- void scan(char *ip,int minport,int maxport)
- {
- printf("您输入的信息是:ip:%s,port:%d,port:%d\n",ip,minport,maxport);
- int i = 0;
- printf("开始扫描.......操\n");
- for(i=minport;i<maxport;i++)
- {
- struct sockaddr_in clientaddr;
- struct hostent *pDest;
- clientaddr.sin_family = AF_INET;
- clientaddr.sin_addr.s_addr = inet_addr(ip);
- clientaddr.sin_port = htons(i);
- int net = socket(AF_INET,SOCK_STREAM,0);
- printf("%d,\n",net);
- if ( net < 0 );
- {
- perror("error :create socket!!!\n");
- return;
- }
- int err = connect( net,(struct sockaddr*)&clientaddr,sizeof(clientaddr) );
- if (err < 0)
- {
- printf("端口:%5d | 状态:关闭!!!\n",i);
- fflush(stdout);
- }
- else
- {
- struct servent* sptr;
- if((sptr=getservbyport(htons(i),"tcp"))!=NULL)
- {
- printf("端口:%5d | 服务:%s | 状态:open!!!\n",i,sptr->s_name);
- }
- else
- printf("端口:%5d | 状态:开启!!!\n",i);
- }
- close(net);
- }
- }
- int main(int argc,char **argv)
- {
- while(1)
- {
- printf("请输入相应的指令!");
- char command[20];
- scanf("%s",&command);
-
- if(strcmp(command,"scan")==0)
- {
- printf("please input the ip ");
- char ip[20];
- scanf("%s",ip);
-
- printf("please input the minport ");
- int minport;
- scanf("%d",&minport);
- printf("please input the maxport ");
- int maxport;
- scanf("%d",&maxport);
- scan(ip,minport,maxport);
- }
-
- else
- {
- printf("您输入的格式误\n");
- }
- }
- }
复制代码 |
|