john renfrew Posted August 3, 2015 Posted August 3, 2015 just a bit of code I made to let you see the results of pinging a server.. <code> // GetLatency ( fm_ip ; fm_port ; verbose ) // 15_08_03 JohnRenfrew // v1.0 // fm_ip is IP address or web address // fm_port is port to ping - defaults to 80 // verbose - if empty returns just average, else anything returns results of 5 loops import java.net.Socket import java.net.InetAddress import java.net.SocketAddress import java.net.InetSocketAddress import java.net.SocketTimeoutException port = !fm_port ? 80i : fm_port.toInteger() times = 5 result = '' try { hostaddr = InetAddress.getByName(fm_ip).getHostAddress() } catch (UnknownHostException e) { result = 'Invalid Host entered.' } if ( result != '' ){ return result } result += 'Pinging '+fm_ip+' (' + hostaddr + ') ' + times + ' times on port ' + port + '...' total = 0i totalping = 0l Socket s = null for ( i in 1..times) { start = System.currentTimeMillis() try { sockaddr = new InetSocketAddress(hostaddr, port) s = new Socket() s.connect(sockaddr, 1000) } catch(SocketTimeoutException e) { result += '\nSocket Request[' + i + ']: Connection timed out.' times = times - 1 continue } catch( e) { } end = System.currentTimeMillis() totalping += (end-start) totaltime = (end-start) avg = (totalping/i) result += '\nSocket Request['+ i +']: Time(In MS): ' +totaltime + ' Average: '+avg } result += '\n' averagePing = totalping/ Math.max(times, 1) result += '\nFinal Result: Average request - ' + averagePing + '\n' if ( verbose ){ return result } else { return avgPing } </code>
Recommended Posts
This topic is 3411 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now