1. Create new socket
- Posted by ghaberek (admin) Aug 25, 2009
- 1014 views
Am I doing something wrong, is this a bug, or am I just crazy?
Returns:
An atom, -1 on failure, else a supposedly valid socket id.
include std/socket.e as sock object s = sock:create( AF_INET, SOCK_STREAM, 0 ) ? s
{1952,11145624}
-Greg
2. Re: Create new socket
- Posted by jeremy (admin) Aug 25, 2009
- 1003 views
The docs are wrong. Originally it returned an atom, but that was later changed. The first element of that sequence is actually the socket, the second is a pointer to the sockets address. The internals, however, shouldn't need to be of a concern and may change in the future w/o notice. The important thing to know is that on failure an atom is returned giving the error code, otherwise, it's a valid socket object.
That socket object can be passed to any of the socket functions. They will use the "sequence" correctly.
Jeremy
3. Re: Create new socket
- Posted by DerekParnell (admin) Aug 25, 2009
- 949 views
Am I doing something wrong, is this a bug, or am I just crazy?
The docs are wrong. It should be an object that is returned. If this is -1 then an error occured. A valid socket happens to be a two-element sequence.
4. Re: Create new socket
- Posted by euphoric (admin) Aug 25, 2009
- 939 views
It should be an object that is returned. If this is -1 then an error occured. A valid socket happens to be a two-element sequence.
Why not return a sequence on FAIL as well, such as {-1,0}?
5. Re: Create new socket
- Posted by jeremy (admin) Aug 25, 2009
- 947 views
It should be an object that is returned. If this is -1 then an error occured. A valid socket happens to be a two-element sequence.
Why not return a sequence on FAIL as well, such as {-1,0}?
Why would you? The contents of the socket object shouldn't be of concern to the programmer. It contains no data that is of value to the euphoria program, only of value to the C socket functions, thus, the content of the object may change at any time (but will never cause the programmer problem, because if the internals change, then the internal C socket funcs would be changed as well).
Jeremy
6. Re: Create new socket
- Posted by euphoric (admin) Aug 25, 2009
- 938 views
It should be an object that is returned. If this is -1 then an error occured. A valid socket happens to be a two-element sequence.
Why not return a sequence on FAIL as well, such as {-1,0}?
Why would you?
Because then I don't have to use the ambiguous "object" type identifier.
Instead of
x = thefunc() if not atom(x) then...
I can use
x = thefunc() if x[1] > 0 then...
This assumes that a sequence type is more efficient than using an object type.
But it also just seems more logical and reasonable to return a consistent result.
On fail: {-1,0}, or even {-1}
Throughout BBCMF, function results are returned as follows:
{ 1, result } for success, where result can then be in any format
{ 0, result } for fail, where result is usually an error message or code
if result[1] then -- process result[2] else -- display result[2] end if
It has made things quite efficient, especially during development.
The primary thing is, I get rid of an ambiguous and inefficient "object" type declaration.
7. Re: Create new socket
- Posted by jeremy (admin) Aug 25, 2009
- 954 views
The primary thing is, I get rid of an ambiguous and inefficient "object" type declaration.
That's one thing I am unsure of and would like to have comments... There is a type socket. So, you could:
socket s = create()
However, if create() fails, it'll return a type error. I suppose the socket type could be modified to allow it to also be an atom, then you could:
socket s = create() if atom(s) then printf(1, "Could not create socket, error number: %d\n", { s }) end if
Jeremy
8. Re: Create new socket
- Posted by euphoric (admin) Aug 25, 2009
- 933 views
socket s = create() if atom(s) then printf(1, "Could not create socket, error number: %d\n", { s }) end if
Or:
socket s = create() if not valid(s) then -- a sockets.e function to validate your socket* printf(1, "Error %d:%s", { get_error_num( s ), get_error_msg( s ) }) -- sockets.e error management -- or printf(1, "Error %d:%s", get_error(s) ) -- or get_error() could return {errnum,errmsg} -- or printf(1, "Error %d:%s", s[2] ) -- on fail, s is {0,{errnum,errmsg}} else -- process normally end if
-- in sockets.e public function valid(object s) return sequence(s) and length(s) > 0 and s[1] -- hopefully this short-circuits... end function
9. Re: Create new socket
- Posted by DerekParnell (admin) Aug 25, 2009
- 905 views
Or something you can already do:
object s = create() if not socket(s) then writefln(get_text(nnn), s ) else -- process normally end if
10. Re: Create new socket
- Posted by AndyDrummond Aug 26, 2009
- 913 views
Why not return a sequence on FAIL as well, such as {-1,0}?
One good reason is that if you forget to check for errors, and the error return is a sequence, then the program could go on quite a way before the error gets discovered, and the failure would be totally confusing. If the error return is an atom, then the first time the error value was used as if valid then it would fail and you would quickly see why. Then you would add your proper test for error at the right place! IMHO, data which is in error should not be usable as if it was correct.
If you read data from a file, you get a sequence - but at EoF you get an atom. Easily identified, but impossible to use as file data. This is a good and safe standard technique.
Andy
11. Re: Create new socket
- Posted by euphoric (admin) Aug 26, 2009
- 934 views
Why not return a sequence on FAIL as well, such as {-1,0}?
IMHO, data which is in error should not be usable as if it was correct.
You present a good argument Andy.
12. Re: Create new socket
- Posted by AndyDrummond Aug 26, 2009
- 913 views
You present a good argument Andy.
I blush with pleasure ...