Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, you are sending widestrings, which means you have to use:</p> <pre><code>Len :=Length(mp3) * SizeOf(Char); </code></pre> <p>Secondly, you dont need the cast. This should do fine:</p> <pre><code>myStream.Write(mp3[1],Len); </code></pre> <p>Third, if tcpSocket is non-blocking, then you must not disable it directly after sending the stream, that will cut the connection before it even gets started. Use the events to know when the data is sent.</p> <p>To avoid all this, use TReader and TWriter on your streams. That way you dont have to care about string datasize on both sides, just the stream size.</p> <p>Tip: Always send a magic value (a constant value) first, that way you can check on the other side if it is a valid data-package.</p> <p>Here is one way you could do it:</p> <pre><code>Procedure TForm1.WriteMP3File(aFilename:string); var mData: TMemoryStream; mFile: TFilestream; mWriter: TWriter; Begin mData:=TMemoryStream.Create; try mFile:=TFileStream.Create(aFilename,fmOpenRead); try mWriter:=TWriter.Create(mData); try // Write header code, any number will do mWriter.WriteInteger($BABE); // Write the filename mWriter.WriteString(ExtractFileName(aFilename)); //Write size of MP3 file mWriter.WriteVariant(mFile.Size); //Push all data into the mData stream mWriter.FlushBuffer; //Append MP3 file mData.CopyFrom(mFile,mFile.Size); finally mWriter.Free; end; mData.Position:=0; socket.sendstream(mData); finally mFile.Free; end; finally mData.Free; end; end; </code></pre> <p>And reading it is done in reverse on the server:</p> <pre><code>Procedure TForm1.ReadMP3File(aMp3PlayPath:String;aStream:TStream); var mReader: TReader; mFileData: TFileStream; mName: String; mSize: Int64; Begin try mReader:=TReader.Create(aStream); try if mReader.ReadInteger=$BABE then Begin mName:=mReader.ReadString; mSize:=mReader.ReadVariant; end else raise exception.Create('Invalid datapackage error'); finally mReader.Free; end; if mSize&gt;0 then Begin mFileData:=TFileStream.Create(aMp3PlayPath + mName,fmCreate); try mFiledata.CopyFrom(aStream,mSize); finally mFiledata.Free; end; end; //Now you can play the file finally //Release stream or do it elsewhere aStream.Free; end; end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload