Re: UDP Sockets?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: UDP Sockets?

Dmitry Zamotkin-3
> Jerome Chan ([hidden email])
> 2001-03-06 21:38:04 PST
>
> Does Dolphin support UDP sockets? I've looked about the class files and
> I don't seem to find any UDP support.

I've just released an UDP Socket implementation. If you still need it feel
free to use it.

-----------------------------------
"Filed out from Dolphin Smalltalk 2000 release 4.01"!

Socket subclass: #UdpSocket
 instanceVariableNames: ''
 classVariableNames: ''
 poolDictionaries: ''
 classInstanceVariableNames: ''!
UdpSocket comment: ''!

UdpSocket guid: (GUID fromString: '{003DA162-A904-11D5-BA8C-0008C789A424}')!

!UdpSocket categoriesForClass!Unclassified! !
!UdpSocket methodsFor!

basicReceiveByteArray: anInteger
 "Private - Reads anInteger bytes from the socket.
 Answers a ByteArray representing the bytes read."

 | bytesReceived byteArray socketAddress socketAddressSize |

 byteArray := ByteArray new: anInteger.
 socketAddress := self socketAddress.
 socketAddressSize := SDWORD new value: socketAddress byteSize.

 bytesReceived := WSockLibrary default
  recvfrom: self asParameter
  buf: byteArray
  len: byteArray size
  flags: 0
  from: socketAddress asParameter
  fromlen: socketAddressSize.

 bytesReceived > 0 ifTrue:
  [ "Success."
  ^byteArray copyFrom: 1 to: bytesReceived ].
 bytesReceived = 0 ifTrue:
  [ "Socket has been closed."
  SocketClosed signal ].
 "Some other error."
 self error.!

basicSendByteArray: aByteArray
 "Private - Sends aByteArray through the socket."

 | result socketAddress |

 socketAddress := self socketAddress.

 result := WSockLibrary default
  sendto: self asParameter
  buf: aByteArray
  len: aByteArray size
  flags: 0
  to: socketAddress asParameter
  tolen: socketAddress byteSize.

 result = -1 ifTrue: [self error].
 ^result!

connect
 "No needed for UDP"

 self shouldNotImplement!

create
 "Private - Create a new socket."

 self descriptor: (WSockLibrary default socket: AF_INET type: SOCK_DGRAM
protocol: 0).
 (self descriptor = INVALID_SOCKET) ifTrue: [ self error ].

!

socketAddress
 "Remote socket address"

 ^ SOCKADDR_IN new
  sin_family: AF_INET;
  sin_addr: (IN_ADDR address: self address);
  sin_port: (WSockLibrary default htons: self port);
  yourself! !
!UdpSocket categoriesFor: #basicReceiveByteArray:!operations!private! !
!UdpSocket categoriesFor: #basicSendByteArray:!operations!private! !
!UdpSocket categoriesFor: #connect!*-should not
implement!operations!private! !
!UdpSocket categoriesFor: #create!operations!private! !
!UdpSocket categoriesFor: #socketAddress!accessing!private! !

!UdpSocket class methodsFor!

port: anIntegerPort address: anInternetAddress
 "Socket creating"

 ^ ( super port: anIntegerPort address: anInternetAddress )
  create;
  yourself
! !
!UdpSocket class categoriesFor: #port:address:!instance creation!public! !

-----------------------------------

Dmitry Zamotkin


Reply | Threaded
Open this post in threaded view
|

Re: UDP Sockets?

Chris Uppal-3
Dmitry,

> I've just released an UDP Socket implementation. If you still need it feel
> free to use it.

Thanks for that.

> Dmitry Zamotkin

    -- chris