You know what?
Here is my single-shot "web server":
Code:
void MainL()
{
_LIT(KHelloWorld,"Hello World");
_LIT(KFormat,"%d\n");
CConsoleBase *console=Console::NewL(KHelloWorld,TSize(KConsFullScreen,KConsFullScreen));
CleanupStack::PushL(console);
RSocketServ sockserv;
User::LeaveIfError(sockserv.Connect());
CleanupClosePushL(sockserv);
RConnection connection;
User::LeaveIfError(connection.Open(sockserv));
User::LeaveIfError(connection.Start());
CleanupClosePushL(connection);
RSocket servsock;
User::LeaveIfError(servsock.Open(sockserv,KAfInet,KSockStream,KProtocolInetTcp,connection));
CleanupClosePushL(servsock);
TSoInetInterfaceInfo ifinfo;
TPckg<TSoInetInterfaceInfo> ifinfopkg(ifinfo);
TSoInetIfQuery ifquery;
TPckg<TSoInetIfQuery> ifquerypkg(ifquery);
TInetAddr addr;
TUint32 iap;
_LIT(KIapId,"IAP\\Id");
User::LeaveIfError(connection.GetIntSetting(KIapId, iap));
User::LeaveIfError(servsock.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
while(KErrNone==servsock.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, ifinfopkg))
{
ifquery.iName = ifinfo.iName;
if(KErrNone!=servsock.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, ifquerypkg))
break;
if (ifquery.iZone[1] == iap)
{
if(ifinfo.iAddress.Address() > 0)
{
addr = ifinfo.iAddress;
break;
}
}
}
{
TBuf <100> buf;
addr.Output(buf);
_LIT(KNewLine,"\n");
buf.Insert(0,KNewLine);
buf.Append(KNewLine);
console->Printf(buf);
}
RSocket socket;
User::LeaveIfError(socket.Open(sockserv));
CleanupClosePushL(socket);
TRequestStatus status;
User::LeaveIfError(servsock.SetLocalPort(80));
User::LeaveIfError(servsock.Listen(4));
servsock.Accept(socket,status);
User::WaitForRequest(status);
User::LeaveIfError(status.Int());
CBufFlat *flat=CBufFlat::NewL(20);
CleanupStack::PushL(flat);
TBuf8<100> buf8;
TSockXfrLength x;
_LIT8(KDoubleNewLine8,"\r\n\r\n");
while(status==KErrNone)
{
socket.RecvOneOrMore(buf8,0,status,x);
User::WaitForRequest(status);
flat->InsertL(flat->Size(),buf8);
console->Printf(_L("%d %d\n"),buf8.Length(),flat->Size());
if(flat->Size()>=4 && flat->Ptr(flat->Size()-4).Compare(KDoubleNewLine8)==0)
break;
}
flat->ResizeL(flat->Size()*2);
TPtr8 ptr8=flat->Ptr(0);
ptr8.SetLength(ptr8.Length()/2);
TPtr ptr=ptr8.Expand();
console->Write(ptr);
_LIT8(KResponse,"HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\nContent-Length: 5\r\n\r\nHello");
socket.Write(KResponse(),status);
User::WaitForRequest(status);
CleanupStack::PopAndDestroy(5); // flat,socket,servsock,connection,sockserv
CleanupStack::PopAndDestroy(); // console
}
It says 'Hello' to anyone who asks.