This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

IP scanner with STM32f107 and PHY

Hello,

I've designed and programmed a board to communicate with other devices with Ethernet. Everything works fine and in this step I need to create an IP scanner to scan an IP range (for example 192.168.1.100 to 192.168.1.254) but it takes more than one hour to scan this range of IP( 155 IPs) and connect to one device which is located at 192.168.1.150 . Here is my IP scanner code:

int ScanTheNetwork(void)
{
	int i;
	NET_ADDR4 addr = { NET_ADDR_IP4, 5000, 192, 168, 1, 0 };
	
	netInitialize ();
	netDHCP_Disable (0);
	socket1[0].SCK = netTCP_GetSocket (tcp_cb_func);	
	
	for(i=100;i<255;i++)
	{
			addr.addr[3]=i;
			if (socket1[0].SCK >= 0) 
			{
				netTCP_SetOption (socket1[0].SCK , netTCP_OptionTimeout, 1);
				netTCP_Connect (socket1[0].SCK, (NET_ADDR *)&addr, 0);							
				netTCP_SetOption (socket1[0].SCK , netTCP_OptionTimeout, 1);
				
					while(1)
					{
								IWDG_ReloadCounter();						
								if(netTCP_GetState (socket1[0].SCK)==netTCP_StateUNUSED || netTCP_GetState (socket1[0].SCK)==netTCP_StateUNUSED)
								{
										netTCP_Close(socket1[0].SCK);
								}else if(netTCP_GetState (socket1[0].SCK)==netTCP_StateESTABLISHED)
								{
										return i;	
								}				
					}		
			}
	}		
}

In the above code I need to find a device ( I didn't put many devices) which is alive and its open port is 5000. I changed the IP range from (148 to 152 ) which the alive one is located at 150 but it takes more than 5minutes to connect to my devices.

I need to know what is best way to find my alive device and connect to it as soon as possible in the an IP range.

  • Generally speaking, I don't think using TCP client as an IP scanner is a good option. Depending on the socket options used in your TCP server (in your case 192.168.1.150), a TCP socket connection may remain "alive" for quite some time (e.g. until a default timeout happens), even if there is no data exchanged.

    You may use wireshark and enable Middleware network debug output to get more details about what happens in this 5 mins

    https://developer.arm.com/documentation/ka004208/latest

    Inside netTCP_Connect() there are also retry and timeout for each retry defined internally. 

    You may consider using ping client instead as your IP scanner:

    www.keil.com/.../group__net_ping___func.html