Thread: writing a file to an open socket...

  1. #1
    Unregistered
    Guest

    Unhappy writing a file to an open socket...

    You know, just a few lines of code that can open a file, then write that file to the open socket (newsockfd in my case)...plz? Help?

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    If using WIN32 API file and memory functions

    Code:
    hFile=CreateFile(sFileName,GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL); //open the file
    //error check
    iSize=GetFileSize(hFile);//find its size
    pFile=GlobalAlloc(GHND,iSize+1); //alloc buffer for whole file and terminator 
    //error check
    sBuffer=(char *)GlobalLock(pFile);//lock the pointer down
    iReturn=ReadFile(hFile,sBuffer,iSize,&dwRead,NULL);//read whole file
    //error check
    send(newsockfd,sBuffer,iSize,dwFlags);
    //error check
    GlobalUnlock(pFile);
    pFile=GlobalFree(pFile);
    //error check
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Unregistered
    Guest

    Unhappy unix support?

    I plan on running this on unix. Is there any way you could rewrite it so it used stdio.h in unix/linux instead of a win32 api?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I'll show you mine if you show me yours.




    In otherwords, not without seeing that YOU tried and failed.

    I'm not here to write your code, unless you want to pay me (as the rest of my customers do).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Unregistered
    Guest

    here it goes...

    well, here is my crappy code.

    int ch, i = 0;

    while((ch = getchar()) != EOF)
    i ++;
    write(newsockfd,ch,1)

    I am not sure if it works (don't have access to my unix comp right now), but the problem is that it writes every character one at a time to the socket.

  6. #6
    Unregistered
    Guest

    ACK!

    ack!


    the right one:
    <code>
    int ch, i = 0;

    while((ch = getch(stdin)) != EOF)
    i ++;
    write(newsockfd,ch,1)
    </code>

  7. #7
    Unregistered
    Guest

    dangit

    I feel like a retard. Glad I am unregistered.


    int ch, i = 0;

    while((ch = getc(stdin)) != EOF)
    i ++;
    write(newsockfd,ch,1)

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: dangit

    Originally posted by Unregistered
    I feel like a retard. Glad I am unregistered.


    int ch, i = 0;

    while((ch = getc(stdin)) != EOF)
    i ++;
    write(newsockfd,ch,1)
    Here I go again...

    What exactly is this supposed to do? All your code does is read from stdin until you enter EOF (ctrl Z or D, depending, typicaly), and then it writes one character to your socket.

    You can't mean for this to happen. Why do you even have 'i' in your code example at all?

    Might I recommend Beej?

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    In UNIX, everything is a file, from your directory to your mouse. If you can write to a file on your file system, you could as well write to an open socket, the very same way, the only criteria is that the socket be opened and initialized properly.

    Also, as quzah said, check you code. One more thing, your parameter 'ch' to write() is not right, you should be passing the address of 'ch', not 'ch' itself:
    Code:
    while((ch = getc(stdin)) != EOF) 
    /* i ++;  */
    write(newsockfd, &ch, 1);
    OR

    Code:
    while((ch = getc(stdin)) != EOF) {
          i ++;
          write(newsockfd, &ch, 1);
    }

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Do not write the file byte by byte to the socket.

    Each write is a message to the OS on the recv end, each will have to have a packet header ect, further increasing the overhead. You will probably create more WOULDBLOCK's this way. (the socket is busy/full and the info will need to be buffered and sent later, by your app not by the OS)

    Use setsockopt() with the SO_SNDBUF flag (there must be a 'NIX version) to make sure you have a decent buffer size (I use 32K) and then read the file in buffer sized increments and write to the socket.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM