1. Create new socket

Am I doing something wrong, is this a bug, or am I just crazy?

12.1.7.4 create

create said...

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

new topic     » topic index » view message » categorize

2. Re: Create new socket

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

new topic     » goto parent     » topic index » view message » categorize

3. Re: Create new socket

ghaberek said...

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.

new topic     » goto parent     » topic index » view message » categorize

4. Re: Create new socket

DerekParnell said...

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}?

new topic     » goto parent     » topic index » view message » categorize

5. Re: Create new socket

euphoric said...
DerekParnell said...

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

new topic     » goto parent     » topic index » view message » categorize

6. Re: Create new socket

jeremy said...
euphoric said...
DerekParnell said...

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 success: {845793,938457}

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.

new topic     » goto parent     » topic index » view message » categorize

7. Re: Create new socket

euphoric said...

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

new topic     » goto parent     » topic index » view message » categorize

8. Re: Create new socket

jeremy said...

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... smile 
end function 
new topic     » goto parent     » topic index » view message » categorize

9. Re: Create new socket

Or something you can already do:

object s = create() 
if not socket(s) then  
   writefln(get_text(nnn), s ) 
else 
   -- process normally 
end if 
new topic     » goto parent     » topic index » view message » categorize

10. Re: Create new socket

euphoric said...

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

new topic     » goto parent     » topic index » view message » categorize

11. Re: Create new socket

AndyDrummond said...
euphoric said...

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.

new topic     » goto parent     » topic index » view message » categorize

12. Re: Create new socket

euphoric said...

You present a good argument Andy.

I blush with pleasure ...

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu