--- a/Socket.st Sat Sep 22 19:53:39 2018 +0200
+++ b/Socket.st Tue Sep 25 18:28:02 2018 +0200
@@ -259,412 +259,457 @@
examples
"
example (get help info from an nntp server):
- [exBegin]
- |sock|
-
- sock := Socket newTCPclientToHost:'smtp.exept.de' port:'smtp'.
- sock isNil ifTrue:[
- self warn:'no smtp daemon is running'.
- ^ self
- ].
- Transcript showCR:sock nextLine.
-
- sock nextPutAll:'HELO STX socket test'; cr.
- Transcript showCR:sock nextLine.
- sock close
- [exEnd]
+ [exBegin]
+ |sock|
+
+ sock := Socket newTCPclientToHost:'smtp.exept.de' port:'smtp'.
+ sock isNil ifTrue:[
+ self warn:'no smtp daemon is running'.
+ ^ self
+ ].
+ Transcript showCR:sock nextLine.
+
+ sock nextPutAll:'HELO STX socket test'; cr.
+ Transcript showCR:sock nextLine.
+ sock close
+ [exEnd]
example (connect to finger daemon, get users entry):
- [exBegin]
- |sock entry|
-
- sock := Socket newTCPclientToHost:'localhost' port:'finger'.
- sock isNil ifTrue:[
- self warn:'no finger daemon is running'.
- ^ self
- ].
- sock useCRLF:true.
- sock buffered:false.
- sock isNil ifTrue:[
- Transcript showCR:'cannot connect to local finger daemon'
- ] ifFalse:[
- sock nextPutAll:(OperatingSystem getLoginName).
- sock cr.
-
- entry := sock nextLine.
- Transcript showCR:entry.
-
- sock close
- ]
- [exEnd]
+ [exBegin]
+ |sock entry|
+
+ sock := Socket newTCPclientToHost:'localhost' port:'finger'.
+ sock isNil ifTrue:[
+ self warn:'no finger daemon is running'.
+ ^ self
+ ].
+ sock useCRLF:true.
+ sock buffered:false.
+ sock isNil ifTrue:[
+ Transcript showCR:'cannot connect to local finger daemon'
+ ] ifFalse:[
+ sock nextPutAll:(OperatingSystem getLoginName).
+ sock cr.
+
+ entry := sock nextLine.
+ Transcript showCR:entry.
+
+ sock close
+ ]
+ [exEnd]
example (connect to an ftp server):
- [exBegin]
- |sock|
-
- sock := Socket newTCPclientToHost:'www.exept.de' port:'ftp'.
-
- sock buffered:false.
- Transcript showCR:sock nextLine.
- sock nextPutAll:('USER ' , 'anonymous'); cr.
- Transcript showCR:sock nextLine.
- sock nextPutAll:('PASS ' , 'fooBar'); cr.
- Transcript showCR:sock nextLine.
- sock nextPutAll:'HELP'; cr.
- [
- |line|
- line := sock nextLine.
- Transcript showCR:line.
- (line at:4) = $-
- ] whileTrue.
- sock close.
-
- 'don't know enough of the ftp protocol to continue here ...'
- [exEnd]
+ [exBegin]
+ |sock|
+
+ sock := Socket newTCPclientToHost:'www.exept.de' port:'ftp'.
+
+ sock buffered:false.
+ Transcript showCR:sock nextLine.
+ sock nextPutAll:('USER ' , 'anonymous'); cr.
+ Transcript showCR:sock nextLine.
+ sock nextPutAll:('PASS ' , 'fooBar'); cr.
+ Transcript showCR:sock nextLine.
+ sock nextPutAll:'HELP'; cr.
+ [
+ |line|
+ line := sock nextLine.
+ Transcript showCR:line.
+ (line at:4) = $-
+ ] whileTrue.
+ sock close.
+
+ 'don't know enough of the ftp protocol to continue here ...'
+ [exEnd]
example (connect to an snmp server [UDP]):
Note: this is not a real connection, only the destination address is
- being fixed.
- [exBegin]
- |sock port|
-
- sock := Socket newUDP.
- port := Socket portOfService:'snmp'.
- sock connectTo:'localhost' port:port.
- sock buffered:false.
- Transcript showCR:'got it'.
- sock close.
- [exEnd]
+ being fixed.
+ [exBegin]
+ |sock port|
+
+ sock := Socket newUDP.
+ port := Socket portOfService:'snmp'.
+ sock connectTo:'224.1.2.3' port:port.
+ sock buffered:false.
+ Transcript showCR:'got it'.
+ sock close.
+ [exEnd]
example (await connection from a client and read some data):
- [exBegin]
- |connectSock sock|
-
- connectSock := Socket newTCPserverAtPort:9998.
- connectSock isNil ifTrue:[
- Transcript showCR:'socket setup failed.'.
- ] ifFalse:[
- Transcript showCR:'listen ..'.
- (connectSock listenFor:5) ifFalse:[
- Transcript showCR:'listen failed.'.
- ] ifTrue:[
- Transcript showCR:'wait'.
- connectSock readWait.
- Transcript showCR:'accept'.
- sock := connectSock accept.
- sock isNil ifTrue:[
- Transcript showCR:'accept failed.'.
- ] ifFalse:[
- sock buffered:false.
- Transcript showCR:'server: got it'.
- 'can now do transfer via sock'.
- Transcript showCR:'read'.
- Transcript showCR:('got: ' , sock nextLine).
-
- Transcript showCR:'close'.
- sock close
- ].
- connectSock close.
- ]
- ]
- [exEnd]
+ [exBegin]
+ |connectSock sock|
+
+ connectSock := Socket newTCPserverAtPort:9998.
+ connectSock isNil ifTrue:[
+ Transcript showCR:'socket setup failed.'.
+ ] ifFalse:[
+ Transcript showCR:'listen ..'.
+ (connectSock listenFor:5) ifFalse:[
+ Transcript showCR:'listen failed.'.
+ ] ifTrue:[
+ Transcript showCR:'wait'.
+ connectSock readWait.
+ Transcript showCR:'accept'.
+ sock := connectSock accept.
+ sock isNil ifTrue:[
+ Transcript showCR:'accept failed.'.
+ ] ifFalse:[
+ sock buffered:false.
+ Transcript showCR:'server: got it'.
+ 'can now do transfer via sock'.
+ Transcript showCR:'read'.
+ Transcript showCR:('got: ' , sock nextLine).
+
+ Transcript showCR:'close'.
+ sock close
+ ].
+ connectSock close.
+ ]
+ ]
+ [exEnd]
example (connect to above server and send some data):
- [exBegin]
- |sock|
-
- sock := Socket newTCPclientToHost:'localhost' port:9998.
- sock isNil ifTrue:[
- Transcript showCR:'nope'
- ] ifFalse:[
- sock buffered:false.
- Transcript showCR:'client: got it'.
- 'can now do transfer via sock'.
- Transcript showCR:'sending <hello>'.
- sock nextPutLine:'hello'.
- sock close
- ]
- [exEnd]
+ [exBegin]
+ |sock|
+
+ sock := Socket newTCPclientToHost:'localhost' port:9998.
+ sock isNil ifTrue:[
+ Transcript showCR:'nope'
+ ] ifFalse:[
+ sock buffered:false.
+ Transcript showCR:'client: got it'.
+ 'can now do transfer via sock'.
+ Transcript showCR:'sending <hello>'.
+ sock nextPutLine:'hello'.
+ sock close
+ ]
+ [exEnd]
example: UNIX domain socket (await connection from a client and read some data):
- |connectSock sock|
-
- '/tmp/ud_socket' asFilename remove.
- connectSock := Socket newUNIXserverAt:'/tmp/ud_socket'.
- connectSock isNil ifTrue:[
- Transcript showCR:'socket setup failed.'.
- ] ifFalse:[
- Transcript showCR:'listen ..'.
- (connectSock listenFor:5) ifFalse:[
- Transcript showCR:'listen failed.'.
- ] ifTrue:[
- Transcript showCR:'wait'.
- connectSock buffered:false.
- connectSock readWait.
- Transcript showCR:'accept'.
- sock := connectSock accept.
- sock isNil ifTrue:[
- Transcript showCR:'accept failed.'.
- ] ifFalse:[
- sock buffered:false.
- Transcript showCR:'server: got it'.
- 'can now do transfer via sock'.
- Transcript showCR:'read'.
- Transcript showCR:('got: ' , sock nextLine).
-
- Transcript showCR:'close'.
- sock close
- ].
- connectSock close.
- ]
- ]
+ |connectSock sock|
+
+ '/tmp/ud_socket' asFilename remove.
+ connectSock := Socket newUNIXserverAt:'/tmp/ud_socket'.
+ connectSock isNil ifTrue:[
+ Transcript showCR:'socket setup failed.'.
+ ] ifFalse:[
+ Transcript showCR:'listen ..'.
+ (connectSock listenFor:5) ifFalse:[
+ Transcript showCR:'listen failed.'.
+ ] ifTrue:[
+ Transcript showCR:'wait'.
+ connectSock buffered:false.
+ connectSock readWait.
+ Transcript showCR:'accept'.
+ sock := connectSock accept.
+ sock isNil ifTrue:[
+ Transcript showCR:'accept failed.'.
+ ] ifFalse:[
+ sock buffered:false.
+ Transcript showCR:'server: got it'.
+ 'can now do transfer via sock'.
+ Transcript showCR:'read'.
+ Transcript showCR:('got: ' , sock nextLine).
+
+ Transcript showCR:'close'.
+ sock close
+ ].
+ connectSock close.
+ ]
+ ]
example (connect to above server and send some data;
- Notice, this fails, if above server code is executed in the same ST/X image
- (at least on LINUX), since the OS does not correctly handle
- a connect from within an interrupted accept system call
- On SGI's SVR4, this works ok
- [exBegin]
- |sock|
-
- sock := Socket newUNIXclientTo:'/tmp/ud_socket'.
- sock isNil ifTrue:[
- Transcript showCR:'nope'
- ] ifFalse:[
- sock buffered:false.
- Transcript showCR:'client: got it'.
- 'can now do transfer via sock'.
- Transcript showCR:'sending <hello>'.
- sock nextPutLine:'hello'.
- sock close
- ]
- [exEnd]
+ Notice, this fails, if above server code is executed in the same ST/X image
+ (at least on LINUX), since the OS does not correctly handle
+ a connect from within an interrupted accept system call
+ On SGI's SVR4, this works ok
+ [exBegin]
+ |sock|
+
+ sock := Socket newUNIXclientTo:'/tmp/ud_socket'.
+ sock isNil ifTrue:[
+ Transcript showCR:'nope'
+ ] ifFalse:[
+ sock buffered:false.
+ Transcript showCR:'client: got it'.
+ 'can now do transfer via sock'.
+ Transcript showCR:'sending <hello>'.
+ sock nextPutLine:'hello'.
+ sock close
+ ]
+ [exEnd]
example (UDP await packet from a client and read some data):
- [exBegin]
- |udpSock sock addr n dataBuffer|
-
- udpSock := Socket newUDPserverAtPort:9999.
- udpSock isNil ifTrue:[
- Transcript showCR:'socket setup failed.'.
- ] ifFalse:[
- Transcript showCR:'wait'.
- udpSock readWait.
-
- addr := IPSocketAddress new.
- dataBuffer := ByteArray new:1000.
- n := udpSock receiveFrom:addr buffer:dataBuffer start:1 for:dataBuffer size.
- n > 0 ifTrue:[
- Transcript showCR:('got: ' , n printString , 'bytes from ' , addr printString).
- Transcript showCR:('data: ' , (dataBuffer copyTo:n) printString).
- ] ifFalse:[
- Transcript showCR:'read failed'.
- ].
-
- Transcript showCR:'close'.
- udpSock close
- ]
- [exEnd]
+ [exBegin]
+ |udpSock sock addr n dataBuffer|
+
+ udpSock := Socket newUDPserverAtPort:9999.
+ udpSock isNil ifTrue:[
+ Transcript showCR:'socket setup failed.'.
+ ] ifFalse:[
+ Transcript showCR:'wait'.
+ udpSock readWait.
+
+ addr := IPSocketAddress new.
+ dataBuffer := ByteArray new:1000.
+ n := udpSock receiveFrom:addr buffer:dataBuffer start:1 for:dataBuffer size.
+ n > 0 ifTrue:[
+ Transcript showCR:('got: ' , n printString , 'bytes from ' , addr printString).
+ Transcript showCR:('data: ' , (dataBuffer copyTo:n) printString).
+ ] ifFalse:[
+ Transcript showCR:'read failed'.
+ ].
+
+ Transcript showCR:'close'.
+ udpSock close
+ ]
+ [exEnd]
+
example (connect to above UDP server and send some data;
- [exBegin]
- |sock|
-
- sock := Socket newUDP.
- sock isNil ifTrue:[
- Transcript showCR:'nope'
- ] ifFalse:[
- sock sendTo:(IPSocketAddress new hostName:'localhost' port:9999) buffer:'hello world'.
- sock close
- ]
- [exEnd]
+ [exBegin]
+ |sock|
+
+ sock := Socket newUDP.
+ sock isNil ifTrue:[
+ Transcript showCR:'nope'
+ ] ifFalse:[
+ sock
+ sendTo:(IPSocketAddress hostName:'127.0.0.1' port:9999) buffer:'hello world'.
+ sock close
+ ]
+ [exEnd]
+
+
+ example (UDP await packet from a client and read some multicast data):
+ [exBegin]
+ |udpSock sock addr n dataBuffer|
+
+ udpSock := Socket newUDPserverAtPort:9999.
+ udpSock isNil ifTrue:[
+ Transcript showCR:'socket setup failed.'.
+ ] ifFalse:[
+ udpSock setSocketOption:#'IP_ADD_MEMBERSHIP' argument:#[224 1 2 3] argument:#[0 0 0 0].
+ Transcript showCR:'wait'.
+ udpSock readWait.
+
+ addr := IPSocketAddress new.
+ dataBuffer := ByteArray new:1000.
+ n := udpSock receiveFrom:addr buffer:dataBuffer start:1 for:dataBuffer size.
+ n > 0 ifTrue:[
+ Transcript showCR:('got: ' , n printString , 'bytes from ' , addr printString).
+ Transcript showCR:('data: ' , (dataBuffer copyTo:n) printString).
+ ] ifFalse:[
+ Transcript showCR:'read failed'.
+ ].
+
+ Transcript showCR:'close'.
+ udpSock close
+ ]
+ [exEnd]
+
+ example (connect to above UDP server and send some multicast data):
+ [exBegin]
+ |sock|
+
+ sock := Socket newUDP.
+ sock isNil ifTrue:[
+ Transcript showCR:'nope'
+ ] ifFalse:[
+ sock
+ setSocketOption:#'IP_MULTICAST_TTL' argument:3 argument:nil;
+ sendTo:(IPSocketAddress hostName:'224.1.2.3' port:9999) buffer:'hello world'.
+ sock close
+ ]
+ [exEnd]
example: pingWalk (try to ping hosts on the local network)
Note: it dosen't use ICMP ping, but tries to reache the echo service,
- which is disabled on most OS.
- [exBegin]
- |myAddress list top hosts walkProcess port|
-
- myAddress := OperatingSystem getNetworkAddresses
- keysAndValuesSelect:[:eachIFName :eachAddress|
- eachAddress isLocal not
- and:[eachIFName = 'wlan0']
- ].
- myAddress := myAddress first hostAddress.
-
- port := Socket portOfService:'echo'.
- port isNil ifTrue:[
- self error:'dont know echo port'.
- ^ self
- ].
-
- top := StandardSystemView new.
- top label:'PING net walk'.
-
- list := ScrollableView for:ListView in:top.
- list origin:0.0@0.0 corner:1.0@1.0.
-
- top openAndWait.
-
- walkProcess := [
- |l low hi direction tryHostID dottedName hostName conn addr|
-
- l := SortedCollection new.
-
- ' only works with type C-net
- the code below could simply do 1 to:254 do:[:hostID }
- but, to probe likely hosts earlier, the probing is done
- ping-pong like around my ip-address (assuming, that other machines
- have numbers around my own)'.
-
- low := hi := (myAddress at:4).
- direction := 1.
-
- [low > 0 or:[hi < 255]] whileTrue:[
- direction > 0 ifTrue:[
- hi := hi + 1.
- tryHostID := hi.
- direction := -1.
- ] ifFalse:[
- low := low - 1.
- tryHostID := low.
- direction := 1.
- ].
- (tryHostID between:1 and:254) ifTrue:[
- dottedName := (myAddress at:1) printString
- , '.' , (myAddress at:2) printString
- , '.' , (myAddress at:3) printString
- , '.' , tryHostID printString.
-
- top label:'PING net walk - trying ' , dottedName.
-
- top windowGroup withCursor:Cursor wait do:[
- conn := Socket newTCPclientToHost:dottedName port:port withTimeout:1000.
- conn notNil ifTrue:[
- addr := Socket ipAddressOfHost:dottedName.
- hostName := Socket hostWithIpAddress:addr.
- hostName isNil ifTrue:[
- hostName :='?'
- ].
- l add:(dottedName paddedTo:15 with:Character space)
- , ' '
- , (hostName paddedTo:15 with:Character space)
- , ' up & reachable'.
- list list:l.
- conn close.
- ]
- ].
- ].
- ].
- top label:'PING reachable hosts'.
- ] forkAt:(Processor userBackgroundPriority).
- walkProcess name:'ping net walker'.
- [exEnd]
-
-
- This example creates a simple UDP server that accepts
- single packets from anybody and broadcasts them to all
- clients that have connected so far.
-
- [exBegin]
- | socket address buffer msgSize clients |
- clients := Set new.
- address := IPSocketAddress new.
- buffer := String new: 1024.
-
- socket := self newUDPserverAtPort: 6666.
-
- Transcript showCR: 'server starting'.
-
- [
- [true] whileTrue: [
- (socket readWaitWithTimeoutMs: 200) ifFalse: [
- msgSize := socket
- receiveFrom: address
- buffer: buffer
- start: 1
- for: buffer size.
-
- clients add: address copy.
- clients do: [ :clientAddress |
- socket
- sendTo: clientAddress
- buffer: buffer
- start: 1
- for: msgSize]]
- ]
- ] ensure:[
- Transcript showCR: 'server shutting down'.
- socket close
- ]
- [exEnd]
+ which is disabled on most OS.
+ [exBegin]
+ |myAddress list top hosts walkProcess port|
+
+ myAddress := OperatingSystem getNetworkAddresses
+ keysAndValuesSelect:[:eachIFName :eachAddress|
+ eachAddress isLocal not
+ and:[eachIFName = 'wlan0']
+ ].
+ myAddress := myAddress first hostAddress.
+
+ port := Socket portOfService:'echo'.
+ port isNil ifTrue:[
+ self error:'dont know echo port'.
+ ^ self
+ ].
+
+ top := StandardSystemView new.
+ top label:'PING net walk'.
+
+ list := ScrollableView for:ListView in:top.
+ list origin:0.0@0.0 corner:1.0@1.0.
+
+ top openAndWait.
+
+ walkProcess := [
+ |l low hi direction tryHostID dottedName hostName conn addr|
+
+ l := SortedCollection new.
+
+ ' only works with type C-net
+ the code below could simply do 1 to:254 do:[:hostID }
+ but, to probe likely hosts earlier, the probing is done
+ ping-pong like around my ip-address (assuming, that other machines
+ have numbers around my own)'.
+
+ low := hi := (myAddress at:4).
+ direction := 1.
+
+ [low > 0 or:[hi < 255]] whileTrue:[
+ direction > 0 ifTrue:[
+ hi := hi + 1.
+ tryHostID := hi.
+ direction := -1.
+ ] ifFalse:[
+ low := low - 1.
+ tryHostID := low.
+ direction := 1.
+ ].
+ (tryHostID between:1 and:254) ifTrue:[
+ dottedName := (myAddress at:1) printString
+ , '.' , (myAddress at:2) printString
+ , '.' , (myAddress at:3) printString
+ , '.' , tryHostID printString.
+
+ top label:'PING net walk - trying ' , dottedName.
+
+ top windowGroup withCursor:Cursor wait do:[
+ conn := Socket newTCPclientToHost:dottedName port:port withTimeout:1000.
+ conn notNil ifTrue:[
+ addr := Socket ipAddressOfHost:dottedName.
+ hostName := Socket hostWithIpAddress:addr.
+ hostName isNil ifTrue:[
+ hostName :='?'
+ ].
+ l add:(dottedName paddedTo:15 with:Character space)
+ , ' '
+ , (hostName paddedTo:15 with:Character space)
+ , ' up & reachable'.
+ list list:l.
+ conn close.
+ ]
+ ].
+ ].
+ ].
+ top label:'PING reachable hosts'.
+ ] forkAt:(Processor userBackgroundPriority).
+ walkProcess name:'ping net walker'.
+ [exEnd]
+
+
+ This example creates a simple UDP server that accepts
+ single packets from anybody and broadcasts them to all
+ clients that have connected so far.
+
+ [exBegin]
+ | socket address buffer msgSize clients |
+ clients := Set new.
+ address := IPSocketAddress new.
+ buffer := String new: 1024.
+
+ socket := self newUDPserverAtPort: 6666.
+
+ Transcript showCR: 'server starting'.
+
+ [
+ [true] whileTrue: [
+ (socket readWaitWithTimeoutMs: 200) ifFalse: [
+ msgSize := socket
+ receiveFrom: address
+ buffer: buffer
+ start: 1
+ for: buffer size.
+
+ clients add: address copy.
+ clients do: [ :clientAddress |
+ socket
+ sendTo: clientAddress
+ buffer: buffer
+ start: 1
+ for: msgSize]]
+ ]
+ ] ensure:[
+ Transcript showCR: 'server shutting down'.
+ socket close
+ ]
+ [exEnd]
send a datagram to above server:
- [exBegin]
-
- | socket address buffer host msg |
-
- host := Dialog
- request: 'What is the name of the server''s host?'
- initialAnswer: 'localhost'.
-
- socket := self newUDP.
-
- address := IPSocketAddress hostName: host port: 6666.
-
- buffer := ByteArray new: 1000.
- [
- [(msg := Dialog request: 'Say something') isEmpty] whileFalse:[
- | replySize stream |
-
- socket writeWait.
- stream := buffer writeStream.
- stream nextPutAll: msg.
- socket sendTo:address buffer:buffer start:1 for:stream position.
- socket readWait.
-
- replySize := socket receiveFrom:address buffer:buffer.
- replySize > 0 ifTrue: [
- Transcript cr; nextPutAll: 'Server acknowledged: '.
- Transcript show: ((buffer copyFrom: 1 to: replySize) asString)
- ]
- ]
- ] ensure: [socket close].
- Transcript cr
- [exEnd]
+ [exBegin]
+
+ | socket address buffer host msg |
+
+ host := Dialog
+ request: 'What is the name of the server''s host?'
+ initialAnswer: 'localhost'.
+
+ socket := self newUDP.
+
+ address := IPSocketAddress hostName: host port: 6666.
+
+ buffer := ByteArray new: 1000.
+ [
+ [(msg := Dialog request: 'Say something') isEmpty] whileFalse:[
+ | replySize stream |
+
+ socket writeWait.
+ stream := buffer writeStream.
+ stream nextPutAll: msg.
+ socket sendTo:address buffer:buffer start:1 for:stream position.
+ socket readWait.
+
+ replySize := socket receiveFrom:address buffer:buffer.
+ replySize > 0 ifTrue: [
+ Transcript cr; nextPutAll: 'Server acknowledged: '.
+ Transcript show: ((buffer copyFrom: 1 to: replySize) asString)
+ ]
+ ]
+ ] ensure: [socket close].
+ Transcript cr
+ [exEnd]
loopBack:
- [exBegin]
-
- |readerTask readingSocket writingSocket|
-
- readingSocket := self newTCPserverAtPort:9999.
- readerTask :=
- [
- |connection|
-
- readingSocket listenFor:1.
- connection := readingSocket accept.
- readingSocket close.
- [connection atEnd] whileFalse:[
- Transcript showCR:(connection nextLine).
- ].
- connection close.
- ] fork.
-
- Delay waitForSeconds:1.
- writingSocket := self newTCPclientToHost:'localhost' port:9999.
- writingSocket nextPutLine:'Hello'.
- writingSocket nextPutLine:'World'.
- writingSocket close.
- [exEnd]
+ [exBegin]
+
+ |readerTask readingSocket writingSocket|
+
+ readingSocket := self newTCPserverAtPort:9999.
+ readerTask :=
+ [
+ |connection|
+
+ readingSocket listenFor:1.
+ connection := readingSocket accept.
+ readingSocket close.
+ [connection atEnd] whileFalse:[
+ Transcript showCR:(connection nextLine).
+ ].
+ connection close.
+ ] fork.
+
+ Delay waitForSeconds:1.
+ writingSocket := self newTCPclientToHost:'localhost' port:9999.
+ writingSocket nextPutLine:'Hello'.
+ writingSocket nextPutLine:'World'.
+ writingSocket close.
+ [exEnd]
"
! !
@@ -3158,7 +3203,7 @@
|ok error|
handle isNil ifTrue:[
- ^ self errorNotOpen
+ ^ self errorNotOpen
].
%{ /* STACK: 32000 */
@@ -3166,234 +3211,255 @@
OBJ fp = __INST(handle);
if (fp != nil) {
- SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp);
- int opt = -1;
- int level = -1;
- int usize = -1;
- int ret;
- union u {
- BOOL u_bool;
- int u_int;
- struct linger u_linger;
+ SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp);
+ int opt = -1;
+ int level = -1;
+ int usize = -1;
+ int ret;
+ union u {
+ BOOL u_bool;
+ int u_int;
+ struct linger u_linger;
# ifdef IP_ADD_MEMBERSHIP
- struct ip_mreq u_ip_mreq;
+ struct ip_mreq u_ip_mreq;
# endif
# if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
- struct timeval u_tv;
+ struct timeval u_tv;
# endif
-
-
- } u;
+ } u;
# ifdef IP_ADD_MEMBERSHIP
- if (option == @symbol(IP_DROP_MEMBERSHIP)) {
- opt = IP_DROP_MEMBERSHIP;
- goto dropOrAdd;
- }
- if (option == @symbol(IP_ADD_MEMBERSHIP)) {
- /* add membership to a multicast group */
- opt = IP_ADD_MEMBERSHIP;
+ if (option == @symbol(IP_DROP_MEMBERSHIP)) {
+ opt = IP_DROP_MEMBERSHIP;
+ goto dropOrAdd;
+ }
+ if (option == @symbol(IP_ADD_MEMBERSHIP)) {
+ /* add membership to a multicast group */
+ opt = IP_ADD_MEMBERSHIP;
dropOrAdd:
- level = IPPROTO_IP;
- usize = sizeof(u.u_ip_mreq);
- if (__isByteArrayLike(arg1) && __isByteArrayLike(arg2)) {
- memcpy(&u.u_ip_mreq.imr_multiaddr, __byteArrayVal(arg1), sizeof(u.u_ip_mreq.imr_multiaddr));
- memcpy(&u.u_ip_mreq.imr_interface, __byteArrayVal(arg2), sizeof(u.u_ip_mreq.imr_interface));
- }
- else
- goto argError;
- }
+ level = IPPROTO_IP;
+ usize = sizeof(u.u_ip_mreq);
+ if (__isByteArrayLike(arg1) && __isByteArrayLike(arg2)) {
+ memcpy(&u.u_ip_mreq.imr_multiaddr, __byteArrayVal(arg1), sizeof(u.u_ip_mreq.imr_multiaddr));
+ memcpy(&u.u_ip_mreq.imr_interface, __byteArrayVal(arg2), sizeof(u.u_ip_mreq.imr_interface));
+ // once we use the struct ip_mreqn:
+ // u.u_ip_mreqn.imr_ifindex = 0;
+ }
+ else
+ goto argError;
+ }
# endif /* IP_ADD_MEMBERSHIP */
+# ifdef IP_TTL
+ if (option == @symbol(IP_TTL)) {
+ opt = IP_TTL;
+ level = IPPROTO_IP;
+ usize = sizeof(u.u_int);
+ if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
+ else goto argError;
+ }
+# endif /* IP_TTL */
+
+# ifdef IP_MULTICAST_TTL
+ if (option == @symbol(IP_MULTICAST_TTL)) {
+ opt = IP_MULTICAST_TTL;
+ level = IPPROTO_IP;
+ usize = sizeof(u.u_int);
+ if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
+ else goto argError;
+ }
+# endif /* IP_TTL */
+
+
# ifdef SO_BROADCAST
- if (option == @symbol(SO_BROADCAST)) {
- /* Enables transmission and receipt of broadcast messages on the socket. */
- opt = SO_BROADCAST;
- level = SOL_SOCKET;
- usize = sizeof(u.u_bool);
- if (arg1 == true) u.u_bool = TRUE;
- else if (arg1 == false) u.u_bool = FALSE;
- else goto argError;
- }
+ if (option == @symbol(SO_BROADCAST)) {
+ /* Enables transmission and receipt of broadcast messages on the socket. */
+ opt = SO_BROADCAST;
+ level = SOL_SOCKET;
+ usize = sizeof(u.u_bool);
+ if (arg1 == true) u.u_bool = TRUE;
+ else if (arg1 == false) u.u_bool = FALSE;
+ else goto argError;
+ }
# endif /* SO_BROADCAST */
# ifdef SO_CONDITIONAL
# if 0
- if (option == @symbol(SO_CONDITIONAL)) {
- /* Enables sockets to delay the acknowledgment of a connection until after the WSAAccept condition function is called. */
- opt = SO_CONDITIONAL;
- level = SOL_SOCKET;
- usize = sizeof(u.u_bool);
- if (arg1 == true) u.u_bool = TRUE;
- else if (arg1 == false) u.u_bool = FALSE;
- else goto argError;
- }
+ if (option == @symbol(SO_CONDITIONAL)) {
+ /* Enables sockets to delay the acknowledgment of a connection until after the WSAAccept condition function is called. */
+ opt = SO_CONDITIONAL;
+ level = SOL_SOCKET;
+ usize = sizeof(u.u_bool);
+ if (arg1 == true) u.u_bool = TRUE;
+ else if (arg1 == false) u.u_bool = FALSE;
+ else goto argError;
+ }
# endif
# endif /* SO_CONDITIONAL */
# ifdef SO_DEBUG
- if (option == @symbol(SO_DEBUG)) {
- /* Records debugging information. */
- opt = SO_DEBUG;
- level = SOL_SOCKET;
- usize = sizeof(u.u_bool);
- if (arg1 == true) u.u_bool = TRUE;
- else if (arg1 == false) u.u_bool = FALSE;
- else goto argError;
- }
+ if (option == @symbol(SO_DEBUG)) {
+ /* Records debugging information. */
+ opt = SO_DEBUG;
+ level = SOL_SOCKET;
+ usize = sizeof(u.u_bool);
+ if (arg1 == true) u.u_bool = TRUE;
+ else if (arg1 == false) u.u_bool = FALSE;
+ else goto argError;
+ }
# endif /* SO_DEBUG */
# ifdef SO_DONTLINGER
- if (option == @symbol(SO_DONTLINGER)) {
- /* Does not block close waiting for unsent data to be sent.
- Setting this option is equivalent to setting SO_LINGER with l_onoff set to zero. */
- opt = SO_DONTLINGER;
- level = SOL_SOCKET;
- usize = sizeof(u.u_bool);
- if (arg1 == true) u.u_bool = TRUE;
- else if (arg1 == false) u.u_bool = FALSE;
- else goto argError;
- }
+ if (option == @symbol(SO_DONTLINGER)) {
+ /* Does not block close waiting for unsent data to be sent.
+ Setting this option is equivalent to setting SO_LINGER with l_onoff set to zero. */
+ opt = SO_DONTLINGER;
+ level = SOL_SOCKET;
+ usize = sizeof(u.u_bool);
+ if (arg1 == true) u.u_bool = TRUE;
+ else if (arg1 == false) u.u_bool = FALSE;
+ else goto argError;
+ }
# endif /* SO_DONTLINGER */
# ifdef SO_DONTROUTE
- if (option == @symbol(SO_DONTROUTE)) {
- /* Does not route: sends directly to interface.
- Succeeds but is ignored on AF_INET sockets;
- fails on AF_INET6 sockets with WSAENOPROTOOPT.
- Not supported on ATM sockets (results in an error). */
- opt = SO_DONTROUTE;
- level = SOL_SOCKET;
- usize = sizeof(u.u_bool);
- if (arg1 == true) u.u_bool = TRUE;
- else if (arg1 == false) u.u_bool = FALSE;
- else goto argError;
- }
+ if (option == @symbol(SO_DONTROUTE)) {
+ /* Does not route: sends directly to interface.
+ Succeeds but is ignored on AF_INET sockets;
+ fails on AF_INET6 sockets with WSAENOPROTOOPT.
+ Not supported on ATM sockets (results in an error). */
+ opt = SO_DONTROUTE;
+ level = SOL_SOCKET;
+ usize = sizeof(u.u_bool);
+ if (arg1 == true) u.u_bool = TRUE;
+ else if (arg1 == false) u.u_bool = FALSE;
+ else goto argError;
+ }
# endif /* SO_DONTROUTE */
#if defined(IPPROTO_TCP) && defined(TCP_NODELAY)
- if (option == @symbol(TCP_NODELAY)) {
- /* enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm) */
- opt = TCP_NODELAY;
- level = IPPROTO_TCP;
- usize = sizeof(u.u_bool);
- if (arg1 == true) u.u_bool = TRUE;
- else if (arg1 == false) u.u_bool = FALSE;
- else goto argError;
- }
+ if (option == @symbol(TCP_NODELAY)) {
+ /* enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm) */
+ opt = TCP_NODELAY;
+ level = IPPROTO_TCP;
+ usize = sizeof(u.u_bool);
+ if (arg1 == true) u.u_bool = TRUE;
+ else if (arg1 == false) u.u_bool = FALSE;
+ else goto argError;
+ }
# endif /* TCP_NODELAY */
# ifdef SO_KEEPALIVE
- if (option == @symbol(SO_KEEPALIVE)) {
- /* Sends keep-alives. Not supported on ATM sockets (results in an error). */
- opt = SO_KEEPALIVE;
- level = SOL_SOCKET;
- usize = sizeof(u.u_bool);
- if (arg1 == true) u.u_bool = TRUE;
- else if (arg1 == false) u.u_bool = FALSE;
- else goto argError;
- }
+ if (option == @symbol(SO_KEEPALIVE)) {
+ /* Sends keep-alives. Not supported on ATM sockets (results in an error). */
+ opt = SO_KEEPALIVE;
+ level = SOL_SOCKET;
+ usize = sizeof(u.u_bool);
+ if (arg1 == true) u.u_bool = TRUE;
+ else if (arg1 == false) u.u_bool = FALSE;
+ else goto argError;
+ }
# endif /* SO_KEEPALIVE */
# ifdef SO_LINGER
- if (option == @symbol(SO_LINGER)) {
- /* Lingers on close if unsent data is present. */
- opt = SO_LINGER;
- level = SOL_SOCKET;
- usize = sizeof(u.u_linger);
- if (arg1 == true) u.u_linger.l_onoff = TRUE;
- else if (arg1 == false) u.u_linger.l_onoff = FALSE;
- else goto argError;
- if (arg2 == nil) u.u_linger.l_linger = 0;
- else if (__isSmallInteger(arg2))u.u_linger.l_linger = __intVal(arg2);
- else goto argError;
- DBGPRINTF(("SOCKET: SO_LINGER %d %d\n", u.u_linger.l_onoff, u.u_linger.l_linger));
- }
+ if (option == @symbol(SO_LINGER)) {
+ /* Lingers on close if unsent data is present. */
+ opt = SO_LINGER;
+ level = SOL_SOCKET;
+ usize = sizeof(u.u_linger);
+ if (arg1 == true) u.u_linger.l_onoff = TRUE;
+ else if (arg1 == false) u.u_linger.l_onoff = FALSE;
+ else goto argError;
+ if (arg2 == nil) u.u_linger.l_linger = 0;
+ else if (__isSmallInteger(arg2))u.u_linger.l_linger = __intVal(arg2);
+ else goto argError;
+ DBGPRINTF(("SOCKET: SO_LINGER %d %d\n", u.u_linger.l_onoff, u.u_linger.l_linger));
+ }
# endif /* SO_LINGER */
# ifdef SO_OOBINLINE
- if (option == @symbol(SO_OOBINLINE)) {
- /* Receives OOB data in the normal data stream. */
- opt = SO_OOBINLINE;
- level = SOL_SOCKET;
- usize = sizeof(u.u_bool);
- if (arg1 == true) u.u_bool = TRUE;
- else if (arg1 == false) u.u_bool = FALSE;
- else goto argError;
- }
+ if (option == @symbol(SO_OOBINLINE)) {
+ /* Receives OOB data in the normal data stream. */
+ opt = SO_OOBINLINE;
+ level = SOL_SOCKET;
+ usize = sizeof(u.u_bool);
+ if (arg1 == true) u.u_bool = TRUE;
+ else if (arg1 == false) u.u_bool = FALSE;
+ else goto argError;
+ }
# endif /* SO_OOBINLINE */
# ifdef SO_RCVBUF
- if (option == @symbol(SO_RCVBUF)) {
- /* Specifies the total per-socket buffer space reserved for receives.
- This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. */
- opt = SO_RCVBUF;
- level = SOL_SOCKET;
- usize = sizeof(u.u_int);
- if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
- else goto argError;
- }
+ if (option == @symbol(SO_RCVBUF)) {
+ /* Specifies the total per-socket buffer space reserved for receives.
+ This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. */
+ opt = SO_RCVBUF;
+ level = SOL_SOCKET;
+ usize = sizeof(u.u_int);
+ if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
+ else goto argError;
+ }
# endif /* SO_RCVBUF */
# ifdef SO_SNDBUF
- if (option == @symbol(SO_SNDBUF)) {
- /* Specifies the total per-socket buffer space reserved for sends.
- This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. */
- opt = SO_SNDBUF;
- level = SOL_SOCKET;
- usize = sizeof(u.u_int);
- if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
- else goto argError;
- }
+ if (option == @symbol(SO_SNDBUF)) {
+ /* Specifies the total per-socket buffer space reserved for sends.
+ This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. */
+ opt = SO_SNDBUF;
+ level = SOL_SOCKET;
+ usize = sizeof(u.u_int);
+ if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
+ else goto argError;
+ }
# endif /* SO_SNDBUF */
# ifdef SO_REUSEADDR
- if (option == @symbol(SO_REUSEADDR)) {
- /* Allows the socket to be bound to an address that is already in use. */
- opt = SO_REUSEADDR;
- level = SOL_SOCKET;
- usize = sizeof(u.u_bool);
- if (arg1 == true) u.u_bool = TRUE;
- else if (arg1 == false) u.u_bool = FALSE;
- else goto argError;
- }
+ if (option == @symbol(SO_REUSEADDR)) {
+ /* Allows the socket to be bound to an address that is already in use. */
+ opt = SO_REUSEADDR;
+ level = SOL_SOCKET;
+ usize = sizeof(u.u_bool);
+ if (arg1 == true) u.u_bool = TRUE;
+ else if (arg1 == false) u.u_bool = FALSE;
+ else goto argError;
+ }
# endif /* SO_OOBINLINE */
# ifdef SO_EXCLUSIVEADDRUSE
- if (option == @symbol(SO_EXCLUSIVEADDRUSE)) {
- /* Enables a socket to be bound for exclusive access.
- Does not require administrative privilege. */
- opt = SO_EXCLUSIVEADDRUSE;
- level = SOL_SOCKET;
- usize = sizeof(u.u_bool);
- if (arg1 == true) u.u_bool = TRUE;
- else if (arg1 == false) u.u_bool = FALSE;
- else goto argError;
- }
+ if (option == @symbol(SO_EXCLUSIVEADDRUSE)) {
+ /* Enables a socket to be bound for exclusive access.
+ Does not require administrative privilege. */
+ opt = SO_EXCLUSIVEADDRUSE;
+ level = SOL_SOCKET;
+ usize = sizeof(u.u_bool);
+ if (arg1 == true) u.u_bool = TRUE;
+ else if (arg1 == false) u.u_bool = FALSE;
+ else goto argError;
+ }
# endif /* SO_OOBINLINE */
# ifdef SO_RCVTIMEO
- if ((option == @symbol(SO_RCVTIMEO))
- && __isSmallInteger(arg1)
- && __isSmallInteger(arg2)) {
- opt = SO_RCVTIMEO;
- level = SOL_SOCKET;
- u.u_tv.tv_sec = __intVal(arg1);
- u.u_tv.tv_usec = __intVal(arg2);
- usize = sizeof(u.u_tv);
- }
+ if ((option == @symbol(SO_RCVTIMEO))
+ && __isSmallInteger(arg1)
+ && __isSmallInteger(arg2)) {
+ opt = SO_RCVTIMEO;
+ level = SOL_SOCKET;
+ u.u_tv.tv_sec = __intVal(arg1);
+ u.u_tv.tv_usec = __intVal(arg2);
+ usize = sizeof(u.u_tv);
+ }
# endif /* SO_RCVTIMEO */
# ifdef SO_SNDTIMEO
- if ((option == @symbol(SO_SNDTIMEO))
- && __isSmallInteger(arg1)
- && __isSmallInteger(arg2)) {
- opt = SO_SNDTIMEO;
- level = SOL_SOCKET;
- u.u_tv.tv_sec = __intVal(arg1);
- u.u_tv.tv_usec = __intVal(arg2);
- usize = sizeof(u.u_tv);
- }
+ if ((option == @symbol(SO_SNDTIMEO))
+ && __isSmallInteger(arg1)
+ && __isSmallInteger(arg2)) {
+ opt = SO_SNDTIMEO;
+ level = SOL_SOCKET;
+ u.u_tv.tv_sec = __intVal(arg1);
+ u.u_tv.tv_usec = __intVal(arg2);
+ usize = sizeof(u.u_tv);
+ }
# endif /* SO_SNDTIMEO */
# if !defined(IPV6_V6ONLY) && defined(__win32__)
@@ -3402,39 +3468,39 @@
# endif
# if defined(IPV6_V6ONLY)
- if (option == @symbol(IPV6_V6ONLY)) {
- opt = IPV6_V6ONLY;
- level = IPPROTO_IPV6;
- usize = sizeof(u.u_bool);
- if (arg1 == true) u.u_bool = TRUE;
- else if (arg1 == false) u.u_bool = FALSE;
- else goto argError;
+ if (option == @symbol(IPV6_V6ONLY)) {
+ opt = IPV6_V6ONLY;
+ level = IPPROTO_IPV6;
+ usize = sizeof(u.u_bool);
+ if (arg1 == true) u.u_bool = TRUE;
+ else if (arg1 == false) u.u_bool = FALSE;
+ else goto argError;
# ifdef __win32__
- console_fprintf(stderr, "%d %d %d %d\n", level, opt, usize, u.u_int);
+ console_fprintf(stderr, "%d %d %d %d\n", level, opt, usize, u.u_int);
# endif
- }
+ }
# endif /* IPV6_V6ONLY */
- if (usize == -1) goto argError;
-
- ok = (setsockopt(sock, level, opt, (char *)&u, usize) >= 0) ? true : false;
- if (ok == false) {
+ if (usize == -1) goto argError;
+
+ ok = (setsockopt(sock, level, opt, (char *)&u, usize) >= 0) ? true : false;
+ if (ok == false) {
# ifdef __win32__
- error = __mkSmallInteger(WSAGetLastError());
+ error = __mkSmallInteger(WSAGetLastError());
# else
- error = __mkSmallInteger(errno);
+ error = __mkSmallInteger(errno);
# endif
- }
+ }
}
argError: ;
#endif /* NO_SOCKET */
%}.
ok isNil ifTrue:[
- self primitiveFailed
+ self primitiveFailed
].
ok ifFalse:[
- '++++ Info: Socket>>#setSocketOption:... failed. error: ' infoPrint. error infoPrintCR.
+ '++++ Info: Socket>>#setSocketOption:... failed. error: ' infoPrint. error infoPrintCR.
].
!