|
发表于 2005-8-9 03:00:17
|
显示全部楼层
udp,raw这些socket几乎都可以调用bind函数,只不过意义更tcp的不大一样.
下面是一个raw socket的例子:
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <netinet/in.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
static char buffer[1600];
int main(void)
{
int fd, n;
struct sockaddr_ll addr;
bzero(&addr, sizeof(addr));
addr.sll_family = AF_PACKET;
addr.sll_ifindex = 2;
if((fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0)
return EXIT_FAILURE;
if(bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
return EXIT_FAILURE;
while((n = recv(fd, buffer, 1600, 0)) >= 0)
printf("Recv from link %i bytes\n", n);
return EXIT_SUCCESS;
} |
|