本帖最后由 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();
Symbian C++ 代码:
主程序
CSocketListenEngine* engine = CSocketListenEngine::NewL();
engine->StartEngineL();
CActiveScheduler::Start();
CSocketListenEngine代码:
void CSocketListenEngine::ConnectL()
{
TInt err;
err=iListen.Open(iSession, KAfInet, KSockStream, KProtocolInetTcp);