今天晚上实验了两种方法,一种是用Symbian自己的RSocket,另一种是用POSIX标准的 socket。
使用的是Carbide C++ 1.1
源代码如下:
1。RSocket版
/*
============================================================================
Name : Sniffer.cpp
Author :
Copyright : Your copyright notice
Description : Exe source file
============================================================================
*/
// Include Files
#include "Sniffer.h"
#include <e32base.h>
#include <e32std.h>
#include <e32cons.h> // Console
#include <in_sock.h>
#include <es_sock.h>
// Constants
_LIT(KTextConsoleTitle, "Console");
_LIT(KTextFailed, " failed, leave code = %d");
_LIT(KTextPressAnyKey, " [press any key]/n");
// Global Variables
LOCAL_D CConsoleBase* console; // write all messages to this
// Local Functions
LOCAL_C void MainL()
{
RSocketServ ss;
RSocket listenSocket;
TRequestStatus status;
TBuf8<3000> getBuff;
TBuf<3000> buff16;
User::LeaveIfError(ss.Connect());
User::LeaveIfError(listenSocket.Open(ss, KAfInet, KSockRaw, KProtocolInetIp)); //返回错误是-28:Band Name
console->Write(_L("Hello, world!/n"));
}
LOCAL_C void DoStartL()
{
// Create active scheduler (to run active objects)
CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
CleanupStack::PushL(scheduler);
CActiveScheduler::Install(scheduler);
MainL();
// Delete active scheduler
CleanupStack::PopAndDestroy(scheduler);
}
// Global Functions
GLDEF_C TInt E32Main()
{
// Create cleanup stack
__UHEAP_MARK;
CTrapCleanup* cleanup = CTrapCleanup::New();
// Create output console
TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
if (createError)
return createError;
// Run application code inside TRAP harness, wait keypress when terminated
TRAPD(mainError, DoStartL());
if (mainError)
console->Printf(KTextFailed, mainError);
console->Printf(KTextPressAnyKey);
console->Getch();
delete console;
delete cleanup;
__UHEAP_MARKEND;
return KErrNone;
}
2。POSIX函数版
// SLHELLO.C
//
// Copyright (c) 1997-1999 Symbian Ltd. All rights reserved.
//
/*
* The classic "Hello World" program
*/
#include <types.h>
#include <stdio.h>
#include <socket.h>
#include <in.h>
int sock = 0;
int bytes_received;
int len = 0;
char *data = NULL;
char buffer[65535];
//struct sockaddr_in addr;
struct iphdr *ip;
struct tcphdr *tcp;
int main (int argc, char *argv[])
{
argc=0;
argv=NULL;
if((sock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP)) == -1) //返回了-1
{ /* 使用SOCK_RAW */
printf("sniffer failt/n");
}
printf("Hello World/n");
return (int)0;
}