tim Rowledge uploaded a new version of Network to project The Trunk: http://source.squeak.org/trunk/Network-tpr.280.mcz
==================== Summary ====================
Name: Network-tpr.280 Author: tpr Time: 5 July 2023, 4:48:48.448418 pm UUID: 2e0c8513-b3db-4325-9490-1dfb12c926ec Ancestors: Network-ct.279
Add commentary to some Socket methods to make it clear that the servers involved need to be running relevant services - and that at least some of them are rarely installed these days. In particular the #ping: method doesn't do what we might expect from the ping cmd.
=============== Diff against Network-ct.279 ===============
Item was changed: ----- Method: Socket class>>ping: (in category 'utilities') ----- ping: hostName + "kinda-Ping the given host. Useful for checking network connectivity. + The host *must* be running a TCP echo server - note that these days very few actually do so and thus you may need to install one. Fortunately this is usually fairly simple; on a Ubuntu type linux system + sudo apt install ncat + sudo ncat -l 7 --keep-open --exec '/bin/cat' + ... ought to work. Ctrl-c to kill the ersatz echoserver when you are done testing. + The ping command in a terminal uses a completely different protocol involving ICMP sockets and sacrifices to the Moon etc." + - "Ping the given host. Useful for checking network connectivity. The host must be running a TCP echo server." "Socket ping: 'squeak.cs.uiuc.edu'"
| tcpPort sock serverAddr startTime echoTime | tcpPort := 7. "7 = echo port, 13 = time port, 19 = character generator port"
serverAddr := NetNameResolver addressForName: hostName timeout: 10. serverAddr ifNil: [ ^self inform: 'Could not find an address for ', hostName ].
sock := Socket new. sock connectNonBlockingTo: serverAddr port: tcpPort. [sock waitForConnectionFor: 10] on: ConnectionTimedOut do: [:ex | (self confirm: 'Continue to wait for connection to ', hostName, '?') ifTrue: [ex retry] ifFalse: [ sock destroy. ^ self]].
sock sendData: 'echo!!'. startTime := Time millisecondClockValue. [sock waitForDataFor: 15] on: ConnectionTimedOut do: [:ex | (self confirm: 'Packet sent but no echo yet; keep waiting?') ifTrue: [ex retry]]. echoTime := Time millisecondClockValue - startTime.
sock destroy. self inform: hostName, ' responded in ', echoTime printString, ' milliseconds'. !
Item was changed: ----- Method: Socket class>>pingPorts:on:timeOutSecs: (in category 'utilities') ----- pingPorts: portList on: hostName timeOutSecs: timeOutSecs + "Attempt to connect to each of the given sockets on the given host. Wait at most timeOutSecs for the connections to be established. Answer an array of strings indicating the available ports. + Remember that the host must actually be running the relevant services for this to do anything useful; for example the port 7 echo is usually not installed on most modern unix servers. See also the #ping: method" - "Attempt to connect to each of the given sockets on the given host. Wait at most timeOutSecs for the connections to be established. Answer an array of strings indicating the available ports."
"Socket pingPorts: #(7 13 19 21 23 25 80 110 119) on: 'squeak.org' timeOutSecs: 15"
| serverAddr sockets startTime timeoutMsecs done result unconnectedCount connectedCount waitingCount | serverAddr := NetNameResolver addressForName: hostName timeout: 10. serverAddr ifNil: [ self inform: 'Could not find an address for ' , hostName. ^ #() ]. sockets := portList collect: [ :portNum | | sock | sock := Socket new. [ sock connectTo: serverAddr port: portNum ] on: ConnectionTimedOut do: [ ]. sock ]. startTime := Time millisecondClockValue. timeoutMsecs := (1000 * timeOutSecs) truncated. done := false. [ done ] whileFalse: [ unconnectedCount := 0. connectedCount := 0. waitingCount := 0. sockets do: [ :s | s isUnconnectedOrInvalid ifTrue: [ unconnectedCount := unconnectedCount + 1 ] ifFalse: [ s isConnected ifTrue: [ connectedCount := connectedCount + 1 ]. s isWaitingForConnection ifTrue: [ waitingCount := waitingCount + 1 ] ] ]. waitingCount = 0 ifTrue: [ done := true ]. connectedCount = sockets size ifTrue: [ done := true ]. (Time millisecondsSince: startTime) >= timeoutMsecs ifTrue: [ done := true ] ]. result := (sockets select: [ :s | s isConnected ]) collect: [ :s | self nameForWellKnownTCPPort: s remotePort ]. sockets do: [ :s | s destroy ]. ^ result!
Item was changed: ----- Method: Socket class>>pingPortsOn: (in category 'utilities') ----- pingPortsOn: hostName + "Attempt to connect to a set of well-known sockets on the given host, and answer the names of the available ports. + See also the comment for #ping: regarding server services. And also note that this is quite likely to seem like a port scanning attack to the server so be careful that it doesn't bite back" - "Attempt to connect to a set of well-known sockets on the given host, and answer the names of the available ports." "Socket pingPortsOn: 'www.disney.com'"
^ Socket pingPorts: #(7 13 19 21 23 25 80 110 119) on: hostName timeOutSecs: 20 !
packages@lists.squeakfoundation.org