在Symbian模拟器和真机上都不能使用raw socket。

今天晚上实验了两种方法,一种是用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;
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值