Re: to bernie: more on mixedlib.e and other stuff
- Posted by cense <cense at MAIL.RU> Nov 21, 2000
- 406 views
On Tue, 21 Nov 2000, Bernie wrote: >> cense: >> Warning I could not test this code. >> >> Because I don't know exactly how the rest of your program is coded >> If I understand what you are doing you don't need AssociatePtr >> I have tried to show you what to do in general. >> I hope this helps. >> Bernie Well im still working on that socket wrapper for linux. I have not worked on it in a while and just discovered this during testing. >> -- include mixedlib.e >> >> -- define the following structures outside of the function >> ----------------------------------------------------------------- >> pointer generic_sockaddr >> generic_sockaddr = >> struc("sa_family_t:ushort: 1 "& -- identifies format of address >> data >> "sa_data : char: 256 ",HIGH) -- address buffer varies and can be >> -- greater than 14 bytes so use 256 >> >> ----------------------------------------------------------------- >> pointer addr_of_size_ptr -- upon RETURN contains (in bytes) address size >> addr_of_size_ptr = struc("length:int:1",HIGH) >> >> ----------------------------------------------------------------- >> >> global -- assuming a socket has been established >> function accept( int socket , pointer client, pointer client_length) >> -- >> int FD -- file_discriptor >> >> FD = c_func( accept_, { socket, client, client_length} ) >> >> if FD < 0 then >> return -1 -- error condition >> else >> return FD -- return FILE DISCRIPTOR use for reading an writing/read >> socket >> end if >> -- >> end function >> >> constant AF_INET = 2 -- >> pointer client1, client2 >> pointer client1_size_ptr1, client2_size_ptr1 >> >> -- create the client structures >> client1 = dups(generic_sockaddr,HIGH) -- "C" socketaddr &client; >> client1_size_ptr = dups(addr_of_size_ptr,HIGH) -- "C" &client_length >> >> Sets(client1,"sa_family_t",AF_INET) -- set the address family FORMAT >> -- so "C" knows what format to use >> int client1_FD >> -- get the file discriptor >> client1_FD = accept( socket, client1, client1_size_ptr) >> -- Now you can use the client1_FD to do read() or write() >> -- To the get the address of the client into a sequence do this: >> pointer client1_str_name >> sequence client1_seq_name >> >> -- allocate a string buffer >> client1_str_name = string(256) >> -- copy the address into the buffer this also gets rid of any garbage >> -- returned in the structure >> strncpy(client1_str_name, loc(client, "sa_data") , client1_size_ptr) >> -- You can use this string with any of the string functions in mixedlib.e >> -- or convert it to a sequence to use in Euphoria >> client1_seq_name = str2seq(client1_str_name) >> >> -- .... talk back and forth >> -- When you are DONE communicating with the client be sure to >> closeFD(client1_FD) >> free(client1) >> free(client1_size_ptr) >> free(client1_str_name) I understand all of your code above just fine. There is one thing that i need clarrified though. When i look at online C socket tutorials for berkley sockets i see the author always typecast a sockaddr_in structure to sockaddr and then pass it to accept( ) instead of just using a sockaddr struct in the first place What would be the use of this? -- evil, corruption and bad taste ^[cense]