ノンブロッキング接続を実行し、選択を実行して、ソケットが書き込み可能か例外中かを確認することで、ネットワーク上のホストを確認しようとしています。ポート 80,139 経由でソケット接続を確立しようとしています。ホストソケットが接続後に書き込み可能である場合、またはホストが RST パケットを送信したときにソケットが例外になった場合に検出可能になります。
Windows ソケットを使用してコードを記述し、ロジックは正常に機能しましたが、Linux ソケットを使用すると、プログラムは目的の結果を与えません。選択関数は、その IP にホストがなくても、特定の IP アドレスに対して 1 を返します。 Winsock のケースでタイムアウトになり、0 が返されました。以下のコードを書きました。問題の正確な場所を教えてください。
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int port[]={80,139};
void Free(int Sock_Arr[])
{
for(int i=0;i<2;i++)
{
close(Sock_Arr[i]);
}
return ;
}
int main()
{
int Socket[2],result=0; //Socket array
struct sockaddr_in service;
fd_set writefds;
fd_set exceptfds;
struct timeval timer;
timer.tv_sec=5;
timer.tv_usec=0;
int flag=0;
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
char Ip_Addr[20];
for(int i=0;i<2;i++)
{
if((Socket[i]=socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0)
{
Free(Socket);
}
fcntl(Socket[i],F_SETFL,O_NONBLOCK);
}
bzero(&service, sizeof(sockaddr_in));
printf("Enter the ip-address : ");
scanf("%s",Ip_Addr);
service.sin_family=AF_INET;
service.sin_addr.s_addr=inet_addr(Ip_Addr);
for(int i=0;i<2;i++)
{
FD_SET(Socket[i],&writefds);
FD_SET(Socket[i],&exceptfds);
service.sin_port=htons((unsigned short int )port[i]);
connect(Socket[i],(struct sockaddr *)&service,sizeof(sockaddr_in));
result= select(Socket[i]+1,NULL,&writefds,&exceptfds,&timer);
if(result<0||result==0)
{
flag=0;
printf("\n The machine could not be found on the port %d ",port[i]);
printf("result : %d",result);
perror("select");
}
else
{
printf("\n The machine could be found on the port %d ",port[i]);
flag=1;
printf("result : %d",result);
if(FD_ISSET(Socket[i],&writefds))
{
printf("The socket triggered on write on port %d",port[i]);
}
else if(FD_ISSET(Socket[i],&exceptfds))
{
printf("The socket triggered on except on port %d",port[i]);
}
else
{
printf("No socket triggered on %d",port[i]);
}
}
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
}
Free(Socket);
if(flag==1)
{
return 1;
}
else
{
return 0;
}
}