Symbian socket问题
本帖最后由 no111 于 2011-03-27 23:32:41 编辑 J2ME 和 symbian c++ 程序进行socket通讯时,遇到这个问题:J2ME发数据给c++程序,c++程序可以正确收到数据,但c++回写数据却不成功,代码如下:
J2ME代码:
conn = (StreamConnection)Connector.open(url);
out = conn.openOutputStream();
byte[] buf = request.getBytes();
out.write(buf, 0, buf.length);
out.flush();
out.close();
byte[] data = new byte[256];
in = conn.openInputStream();
int actualLength = 0;
int mark = 0;
while(in.available() > 0 && (actualLength=in.read(data))!=(-1))
{
String tmp = new String(data);
tmp1 = textbox.getString();
textbox.setString(tmp1 + tmp);
System.arraycopy(data, 0, res, mark, actualLength);
mark += actualLength;
}
String tmp2 = textbox.getString();
textbox.setString(tmp2 + "\n" + "get data finish \n");
in.close();
CSocketListenEngine* engine = CSocketListenEngine::NewL();
engine->StartEngineL();
CActiveScheduler::Start();
void CSocketListenEngine::ConnectL()
{
TInt err;
err=iListen.Open(iSession, KAfInet, KSockStream, KProtocolInetTcp);
User::LeaveIfError(err);
User::LeaveIfError(iSocket.Open(iSession));
TInetAddr ServerAddrOnPort(ServerAddr, ServerPort);
iListen.Bind(ServerAddrOnPort);
}
void CSocketListenEngine::StartEngineL()
{
iListen.Listen(1);
iListen.Accept(iSocket, iStatus);
SetActive();
}
void CSocketListenEngine::RunL()
{
if(iStatus==KErrNone)
{
TRequestStatus status;
TSockXfrLength dummyLength;
TBool running = ETrue;
TBuf8<256> buffer;
iSocket.RecvOneOrMore(buffer, 0, status, dummyLength); // 这个地方可以正确得到数据
User::WaitForRequest(status);
if(status != KErrNone) User::Leave(KErrGeneral);
buffer.Copy(_L("123"));
// respappid = 1234 pStatusCode = 0;
status = KRequestPending;
iSocket.Write(buffer, status); // 问题:这里写数据就不成功
User::WaitForRequest(status);
if(status != KErrNone) User::Leave(KErrGeneral);
}
}