我的应用程序发送少量文本(命令到服务器)或32字节的语音数据块。现在我只是使用套接字OutputStream's
写。然而,问题是Android Java似乎喜欢自己发送第一个字节。例如:
要解决这个分裂问题,我会在每一行前加一个"扔掉"人物#。所以前面的例子发送为:
问题变成当我想发送32个字节的语音时,它作为一个1字节的数据包发送,然后发送一个31字节的数据包。由于TCP / IP开销,这些1字节数据包会浪费大量数据。根据{{3}},这意味着当我使用64 + 32 = 96时,我将使用(64 + 1)+(64 + 31)= 160字节作为我的32字节语音块。这意味着我将使用1.67比我更应用的X数据(在加拿大)将花费我非常可观的一分钱。
有没有办法强制将所有32个字节作为一个数据包发送?
以下是发送的Android代码:
int totalRead = 0, dataRead;
while (totalRead < WAVBUFFERSIZE)
{//although unlikely to be necessary, buffer the mic input
dataRead = wavRecorder.read(wavbuffer, totalRead, WAVBUFFERSIZE - totalRead);
totalRead = totalRead + dataRead;
}
int encodeLength = AmrEncoder.encode(AmrEncoder.Mode.MR122.ordinal(), wavbuffer, amrbuffer);
try
{
Vars.mediaSocket.getOutputStream().write(amrbuffer, 0, encodeLength);
}
catch (Exception e)
{
Utils.logcat(Const.LOGE, encTag, "Cannot send amr out the media socket");
}
以下是用于接收的C / C ++代码:
mediaRead = 0; //to know how much media is ACTUALLY received. don't always assume MAXMEDIA amount was received
bzero(bufferMedia, MAXMEDIA+1);
alarm(ALARMTIMEOUT);
do
{//wait for the media chunk to come in first before doing something
returnValue = SSL_read(sdssl, bufferMedia, MAXMEDIA-mediaRead);
if(returnValue > 0)
{
mediaRead = mediaRead + returnValue;
}
int sslerr = SSL_get_error(sdssl, returnValue);
switch (sslerr)
{
case SSL_ERROR_NONE:
waiting = false;
eventful = true; //an ssl operation completed this round, something did happen
break;
//other cases when necessary. right now only no error signals a successful read
}
} while(waiting && SSL_pending(sdssl));
alarm(0);
if(alarmKilled)
{
alarmKilled = false;
cout << "Alarm killed SSL read of media socket\n";
}
其中MAXMEDIA = 1024,因此对于32字节的语音数据来说有足够的位置