Socket.st
changeset 589 b76269eef694
parent 576 13acab81fad0
child 604 b302a21afece
equal deleted inserted replaced
588:677527917896 589:b76269eef694
   436 !
   436 !
   437 
   437 
   438 examples
   438 examples
   439 "
   439 "
   440     example (get help info from an nntp server):
   440     example (get help info from an nntp server):
   441 									[exBegin]
   441                                                                         [exBegin]
   442 	|sock host|
   442         |sock host|
   443 
   443 
   444 	host := OperatingSystem getEnvironment:'NNTPSERVER'.
   444         host := OperatingSystem getEnvironment:'NNTPSERVER'.
   445 
   445 
   446 	sock := Socket newTCPclientToHost:host port:'nntp'.
   446         sock := Socket newTCPclientToHost:host port:'nntp'.
   447 	Transcript showCR:sock nextLine.
   447         Transcript showCR:sock nextLine.
   448 	sock buffered:false.
   448         sock buffered:false.
   449 
   449 
   450 	sock nextPutAll:'HELP'; cr.
   450         sock nextPutAll:'HELP'; cr.
   451 	[:exit |
   451         [:exit |
   452 	    |line|
   452             |line|
   453 
   453 
   454 	    line := sock nextLine.
   454             line := sock nextLine.
   455 	    line = '.' ifTrue:[exit value:nil].
   455             line = '.' ifTrue:[exit value:nil].
   456 	    Transcript showCR:line.
   456             Transcript showCR:line.
   457 	] loopWithExit.
   457         ] loopWithExit.
   458 	sock close
   458         sock close
   459 									[exEnd]
   459                                                                         [exEnd]
   460 
   460 
   461 
   461 
   462     example (connect to finger daemon, get users entry):
   462     example (connect to finger daemon, get users entry):
   463 									[exBegin]
   463                                                                         [exBegin]
   464 	|sock host entry|
   464         |sock host entry|
   465 
   465 
   466 	host := OperatingSystem getHostName.
   466         host := OperatingSystem getHostName.
   467 
   467 
   468 	sock := Socket newTCPclientToHost:host port:'finger'.
   468         sock := Socket newTCPclientToHost:host port:'finger'.
   469 	sock useCRLF:true.
   469         sock isNil ifTrue:[
   470 	sock buffered:false.
   470             self warn:'no finger daemon is running'.
   471 	sock isNil ifTrue:[
   471             ^ self
   472 	    Transcript showCR:'cannot connect to local finger daemon'
   472         ].
   473 	] ifFalse:[
   473         sock useCRLF:true.
   474 	    sock nextPutAll:(OperatingSystem getLoginName).
   474         sock buffered:false.
   475 	    sock cr.
   475         sock isNil ifTrue:[
   476 
   476             Transcript showCR:'cannot connect to local finger daemon'
   477 	    entry := sock nextLine.
   477         ] ifFalse:[
   478 	    Transcript showCR:entry.
   478             sock nextPutAll:(OperatingSystem getLoginName).
   479 
   479             sock cr.
   480 	    sock close
   480 
   481 	]
   481             entry := sock nextLine.
   482 									[exEnd]
   482             Transcript showCR:entry.
       
   483 
       
   484             sock close
       
   485         ]
       
   486                                                                         [exEnd]
   483 
   487 
   484     example (connect to an ftp server):
   488     example (connect to an ftp server):
   485 									[exBegin]
   489                                                                         [exBegin]
   486 	|sock host|
   490         |sock host|
   487 
   491 
   488 	host := OperatingSystem getHostName.
   492         host := OperatingSystem getHostName.
   489 	sock := Socket newTCPclientToHost:host port:'ftp'.
   493         sock := Socket newTCPclientToHost:host port:'ftp'.
   490 
   494 
   491 	sock buffered:false.
   495         sock buffered:false.
   492 	Transcript showCR:sock nextLine.
   496         Transcript showCR:sock nextLine.
   493 	sock nextPutAll:('USER ' , 'anonymous'); cr.
   497         sock nextPutAll:('USER ' , 'anonymous'); cr.
   494 	Transcript showCR:sock nextLine.
   498         Transcript showCR:sock nextLine.
   495 	sock nextPutAll:('PASS ' , 'fooBar'); cr.
   499         sock nextPutAll:('PASS ' , 'fooBar'); cr.
   496 	Transcript showCR:sock nextLine.
   500         Transcript showCR:sock nextLine.
   497 	sock nextPutAll:'LIST'; cr.
   501         sock nextPutAll:'LIST'; cr.
   498 	Transcript showCR:sock nextLine.
   502         Transcript showCR:sock nextLine.
   499 	sock close.
   503         sock close.
   500 
   504 
   501 	'dont know enough of the ftp protocol to continue here ...'
   505         'dont know enough of the ftp protocol to continue here ...'
   502 									[exEnd]
   506                                                                         [exEnd]
   503 
   507 
   504 
   508 
   505     example (connect to an snmp server [UDP]):
   509     example (connect to an snmp server [UDP]):
   506 									[exBegin]
   510                                                                         [exBegin]
   507 	|sock port|
   511         |sock port|
   508 
   512 
   509 	sock := Socket newUDP.
   513         sock := Socket newUDP.
   510 	port := Socket portOfService:'snmp'.
   514         port := Socket portOfService:'snmp'.
   511 	sock connectTo:(OperatingSystem getHostName) port:port.
   515         sock connectTo:(OperatingSystem getHostName) port:port.
   512 	sock buffered:false.
   516         sock buffered:false.
   513 	Transcript showCR:'got it'.
   517         Transcript showCR:'got it'.
   514 	sock close.
   518         sock close.
   515 									[exEnd]
   519                                                                         [exEnd]
   516 
   520 
   517 
   521 
   518     example (await connection from a client and read some data):
   522     example (await connection from a client and read some data):
   519 	|connectSock sock|
   523         |connectSock sock|
   520 
   524 
   521 	connectSock := Socket newTCPserverAtPort:9998.  
   525         connectSock := Socket newTCPserverAtPort:9998.  
   522 	connectSock isNil ifTrue:[
   526         connectSock isNil ifTrue:[
   523 	    Transcript showCR:'socket setup failed.'.
   527             Transcript showCR:'socket setup failed.'.
   524 	] ifFalse:[
   528         ] ifFalse:[
   525 	    Transcript showCR:'listen ..'.
   529             Transcript showCR:'listen ..'.
   526 	    (connectSock listenFor:5) ifFalse:[
   530             (connectSock listenFor:5) ifFalse:[
   527 		Transcript showCR:'listen failed.'.
   531                 Transcript showCR:'listen failed.'.
   528 	    ] ifTrue:[
   532             ] ifTrue:[
   529 		Transcript showCR:'wait'.
   533                 Transcript showCR:'wait'.
   530 		connectSock readWait.  
   534                 connectSock readWait.  
   531 		Transcript showCR:'accept'.
   535                 Transcript showCR:'accept'.
   532 		sock := connectSock accept.
   536                 sock := connectSock accept.
   533 		sock isNil ifTrue:[
   537                 sock isNil ifTrue:[
   534 		    Transcript showCR:'accept failed.'.
   538                     Transcript showCR:'accept failed.'.
   535 		] ifFalse:[
   539                 ] ifFalse:[
   536 		    sock buffered:false.
   540                     sock buffered:false.
   537 		    Transcript showCR:'server: got it'.
   541                     Transcript showCR:'server: got it'.
   538 		    'can now do transfer via sock'.
   542                     'can now do transfer via sock'.
   539 		    Transcript showCR:'read'.
   543                     Transcript showCR:'read'.
   540 		    Transcript showCR:('got: ' , sock nextLine).
   544                     Transcript showCR:('got: ' , sock nextLine).
   541 
   545 
   542 		    Transcript showCR:'close'.
   546                     Transcript showCR:'close'.
   543 		    sock close
   547                     sock close
   544 		].
   548                 ].
   545 		connectSock close.
   549                 connectSock close.
   546 	    ]
   550             ]
   547 	]
   551         ]
   548 
   552 
   549 
   553 
   550     example (connect to above server and send some data):
   554     example (connect to above server and send some data):
   551 	|sock|
   555         |sock|
   552 
   556 
   553 	sock := Socket newTCPclientToHost:(OperatingSystem getHostName) port:9998.
   557         sock := Socket newTCPclientToHost:(OperatingSystem getHostName) port:9998.
   554 	sock isNil ifTrue:[
   558         sock isNil ifTrue:[
   555 	    Transcript showCR:'nope'
   559             Transcript showCR:'nope'
   556 	] ifFalse:[
   560         ] ifFalse:[
   557 	    sock buffered:false.
   561             sock buffered:false.
   558 	    Transcript showCR:'client: got it'.
   562             Transcript showCR:'client: got it'.
   559 	    'can now do transfer via sock'.
   563             'can now do transfer via sock'.
   560 	    Transcript showCR:'sending <hello>'.
   564             Transcript showCR:'sending <hello>'.
   561 	    sock nextPutLine:'hello'.
   565             sock nextPutLine:'hello'.
   562 	    sock close
   566             sock close
   563 	]
   567         ]
   564 
   568 
   565     example: UNIX domain socket (await connection from a client and read some data):
   569     example: UNIX domain socket (await connection from a client and read some data):
   566 
   570 
   567 	|connectSock sock|
   571         |connectSock sock|
   568 
   572 
   569 	'/tmp/ud_socket' asFilename remove.
   573         '/tmp/ud_socket' asFilename remove.
   570 	connectSock := Socket newUNIXserverAt:'/tmp/ud_socket'.  
   574         connectSock := Socket newUNIXserverAt:'/tmp/ud_socket'.  
   571 	connectSock isNil ifTrue:[
   575         connectSock isNil ifTrue:[
   572 	    Transcript showCR:'socket setup failed.'.
   576             Transcript showCR:'socket setup failed.'.
   573 	] ifFalse:[
   577         ] ifFalse:[
   574 	    Transcript showCR:'listen ..'.
   578             Transcript showCR:'listen ..'.
   575 	    (connectSock listenFor:5) ifFalse:[
   579             (connectSock listenFor:5) ifFalse:[
   576 		Transcript showCR:'listen failed.'.
   580                 Transcript showCR:'listen failed.'.
   577 	    ] ifTrue:[
   581             ] ifTrue:[
   578 		Transcript showCR:'wait'.
   582                 Transcript showCR:'wait'.
   579 		connectSock buffered:false.
   583                 connectSock buffered:false.
   580 		connectSock readWait.  
   584                 connectSock readWait.  
   581 		Transcript showCR:'accept'.
   585                 Transcript showCR:'accept'.
   582 		sock := connectSock accept.
   586                 sock := connectSock accept.
   583 		sock isNil ifTrue:[
   587                 sock isNil ifTrue:[
   584 		    Transcript showCR:'accept failed.'.
   588                     Transcript showCR:'accept failed.'.
   585 		] ifFalse:[
   589                 ] ifFalse:[
   586 		    sock buffered:false.
   590                     sock buffered:false.
   587 		    Transcript showCR:'server: got it'.
   591                     Transcript showCR:'server: got it'.
   588 		    'can now do transfer via sock'.
   592                     'can now do transfer via sock'.
   589 		    Transcript showCR:'read'.
   593                     Transcript showCR:'read'.
   590 		    Transcript showCR:('got: ' , sock nextLine).
   594                     Transcript showCR:('got: ' , sock nextLine).
   591 
   595 
   592 		    Transcript showCR:'close'.
   596                     Transcript showCR:'close'.
   593 		    sock close
   597                     sock close
   594 		].
   598                 ].
   595 		connectSock close.
   599                 connectSock close.
   596 	    ]
   600             ]
   597 	]
   601         ]
   598 
   602 
   599 
   603 
   600     example (connect to above server and send some data;
   604     example (connect to above server and send some data;
   601 	     Notice, this fails, if above server code is executed in the same ST/X image
   605              Notice, this fails, if above server code is executed in the same ST/X image
   602 		     (at least on LINUX), since the OS does not correctly handle
   606                      (at least on LINUX), since the OS does not correctly handle
   603 		     a connect from within an interrupted accept system call
   607                      a connect from within an interrupted accept system call
   604 		     On SGI's SVR4, this works ok
   608                      On SGI's SVR4, this works ok
   605 
   609 
   606 	|sock|
   610         |sock|
   607 
   611 
   608 	sock := Socket newUNIXclientTo:'/tmp/ud_socket'.
   612         sock := Socket newUNIXclientTo:'/tmp/ud_socket'.
   609 	sock isNil ifTrue:[
   613         sock isNil ifTrue:[
   610 	    Transcript showCR:'nope'
   614             Transcript showCR:'nope'
   611 	] ifFalse:[
   615         ] ifFalse:[
   612 	    sock buffered:false.
   616             sock buffered:false.
   613 	    Transcript showCR:'client: got it'.
   617             Transcript showCR:'client: got it'.
   614 	    'can now do transfer via sock'.
   618             'can now do transfer via sock'.
   615 	    Transcript showCR:'sending <hello>'.
   619             Transcript showCR:'sending <hello>'.
   616 	    sock nextPutLine:'hello'.
   620             sock nextPutLine:'hello'.
   617 	    sock close
   621             sock close
   618 	]
   622         ]
   619 
   623 
   620 
   624 
   621     example: pingWalk (try to ping hosts on the local network)
   625     example: pingWalk (try to ping hosts on the local network)
   622 									[exBegin]
   626                                                                         [exBegin]
   623 	|myName myAddress list top hosts walkProcess port|
   627         |myName myAddress list top hosts walkProcess port|
   624 
   628 
   625 	myName := OperatingSystem getHostName.
   629         myName := OperatingSystem getHostName.
   626 	myAddress := Socket ipAddressOfHost:myName.
   630         myAddress := Socket ipAddressOfHost:myName.
   627 
   631 
   628 	port := Socket portOfService:'echo'.
   632         port := Socket portOfService:'echo'.
   629 	port isNil ifTrue:[
   633         port isNil ifTrue:[
   630 	    self error:'dont know echo port'.
   634             self error:'dont know echo port'.
   631 	    ^ self
   635             ^ self
   632 	].
   636         ].
   633 
   637 
   634 	top := StandardSystemView new.
   638         top := StandardSystemView new.
   635 	top label:'PING net walk'.
   639         top label:'PING net walk'.
   636 
   640 
   637 	list := ScrollableView for:ListView in:top.
   641         list := ScrollableView for:ListView in:top.
   638 	list origin:0.0@0.0 corner:1.0@1.0.
   642         list origin:0.0@0.0 corner:1.0@1.0.
   639 
   643 
   640 	top openAndWait.
   644         top openAndWait.
   641 
   645 
   642 	walkProcess := [
   646         walkProcess := [
   643 	    |l low hi direction tryHostID dottedName hostName conn addr|
   647             |l low hi direction tryHostID dottedName hostName conn addr|
   644 
   648 
   645 	    l := SortedCollection new.
   649             l := SortedCollection new.
   646 
   650 
   647 	    ' only works with type C-net
   651             ' only works with type C-net
   648 	      the code below could simply do 1 to:254 do:[:hostID }
   652               the code below could simply do 1 to:254 do:[:hostID }
   649 	      but, to probe likely hosts earlier, the probing is done
   653               but, to probe likely hosts earlier, the probing is done
   650 	      ping-pong like around my ip-address (assuming, that other machines
   654               ping-pong like around my ip-address (assuming, that other machines
   651 	      have numbers around my own)'.
   655               have numbers around my own)'.
   652 
   656 
   653 	    low := hi := (myAddress at:4).
   657             low := hi := (myAddress at:4).
   654 	    direction := 1.
   658             direction := 1.
   655 
   659 
   656 	    [low > 0 or:[hi < 255]] whileTrue:[
   660             [low > 0 or:[hi < 255]] whileTrue:[
   657 		direction > 0 ifTrue:[
   661                 direction > 0 ifTrue:[
   658 		    hi := hi + 1.
   662                     hi := hi + 1.
   659 		    tryHostID := hi.
   663                     tryHostID := hi.
   660 		    direction := -1.
   664                     direction := -1.
   661 		] ifFalse:[
   665                 ] ifFalse:[
   662 		    low := low - 1.
   666                     low := low - 1.
   663 		    tryHostID := low.
   667                     tryHostID := low.
   664 		    direction := 1.
   668                     direction := 1.
   665 		].
   669                 ].
   666 		(tryHostID between:1 and:254) ifTrue:[
   670                 (tryHostID between:1 and:254) ifTrue:[
   667 		    dottedName := (myAddress at:1) printString
   671                     dottedName := (myAddress at:1) printString
   668 				  , '.' , (myAddress at:2) printString
   672                                   , '.' , (myAddress at:2) printString
   669 				  , '.' , (myAddress at:3) printString
   673                                   , '.' , (myAddress at:3) printString
   670 				  , '.' , tryHostID printString.
   674                                   , '.' , tryHostID printString.
   671 
   675 
   672 		    top label:'PING net walk - trying ' , dottedName.
   676                     top label:'PING net walk - trying ' , dottedName.
   673 
   677 
   674 		    top windowGroup withCursor:Cursor wait do:[
   678                     top windowGroup withCursor:Cursor wait do:[
   675 			conn := Socket newTCPclientToHost:dottedName port:port withTimeout:1000.
   679                         conn := Socket newTCPclientToHost:dottedName port:port withTimeout:1000.
   676 			conn notNil ifTrue:[
   680                         conn notNil ifTrue:[
   677 			    addr := Socket ipAddressOfHost:dottedName.
   681                             addr := Socket ipAddressOfHost:dottedName.
   678 			    hostName := Socket hostWithIpAddress:addr.
   682                             hostName := Socket hostWithIpAddress:addr.
   679 			    hostName isNil ifTrue:[
   683                             hostName isNil ifTrue:[
   680 				hostName :='?'
   684                                 hostName :='?'
   681 			    ].
   685                             ].
   682 			    l add:(dottedName paddedTo:15 with:Character space) 
   686                             l add:(dottedName paddedTo:15 with:Character space) 
   683 				   , ' ' 
   687                                    , ' ' 
   684 				   , (hostName paddedTo:15 with:Character space)
   688                                    , (hostName paddedTo:15 with:Character space)
   685 				   , ' up & reachable'.
   689                                    , ' up & reachable'.
   686 			    list list:l.
   690                             list list:l.
   687 			    conn close.
   691                             conn close.
   688 			]
   692                         ]
   689 		    ].
   693                     ].
   690 		].
   694                 ].
   691 	    ].
   695             ].
   692 	    top label:'PING reachable hosts'.
   696             top label:'PING reachable hosts'.
   693 	] forkAt:(Processor userBackgroundPriority).
   697         ] forkAt:(Processor userBackgroundPriority).
   694 	walkProcess name:'ping net walker'.
   698         walkProcess name:'ping net walker'.
   695 									[exEnd]
   699                                                                         [exEnd]
   696 "
   700 "
   697 ! !
   701 ! !
   698 
   702 
   699 !Socket class methodsFor:'ST80 instance creation'!
   703 !Socket class methodsFor:'ST80 instance creation'!
   700 
   704 
  3994 ! !
  3998 ! !
  3995 
  3999 
  3996 !Socket class methodsFor:'documentation'!
  4000 !Socket class methodsFor:'documentation'!
  3997 
  4001 
  3998 version
  4002 version
  3999     ^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.100 1997-10-10 13:01:38 ca Exp $'
  4003     ^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.101 1997-11-03 15:30:26 cg Exp $'
  4000 ! !
  4004 ! !