Hello,
I've just started with C++ and I'm creating a program where I can start and stop music on my PC with my mobile phone. Everything works fine in the program for my phone (client), but it seems that the server (PC) doesn't start to play the music. I'm using the Windows SDK v7.1, 'cause I want to use the Windows Media Player to play the music.

#include "stdafx.h"
#include "MTServer.h"
#include <iostream>
#include "atlbase.h"
#include "atlwin.h"
#include "wmp.h"

#include <stdlib.h>
#include <string>
#include "atlstr.h"
#include "comutil.h"



/*
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
*/
CWinApp theApp;
SOCKET server;
CComPtr<IWMPControls> spControls;
CComPtr<IWMPPlayer> spPlayer;
using namespace std;

UINT  MTServerThread(LPVOID pParam);
UINT  ClientThread(LPVOID pParam);

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    CoInitialize(NULL);

    HRESULT hr = S_OK;
    CComBSTR bstrVersionInfo; // Contains the version string.

    hr = spPlayer.CoCreateInstance( __uuidof(WindowsMediaPlayer), 0, CLSCTX_INPROC_SERVER );
    //spPlayer->get_controls(&spControls);
    if(SUCCEEDED(hr))
    {
        hr = spPlayer->get_versionInfo(&bstrVersionInfo);
    }
    if(SUCCEEDED(hr))
    {
        // Show the version.
        COLE2T pStr(bstrVersionInfo);
        cout << pStr << endl;
    }
    int nRetCode = 0;    

    cout << "Press ESCAPE to terminate program\r\n";
    hr = spPlayer->get_controls(&spControls);
    if(SUCCEEDED(hr))
    {
        cout << "get_controls successfull!" << endl;
    }
    AfxBeginThread(MTServerThread,0);

    while(_getch()!=27);

    // Clean up.
    //spPlayer.Release();
    //CoUninitialize();

    closesocket(server);
    WSACleanup();
    return nRetCode;
}


UINT  MTServerThread(LPVOID pParam)
{        
    WSADATA wsaData;
    sockaddr_in local;
    int wsaret=WSAStartup(0x101,&wsaData);
    if(wsaret!=0)
    {
        return 0;
    }
    local.sin_family=AF_INET;
    local.sin_addr.s_addr=INADDR_ANY;
    local.sin_port=htons((u_short)4444);
    server=socket(AF_INET,SOCK_STREAM,0);
    if(server==INVALID_SOCKET)
    {
        return 0;
    }
    if(bind(server,(sockaddr*)&local,sizeof(local))!=0)
    {
        return 0;
    }
    if(listen(server,10)!=0)
    {
        return 0;
    }

    SOCKET client;
    sockaddr_in from;
    int fromlen=sizeof(from);

    while(true)
    {
        client=accept(server,(struct sockaddr*)&from,&fromlen);
        AfxBeginThread(ClientThread,(LPVOID)client);    
    }    

    return 0;
}

UINT  ClientThread(LPVOID pParam)
{
    char buff[60];
    char buff3[60] = "Show me 10.mp3";
    int n;
    HRESULT hr = S_OK;
    //int x;
    SOCKET client=(SOCKET)pParam;
    //strcpy(buff,"#Server Ready.\r\n");
    send(client,buff3,strlen(buff3),0);
    while(true)
    {
        n=recv(client,buff,strlen(buff),0);

        if(strcmp(buff, "play") == 0)
        {
            spControls->play();
            cout << "play" << endl;
        }
        else if(strcmp(buff, "play") != 0)
        {
            CString string1 = "E:\\Users\\Kevin\\Music\\Musiklk\\";
            string1 += buff3;

            // Convert to a char*
            const size_t newsize = 200;
            char nstring[newsize];
            strcpy_s(nstring, string1);
            strcat_s(nstring, "");
            //cout << nstring << endl;

            // Convert to a wchar_t*
            // You must first convert to a char * for this to work.
            size_t origsize = strlen(string1) + 1;
            size_t convertedChars = 0;
            wchar_t wcstring[newsize];
            mbstowcs_s(&convertedChars, wcstring, origsize, string1, _TRUNCATE);
            wcscat_s(wcstring, L"");
            //wcout << wcstring << endl;

            BSTR MyBstr = SysAllocString(wcstring);
            spPlayer->put_enabled(true);
            hr=spPlayer->put_URL(MyBstr);
            if(SUCCEEDED(hr))
            {
                cout << "URL Loaded!" << endl;
            }
            //spControls->play();
        }
        cout << buff << endl;
        if(n==SOCKET_ERROR )
            break;
        buff[n]=0;
    }        

    closesocket(client);
    return 0;
}

Recommended Answers

All 5 Replies

I see that is an MFC program. What version of the Microsoft compiler are you using? If you want anyone to test out your program you need to zip up the entire project directory and upload it here (us the Advanced Editor options to do that). But first delete all the compiler-generated files, such as *.obj and *.pch.

I'm using VS2010. I've to folders which are named Debug and Release. Should I just delete everything inside both of them?

just delete both of those folders. The IDE will re-create them. And don't forget to remove the precompiled header file -- you will easily see it because of its size.

Sorry, but I don't have that version of the compoiler/IDE.

OK, uploaded it now. I hope I did everything right..

I'm now using a 3rd party audio library instead of WMP. Stil haven't gotten this to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.