Monday 15 March 2010

c - how to transfer pdf, mp3 or mp4 files using socket? -



c - how to transfer pdf, mp3 or mp4 files using socket? -

i have made 1 client server application in client sends file (i.e odt,pdf,mp3,mp4, etc) , server receives file.

i dividing file in chunks , transmits them in while loop. below have given main logic both client , server.

when loop-back 127.0.0.1, code works successfully.

but when runs client , server on 2 different pc, after transmitting file client exits server keeps receiving , have press cltr^c. size of file @ server side reaches on 1gb if file size @ client side around 4.2 mb.

and in loopback not getting such problem.

please tell me needed corrections.

client.c

#define size 512 // or else char sendbuff[size]; file *fr; fr = fopen("1.mp3","r"); while(!feof(fr)){ count = fread(sendbuff, size,1,fr); count = send(clientsd, sendbuff,size,0); //clientsd socket descriptor. } send(clientsd, "xyz", 3, 0); //sending '1'. tells server, transmission on now. close(fr);

server.c

#define size 512 // same client side char recvbuff[size]; file *fw; fw = fopen("2.mp3","w"); while(1){ count = recv(connsd, recvbuff, size,0); if(!strcmp(recvbuff,"xyz")) break; fwrite(recvbuff,size, 1, fw); memset(recvbuff,0,size); } printf("exit while\n"); fclose(fw);

any other simple , efficient way ?

note : have changed question. here answers on old question have transmitted "1" instead of "xyz". error.

the obvious problem stop status on server side.

you assume if first byte received '1' (0x31) transfer over, might byte of info (if first byte of chunk in file '1'). need other way signal end of file. 1 possibility utilize wrapping each packet sent, example, before each packet send specific value (for illustration '1') followed length, , when transfer finish send '0' signal transfer completed.

the other problems can see that:

you open files read text ("r") , write text ("w") stop processing if eof sequence appears in middle of file, instead need open them read/write binary ("rb" / "wb" respectively).

you utilize chunks of 512 bytes, if file not multiple of 512 bytes?

c sockets ipc

No comments:

Post a Comment