Anything wrong with this?

Hello,
I'm trying to send a list of songnames to a client i built. Everything compiles fine, it even sends the first filename right. When the server begins to send the second filename it sends every filename as one. E.g:
First: Like the way.mp3
Second: Pump it up.mp3Billionaire.mp3...... it just continues like this.

I also print every filename with "cout", and there's everything fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
CFileFind finder;
    CString myDirectory = "E:\\Users\\****\\Music\\Musiklk\\";
    BOOL finding = finder.FindFile (myDirectory + CString ("\\*.mp3"));
    while (finding)
    {
        finding = finder.FindNextFile();
        CString fname = finder.GetFileName();
        char songname[200] = "";
        strncpy((char *) songname, (LPCTSTR) fname, sizeof(songname));
        send(client,songname,strlen(songname),0);
        cout << songname << endl;
    }
    finder.Close ();

BTW: I'm still learning C++....
Last edited on
And the client code?
I'm always suspect of C++ that does the following:

1. Uses non-standard types (CString, BOOL, LPCTSTR rather than std::string, bool, and const char*)
2. Uses char arrays rather than std::string.
3. Uses <cstring> function such as strncpy().

The problem with sending data the way you are is that the recipient has no idea what length the song title is. It isn't encoded automatically by send(). Google for "netstring" for a simple way to do that. Otherwise you probably want to use a higher-level protocol for passing messages between processes. You can use SOAP, XML-RPC, STOMP, AMQP, and many others.
Also, your strncpy() will be a problem if the length fname is >= 200. As PanGalactic said, you should try not to use strncpy(). But until you switch, make the last parameter of strncpy() to this: sizeof( songname ) - 1 to protect it.
Topic archived. No new replies allowed.