www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Can't get nonblocking socket.

reply Dawid =?ISO-8859-2?Q?Ci=EA=BFarkiewicz?= <arael fov.pl> writes:
Error: Unable to set socket blocking.

I was googlin' for that but I can't find what am I doing wrong. Maybe
someone can give me some advise.

I've found this only:
http://www.digitalmars.com/d/archives/digitalmars/D/2658.html

I've got glibc 2.3.4 - can be this similar problem?

Regards,
-- 
Dawid Ciężarkiewicz | arael
jid: arael fov.pl
Feb 08 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
------------pRYens0rN8F7TGgv8VITJW
Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8
Content-Transfer-Encoding: 8bit

On Tue, 08 Feb 2005 16:10:46 +0100, Dawid Ciężarkiewicz <arael fov.pl>  
wrote:
 Error: Unable to set socket blocking.

 I was googlin' for that but I can't find what am I doing wrong. Maybe
 someone can give me some advise.

 I've found this only:
 http://www.digitalmars.com/d/archives/digitalmars/D/2658.html

 I've got glibc 2.3.4 - can be this similar problem?

It does look like the same problem. I looked in std.socket and I see: # void blocking(bit byes) # { # version(Win32) # { # uint num = !byes; # if(SOCKET_ERROR == ioctlsocket(sock, FIONBIO, &num)) # goto err; # _blocking = byes; # } # else version(BsdSockets) # { # int x = fcntl(handle, F_GETFL, 0); # if(byes) # x &= ~O_NONBLOCK; # else # x |= O_NONBLOCK; # if(SOCKET_ERROR == fcntl(sock, F_SETFL, x)) # goto err; # } # return; //success # # err: # throw new SocketException("Unable to set socket blocking."); # } which is the error you're getting. I notice it's using fcntl to set the blocking state of the socket. On error it sets errno, so retrieving errno and/or the error message associated might tell us more. To do so call strerror(errno). See the attached file for an example. Ideally add this error string to the exception throw. You'd have to change std.socket and rebuild phobos. I have some code here which uses the 'ioctl' function instead of fcntl, eg. int nonblock = 1; ioctl(sock,FIONBIO,nonblock); so you/we could also try that. You'd have to change std.socket and rebuild phobos. Regan ------------pRYens0rN8F7TGgv8VITJW Content-Disposition: attachment; filename=syserror.d Content-Type: application/octet-stream; name=syserror.d Content-Transfer-Encoding: 8bit module lib.syserror; private import std.string; private import std.c.windows.windows; private import std.c.stdarg; extern (Windows) { DWORD FormatMessageA( DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPTSTR lpBuffer, DWORD nSize, va_list *Arguments ); static uint FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100; static uint FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; static uint FORMAT_MESSAGE_FROM_STRING = 0x00000400; static uint FORMAT_MESSAGE_FROM_HMODULE = 0x00000800; static uint FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; static uint FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; static uint FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF; WORD MAKELANGID(WORD p, WORD s) { return (((cast(WORD)s) << 10) | cast(WORD)p); } WORD PRIMARYLANGID(WORD lgid) { return (cast(WORD)lgid & 0x3ff); } WORD SUBLANGID(WORD lgid) { return (cast(WORD)lgid >> 10); } static uint LANG_NEUTRAL = 0x00; static uint SUBLANG_DEFAULT = 0x01; alias HGLOBAL HLOCAL; HLOCAL LocalFree(HLOCAL hMem); } extern (C) char *strerror(int); class SysError { static char[] msg(uint errcode) { char[] text; version(Windows) { if (errcode == ERROR_NOT_ENOUGH_MEMORY) text = "not enough memory"; else { LPVOID lpMsgBuf; DWORD r; r = FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, null, errcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language cast(LPTSTR)&lpMsgBuf, 0, null ); /* Remove \r\n from error string */ if (r >= 2) r -= 2; text = (cast(char *)lpMsgBuf)[0..r].dup; LocalFree(cast(HLOCAL)lpMsgBuf); } } else { if (errcode == ENOMEM) text = "not enough memory"; else { char* pemsg; uint r; pemsg = strerror(errcode); r = strlen(pemsg); /* Remove \r\n from error string */ if (pemsg[r-1] == '\n') r--; if (pemsg[r-1] == '\r') r--; text = pemsg[0..r].dup; } } return text; } } ------------pRYens0rN8F7TGgv8VITJW--
Feb 08 2005