License:
BSD style: see license.txt
Version:
Initial release: March 2004
author:
Christopher Miller
Kris Bell
Anders F Bjorklund (Darwin patches)
The original code has been modified in several ways:
1
It has been altered to fit within the Tango environment, meaning
that certain original classes have been reorganized, and/or have
subclassed Tango base-classes. For example, the original
Socket
class has been wrapped with three distinct subclasses, and now
derives from class tango.io.Resource.
2 All exception instances now subclass the Tango IOException.
3) Construction of new
Socket
instances via accept() is now
overloadable.
4) Constants and enums have been moved within a class boundary to
ensure explicit namespace usage.
5) changed
Socket
.select() to loop if it was interrupted.
All changes within the main body of code all marked with "Tango:"
For a good tutorial on socket-programming I highly recommend going
)
here:
http:
//www.ecst.csuchico.edu/~beej/guide/net/
)
- struct
timeval
;
- Internal structs:
- ushort
htons
(ushort x);
- conversions for network byte-order
- uint
htonl
(uint x);
- conversions for network byte-order
- static int
lastError
();
- Public interface ...
- enum
SocketOption
;
- these appear to be compatible with x86 platforms,
but not others!
Public interface ...
- union
linger
;
- Public interface ...
- enum
SocketOptionLevel
;
- Public interface ...
- enum
SocketShutdown
;
- Public interface ...
- enum
SocketFlags
;
- Public interface ...
- enum
SocketType
;
- Communication semantics
Public interface ...
-
STREAM
- sequenced, reliable, two-way communication-based byte streams
-
DGRAM
- connectionless, unreliable datagrams with a fixed maximum length; data may be lost or arrive out of order
-
RAW
- raw protocol access
-
RDM
- reliably-delivered message datagrams
-
SEQPACKET
- sequenced, reliable, two-way connection-based datagrams with a fixed maximum length
- enum
ProtocolType
;
- Protocol
Public interface ...
-
IP
- internet protocol version 4
-
ICMP
- internet control message protocol
-
IGMP
- internet group management protocol
-
GGP
- gateway to gateway protocol
-
TCP
- transmission control protocol
-
PUP
- PARC universal packet protocol
-
UDP
- user datagram protocol
-
IDP
- Xerox NS protocol
- enum
AddressFamily
;
- Public interface ...
- class
Socket
;
- Public interface ...
- this(AddressFamily family, SocketType type, ProtocolType protocol, bool create = true);
- Describe a socket flavor. If a single protocol type exists to support
this socket type within the address family, the ProtocolType may be
omitted.
- socket_t
fileHandle
();
- Return the underlying OS handle of this Conduit
- void
reopen
(socket_t sock = -1);
- Create a new native socket instance for this Socket
- bool
isAlive
();
- Is this socket still alive? A closed socket is considered to
be dead, but a shutdown socket is still alive.
- char[]
toString
();
- bool
blocking
();
- getter
- void
blocking
(bool byes);
- setter
- AddressFamily
addressFamily
();
- Socket
bind
(Address addr);
- Socket
connect
(Address to);
- Socket
listen
(int backlog);
- need to bind() first
- Socket
accept
();
- Accept an incoming connection. If the socket is blocking,
accept
waits for a connection request. Throws SocketAcceptException if unable
to
accept
. See accepting for use with derived classes.
- Socket
shutdown
(SocketShutdown how);
- The
shutdown
function shuts down the connection of the socket.
Depending on the argument value, it will:
- stop receiving data for this socket. If further data
arrives, it is rejected.
- stop trying to transmit data from this socket. Also
discards any data waiting to be sent. Stop looking for
acknowledgement of data already sent; don't retransmit
if any data is lost.
- Socket
setLingerPeriod
(int period);
- Tango:
added
- Socket
setAddressReuse
(bool enabled);
- Tango:
added
- Socket
setNoDelay
(bool enabled);
- Tango:
added
- void
joinGroup
(IPv4Address address, bool onOff);
- Helper function to handle the adding and dropping of group
membership.
Tango:
Added
- void
detach
();
- calling shutdown() before this is recommended for connection-
oriented sockets
- Address
newFamilyObject
();
- static char[]
hostName
();
- Tango:
added this to return the hostname
- static uint
hostAddress
();
- Tango:
added this to return the default host address (IPv4)
- Address
remoteAddress
();
- Address
localAddress
();
- const int
ERROR
;
- Send or receive error code.
- int
send
(void[] buf, SocketFlags flags = cast(SocketFlags)0);
- Send data on the connection. Returns the number of bytes actually
sent, or ERROR on failure. If the socket is blocking and there is no
buffer space left,
send
waits.
- int
sendTo
(void[] buf, SocketFlags flags, Address to);
int
sendTo
(void[] buf, Address to);
int
sendTo
(void[] buf, SocketFlags flags = cast(SocketFlags)0);
- Send data to a specific destination Address. If the destination address is not specified, a connection must have been made and that address is used. If the socket is blocking and there is no buffer space left,
sendTo
waits.
- int
receive
(void[] buf, SocketFlags flags = cast(SocketFlags)0);
- Receive data on the connection. Returns the number of bytes actually
received, 0 if the remote side has closed the connection, or ERROR on
failure. If the socket is blocking,
receive
waits until there is data
to be received.
- int
receiveFrom
(void[] buf, SocketFlags flags, Address from);
int
receiveFrom
(void[] buf, Address from);
int
receiveFrom
(void[] buf, SocketFlags flags = cast(SocketFlags)0);
- Receive data and get the remote endpoint Address. Returns the number of bytes actually received, 0 if the remote side has closed the connection, or ERROR on failure. If the socket is blocking,
receiveFrom
waits until there is data to be received.
- int
getOption
(SocketOptionLevel level, SocketOption option, void[] result);
- returns the length, in bytes, of the actual result - very
different from getsockopt()
- Socket
setOption
(SocketOptionLevel level, SocketOption option, void[] value);
- protected static void
exception
(char[] msg);
- Tango:
added this common function
- protected static void
badArg
(char[] msg);
- Tango:
added this common function
- static int
select
(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, timeval * tv);
- SocketSet's are updated to include only those sockets which an
event occured.
Returns the number of events, 0 on timeout, or -1 on error
for a connect()ing socket, writeability means connected
for a listen()ing socket, readability means listening
Winsock:
possibly internally limited to 64 sockets per set
- static int
select
(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, TimeSpan time);
-
select
with specified timeout
- static int
select
(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError);
-
select
with maximum timeout
- static timeval
toTimeval
(TimeSpan time);
- Handy utility for converting TimeSpan into timeval
- class
Address
;
- Public interface ...
- static void
exception
(char[] msg);
- Tango:
added this common function
- class
UnknownAddress
: tango.net.Socket.Address;
- Public interface ...
- protected sockaddr *
name
();
- protected int
nameLen
();
- AddressFamily
addressFamily
();
- char[]
toString
();
- class
NetHost
;
- Public interface ...
- protected void
validHostent
(hostent * he);
- void
populate
(hostent * he);
- bool
getHostByName
(char[] name);
- bool
getHostByAddr
(uint addr);
- bool
getHostByAddr
(char[] addr);
- class
IPv4Address
: tango.net.Socket.Address;
- Public interface ...
- struct
sockaddr_in
;
- protected sockaddr *
name
();
- protected int
nameLen
();
- this();
- AddressFamily
addressFamily
();
- ushort
port
();
- uint
addr
();
- this(char[] addr, int port = PORT_ANY);
- -port- can be PORT_ANY
-addr- is an IP address or host name
- this(uint addr, ushort port);
- this(ushort port);
- synchronized char[]
toAddrString
();
- char[]
toPortString
();
- char[]
toString
();
- static uint
parse
(char[] addr);
- -addr- is an IP address in the format "a.b.c.d"
returns ADDR_NONE on failure
- class
SocketSet
;
- Public interface ...
- this(uint max);
- this();
- void
reset
();
- void
add
(socket_t s);
- void
add
(Socket s);
- void
remove
(socket_t s);
- void
remove
(Socket s);
- int
isSet
(socket_t s);
- int
isSet
(Socket s);
- uint
max
();
-
max
sockets that can be added, like FD_SETSIZE
- fd_set *
toFd_set
();
|