UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2006-10-22 11:26 AM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: Win32 and file descriptors?!

Hi there!

I'm trying to use standard functions, that operates on file descriptors (close (), open (), read() etc.). I had a problem with read () function, which didn't work with large buffers, but it works with smaller buffer, so that's ok.

To search a solution for this i went to msdn.*********.com, and I was really surprised, because I didn't found any note in Microsooft  C Library API documentation about raw file descriptors and it's functions, there's no documentation for functions like read () or write () :confused: (http://msdn2.*********.com/en-us/library/ms859613.aspx).

Does it mean, that there is no official file descriptor support in Windows?! Can someone explain it for me please.

So far I'm using DevC++ with Mingw.

Offline

#2 2006-10-22 12:59 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: Win32 and file descriptors?!

MSWindows is not Unix. Filedescriptors are a unix thing. The functions you mentioned are POSIX functions. What are supported are C file functions like fopen etc.

As for your read() problem, read doesn't necessarily fill your whole buffer at once, so just call it again for the remaining data. How much it did read will be returned.

Offline

#3 2006-10-22 02:22 PM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: Win32 and file descriptors?!

Ok, I changed my code and now it access files via C language I/O (fopen, fwrite etc.), but my code still doesn't work properly. 'fread ()' works very strange, because it read only a few bytes of file and than it only return 0 as return value.
Some parts of my code:

This opens file and it doesn't return any errors:

data->file = g_fopen (file_path, "r");
    if (data->file == NULL) {
        gui_show_message_dlg (GUI_WARNING, _("Faild to send file"),
            _("Can't open file, which you want to send."));
        goofy_send_file_free (data);
        return NULL;
    }

And this reads file (under Linux it works fine):

gchar               buffer   [10240]; 
    gchar               *error_msg;
    gint                sent_size = 0;
    gint                return_val;
    gdouble             progress = 0.0;
    gdouble             progress_gui = 0.0;
    
    gdk_threads_enter ();
    /***  CUT  ***/
    gdk_threads_leave ();
        
    while (sent_size < data->file_size)   {   
        /* cut */       
            /* Read piece of file */
            return_val = fread (buffer, 1, sizeof (buffer), data->file); 
            [color="Green"]printf (">>>>>>>  %d\n", return_val);[/color]
            if (return_val > 0)  {
                /***  CUT  ***/

                sent_size += return_val;
                
                /* Update progress bar */
                progress = (100.0 * sent_size) / data->file_size;
                progress_gui = progress / 100.0;
                
                [color="DarkOrange"]printf (">>>  %lf  | %d | %d | %lf\n", progress, sent_size, data->file_size, progress_gui);[/color]
 
                 /***  CUT  ***/
           }
    }

Like you see I use GTK+/GLib in my program, but I don't think, that's the reason of these behavior. Maybe it's because of using GCC port and Mingw for win32?!

Here is output from Windows XP:
   [color="Green"]>>>>>>>  10240[/color]
[color="DarkOrange"]>>>  0,001394  | 10240 | 734701568 | 0,000014[/color]
>>>>>>>  69
>>>  0,001403  | 10309 | 734701568 | 0,000014
>>>>>>>  0
>>>>>>>  0
>>>>>>>  0
>>>>>>>  0
>>>>>>>  0
>>>>>>>  0

Offline

#4 2006-10-22 05:45 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: Win32 and file descriptors?!

Is this a binary file, by any chance?  If so, I think you need to use "rb" instead of
just "r" in your fopen(), because Windoze stupidly treats text files differently than
binary files...

(And, I thought the raw FD-based I/O functions were standard everywhere there
was a C implementation...  But, you know, leave it to MS to screw something up, if
anyone can... *shrug*)

(Oh, and Michael, did you really go and add MS to the censor list, as I was joking
about in a previous thread??  Based on the link in the first post here, I'm guessing
yes... ;-))

Offline

#5 2006-10-23 06:46 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: Win32 and file descriptors?!

You didn't set data->file_size correctly. Or do you really have a 734 Mb big file?

Other than that the output is as expected. The first call your whole buffer is filled, the second call the remaining data is read. I think your real filesize is 10309.

(I can't find 'microsoft' in the censored words list (which is empty), so either Michael is cunning or tomcio is having a big laugh. Or I'm blind or the software is buggy of course.)

Offline

#6 2006-10-23 08:55 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: Win32 and file descriptors?!

(It was in there...  I checked and removed it after posting my comment...  Michael
was obviously being a smart-ass... ;-))

Offline

#7 2006-10-23 09:07 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: Win32 and file descriptors?!

Well, I must admit, it has aesthetic advantages. ;-)

Offline

#8 2006-10-29 02:41 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Win32 and file descriptors?!

>> Trying to look innocence <<


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

Board footer

Powered by FluxBB