Open Source Development Tutorials | Newsletter Signup | About Us
Search  

Go Back   Open Source Tutorials Forum > Programming Languages > C and C++ Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-27-2006, 11:03 AM
americanguy americanguy is offline
Junior Member
 
Join Date: Dec 2006
Posts: 1 americanguy is on a distinguished road
Default mp 3 search client

here i dont know how to correct the error here can any one help me in this ?

Server returns search result (searchSongResultStruct) to the client
COMMENT: The client sends the request to the server, however, the server does not connect to the database (anymore). It does not send the results to the client and the client does not receive the information correctly)





#include "client.h"


MP3SearchClient::MP3SearchClient()
{
port=20248;
choice=-1;
}

void MP3SearchClient::Getoption()
{
cout<< "Make a selection:"<<endl;
cout<< "1:\tSend SearchSongResultStruct + 2*ID3V1Struct" <<endl;
cout<< "2:\tSend Mp3 file located c:\\test.mp3" <<endl;
cout<< "> " ;
cin >> choice;
cout<<choice<<endl;
}

short MP3SearchClient::CTCPSocket()
{
/* create an IPv4/TCP socket */
client = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( client == INVALID_SOCKET )
{
WSACleanup( );
DispMsg(EXIT_FAILURE02);
return EXIT_FAILURE;
}
return SUCESS;
}

short MP3SearchClient::LaunchClient()
{
retval = WSAStartup( 0x101, &wsaData );
if( retval != 0 )
{
DispMsg(EXIT_FAILURE01,retval);
return EXIT_FAILURE;
}

/* we assume that the server, in this example, is located on the local computer */
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons( port );
serveraddr.sin_addr.s_addr = inet_addr( "172.17.9.67" );

cout<< "connecting to: "<< inet_ntoa( serveraddr.sin_addr )<<":"<< port <<endl;

/* connect to the server */
if( connect( client, (struct sockaddr*)&serveraddr, sizeof( serveraddr ) ) == SOCKET_ERROR )
{
closesocket( client );
WSACleanup( );
DispMsg(EXIT_FAILURE03);
return EXIT_FAILURE;
}


memset( sendbuffer, 0x00, 4096 );

return SUCESS;
}

short MP3SearchClient::ExecuteOption()
{
switch(choice)
{
/* Send SearchSongResultStruct + 2*ID3V1Struct */
case 1:
{
SearchSongResultStruct *ssrs = NULL;
ID3V1Struct *id3 = NULL;

ssrs = (SearchSongResultStruct*) sendbuffer; /* setting pointer to sendbuffer */
ssrs->structid = ID_RESULT_STRUCT;
ssrs->nbOfResults = 2;

id3 = (ID3V1Struct*) (sendbuffer+sizeof(SearchSongResultStruct)); /* setting pointer to sendbuffer */

/* entering information */
strcpy( id3->band, "Abba" );
strcpy( id3->title, "Happy new year" );
id3->year = 1980;
id3->structid = ID_ID3V1_STRUCT;
id3++; /* stepping to the next id3 structure */
strcpy( id3->band, "Abba" );
strcpy( id3->title, "Chiquita" );
id3->year = 1979;
id3->structid = ID_ID3V1_STRUCT;

/* sending */
retval = send( client, sendbuffer, sizeof(ID3V1Struct)*ssrs->nbOfResults+sizeof(SearchSongResultStruct), 0 );

if( retval == SOCKET_ERROR )
{
closesocket( client );
WSACleanup( );
DispMsg(EXIT_FAILURE04);
return -1;
}

break;
}

/* Send Mp3 file located c:\\test.mp3 */
case 2:
{
/* open the mp3 file */
std::ifstream file( "c:\\test.mp3", std::ios::in|std::ios::binary|std::ios::ate );

if( file.is_open( ) )
{
/* creating song struct */
SongStruct *ss = NULL;
ss = (SongStruct*) sendbuffer;
ss->structid = ID_SONG_STRUCT;
int filesize = (int) file.tellg( );
ss->size = filesize;

/* set the file pointer to the beginning of the file */
file.seekg( 0, std::ios::beg );

if( filesize >= (sizeof(sendbuffer)-sizeof(SongStruct)) )
{
/* if the file is larger than the sendbuffer */
file.read( (sendbuffer+sizeof(SongStruct)), sizeof(sendbuffer)-sizeof(SongStruct) );
}
else
{
/* if the file fits in the sendbuffer */
file.read( (sendbuffer+sizeof(SongStruct)), filesize );
}

send( client, sendbuffer, sizeof(sendbuffer), 0 );

if( retval == SOCKET_ERROR )
{
closesocket( client );
WSACleanup( );
DispMsg(EXIT_FAILURE04);
return -1;
}

int read = sizeof(sendbuffer)-sizeof(SongStruct);

/* keep sending until we've sent the entire file */
while( read < filesize )
{
int readsize = sizeof(sendbuffer);

/* it is always a good idea to erase old data from the sendbuffer */
memset( sendbuffer, 0x00, sizeof(sendbuffer) );

if( (int)(read + sizeof(sendbuffer)) > filesize )
readsize = filesize - read;

file.read( sendbuffer, readsize );
read += readsize;

send( client, sendbuffer, readsize, 0 );

if( retval == SOCKET_ERROR )
{
closesocket( client );
WSACleanup( );
DispMsg(EXIT_FAILURE04);
return -1;
}
}
file.close( );
}
break;
}

default:
DispMsg(EXIT_FAILURE04);
}
return SUCESS;
}

short MP3SearchClient::CloseSocket()
{
closesocket(client);
WSACleanup( );
return SUCESS;
}

void MP3SearchClient:ispMsg(short Type, int retval)
{
switch(Type)
{
case EXIT_FAILURE01:
cout<< "failed to initiate winsock: %i"<< retval <<endl;
break;
case EXIT_FAILURE02:
cout<< "failed to create valid socket"<<endl;
break;
case EXIT_FAILURE03:
cout<< "failed to connect"<<endl;
break;
case EXIT_FAILURE04:
cout<< "failed to transmit"<<endl;
break;
default:
cout<< "unknown command"<<endl;
break;
}

}
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



All times are GMT -5. The time now is 06:52 AM.


Powered by: vBulletin Version 3.0.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright 2004 - 2006 GrindingGears.com. All Rights Reserved.