CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2007
    Posts
    19

    How to create UDP Packets

    Hi,

    I want to send a image from server to client using UDP. How to send it? How to create packet ?

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: How to create UDP Packets

    What have you tried, and what in your attempts is not working?

    Mike

  3. #3
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: How to create UDP Packets

    Find this book:
    "Unix Network Programming" by Richard Stevens.
    This is very old book (i read on 1995), but very useful to understanding
    socket programming including UDP.

  4. #4
    Join Date
    Aug 2007
    Posts
    19

    Re: How to create UDP Packets

    Hi, i want to divide image file into packets. i did this in text file it worked fine but its not working in PNG file. Why its not working? i am unable to create copy of the image. Whats wrong with this code!

    CODE :

    #include <iostream>
    #include <fstream>

    using namespace std;

    int main() {
    f1.open("D:\\xx.png",ios::binary);
    if(!f1.is_open()) {
    cout<<"Unable to open file\n";
    return 0;
    }

    f1.seekg(0,ios::end);
    int length = f1.tellg();
    f1.seekg(0,ios::beg);


    fstream f2("xxx.png",ios:ut|ios::binary);
    const int nBytes = 30720;
    char *buffer = new char [nBytes + 1];
    int nRead(0);
    const int bytes = 30720;
    char buffer[bytes + 1];

    while(!f1.eof()) {
    nRead = f1.read(buffer,bytes).tellg ();
    if (nRead <= -1) {
    int temp = length % bytes;
    buffer[temp] = 0;
    f2<<buffer;
    }
    else {
    buffer[bytes] = 0;
    f2<<buffer;
    }
    }
    f1.close();
    f2.close();
    }
    </code>
    Last edited by rajprabhu2k; August 27th, 2007 at 07:24 AM.

  5. #5
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: How to create UDP Packets

    For one thing, this has nothing to do with networks.

    For a second thing, you are treating a binary stream of data as if it was a null-terminated string, which it is not:
    Code:
    if (nRead <= -1) {
    int temp = length % bytes;
    buffer[temp] = 0;  // why are you null-terminating this???
    f2<<buffer;
    }
    There might be others too ...

    Mike

    PS: Nice try with code tags. Use square brackets, not angle brackets, like [ code ] [ /code ] (without the spaces)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured