|
A
ServerSocket
is the Yoix representation of the Java ServerSocket class that servers use
to establish TCP connections to clients.
A server creates a
ServerSocket
variable, stores its address in the
localaddress
and
localport
fields, and then uses
accept
to build a
Socket
that can communicate with each accepted client.
Several incompatible changes introduced in release
1.4.0
of the Yoix interpreter will affect older applications that used the
DatagramSocket,
ServerSocket,
or
Socket
types.
Take a look at the description of the obsolete
port
field if you have an old application that is having trouble using a
ServerSocket.
The fields in a
ServerSocket
are:
| accept([Dictionary init]) |
A
Builtin
that waits for the next client to connect to the serversocket's
localaddress
and
localport
and returns a
Socket
that can be used to communicate with that client or
NULL
if
accept
timed out, was interupted, or encountered any other kind of error.
accept
automatically activates the serversocket if it is not
alive.
The optional
init
argument can be used to initialize fields in the
Socket
that
accept
returns.
| | alive |
An
int
that is
1
if the serversocket has been activated and
0
otherwise.
Storing
1
in
alive
activates the serversocket, which automatically binds it to
localaddress
and
localport.
Calling the
accept
built-in also activate the serversocket.
| | backlog |
An
int
specifying the maximum queue length that the serversocket will support when
connection requests come in faster than the server can handle.
Changing
backlog
while a serversocket is
alive
is not allowed and will result in an
invalidaccess
error.
| | localaddress |
A
String
that identifies network interfaces on the local machine
that the serversocket will bind to when it is activated.
The value should be the name of a network interface
(e.g.,
eth0
or
lo),
an IP address, a host name that can be resolved by DNS, or
NULL
(the default), which means the wildcard address that usually matches
any local address.
A serversocket that is bound to
localaddress
and
localport
will only accept connections that are directed to that address and port.
Reading
localaddress
when the serversocket is
alive
always returns an IP address.
Changing
localaddress
while a serversocket is
alive
is not allowed and will result in an
invalidaccess
error.
| | localname |
A read-only
String
that is the name of the host associated with the IP address read from
localaddress
when the serversocket is
alive
and
NULL
when it is not
alive.
| | localport |
An
int
that identifies the port that the serversocket will bind to when it is activated.
The value should be an integer between
0
(the default) and
65535,
inclusive.
A
localport
that is
0
means the system should pick an ephemeral port,
which usually does not make much sense for a server.
A serversocket that is bound to
localaddress
and
localport
will only accept connections that are directed to that address and port.
Changing
localport
while a serversocket is
alive
is not allowed and will result in an
invalidaccess
error.
| | port |
An obsolete field that was replaced by
localport
in the
1.4.0
release of the Yoix interpreter.
| | receivebuffersize |
An
int
that is the size, in bytes, of the receive buffer used by sockets that
are created by this serversocket, which will also be the size of the
TCP receive window that is advertised to the remote peer.
Reading returns the actual buffer size if the serversocket is
alive,
or the size that will be requested from the operating system
when it is activated.
Writing requests buffers of a certain size, however the request may or may
not be honored by the operating system.
Storing a negative number in
receivebuffersize
is ignored and makes no changes to the current setting.
Buffers larger than
64K
bytes are allowed but must be requested before the serversocket is activated,
otherwise remote peers of the sockets created by this serversocket may not be
able to take advantage of the extra space.
| | reuseaddress |
An
int
that is
1
if another socket can be bound to the same
localaddress
and
localport
pair immediately after this serversocket is closed and
0
otherwise.
Storing
0
in
alive
closes the serversocket, which puts it in a timeout state
(officially called the
TIME_WAIT
state) that can last several minutes.
During that time any attempt to reuse
localaddress
and
localport
by any process on your system will fail unless
resueaddress
was
1
when the serversocket was activated.
Changing
reuseaddress
while the serversocket is
alive
is allowed, but the results are not well defined.
| | timeout |
A
double
that specifies how long the
accept
built-in will wait, in seconds, before giving up.
A zero value disables timeouts and means
accept
will wait forever.
Writing is allowed, but does not affect an
accept
call that has already started.
Storing a negative number in
timeout
is ignored and makes no changes to the current setting.
|
Several permanent fields have not been documented and should not be
used in Yoix applications.
If you look closely you may notice that some fields in a
ServerSocket
are initialized with values, like
-1,
that the documentation says are explicitly ignored.
It is an approach that lets Java (and your operating system) pick values
when the serversocket is activated for fields that you choose not to initialize.
| |
| Example: |
The following program,
import yoix.*.*;
Socket client;
ServerSocket ss = {
int localport = 8888;
};
func(arg) {
printf("Called func: client=%O\n", arg);
}
do {
printf("accepting connections on %d\n", ss.localport);
if ((client = ss.accept()) != NULL) {
Thread handler;
handler.queue(func, client);
}
} while (ss.alive);
does not do any useful work, but it runs and you can easily verify
(e.g., by using telnet) that it really does start a new thread
that executes function
func
whenever a client connects to port 8888.
See the
Socket
documentation for another example.
| | |
| See Also: |
DatagramSocket,
getAddress,
getAllByName,
getHostAddress,
getHostName,
getInterfaceAddress,
getInterfaceAddresses,
isAnyLocalAddress,
isLinkLocalAddress,
isLoopbackAddress,
isMulticastAddress,
isSiteLocalAddress,
MulticastSocket,
Socket
|
|