这是《windows网络编程技术》上的一个范例程序,它就是构建自己的udp包,具体的你自
己看吧
// Module Name: Iphdrinc.c
//
// Description:
// This is a simple app that demonstrates the usage of the
// IP_HDRINCL socket option. A raw socket is created of the
// UDP protocol where we will build our own IP and UDP header
// that we submit to sendto().
//
// Compile:
// cl -o Iphdrinc Iphdrinc.c ws2_32.lib
//
// Command Line Parameters/Arguments:
// iphdrinc [-fp:int] [-fi:str] [-tp:int] [-ti:str] [-n:int]
// [-m:str]
//
// -fp:int From (sender) port number
// -fi:IP From (sender) IP address
// -tp:int To (recipient) port number
// -ti:IP To (recipient) IP address
// -n:int Number of times to send message
// -m:str Message to send
//
#pragma pack(1)
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_MESSAGE 4068
#define MAX_PACKET 4096
//
// Setup some default values
//
#define DEFAULT_PORT 5150
#define DEFAULT_IP "10.0.0.1"
#define DEFAULT_COUNT 5
#define DEFAULT_MESSAGE "This is a test"
//
// Define the IP header. Make the version and length field one
// character since we can't declare two 4 bit fields without
// the compiler aligning them on at least a 1 byte boundary.
//
typedef struct ip_hdr
{
unsigned char ip_verlen; // IP version & length
unsigned char ip_tos; // IP type of service
unsigned short ip_totallength; // Total length
unsigned short ip_id; // Unique identifier
unsigned short ip_offset; // Fragment offset field
unsigned char ip_ttl; // Time to live
unsigned char ip_protocol; // Protocol(TCP,UDP etc)
unsigned short ip_checksum; // IP checksum
unsigned int ip_srcaddr; // Source address
unsigned int ip_destaddr; // Destination address
} IP_HDR, *PIP_HDR, FAR* LPIP_HDR;
//
// Define the UDP header
//
typedef struct udp_hdr
{
unsigned short src_portno; // Source port number
unsigned short dst_portno; // Destination port number
unsigned short udp_length; // UDP packet length
unsigned short udp_checksum; // UDP checksum (optional)
} UDP_HDR, *PUDP_HDR;
//
己看吧
// Module Name: Iphdrinc.c
//
// Description:
// This is a simple app that demonstrates the usage of the
// IP_HDRINCL socket option. A raw socket is created of the
// UDP protocol where we will build our own IP and UDP header
// that we submit to sendto().
//
// Compile:
// cl -o Iphdrinc Iphdrinc.c ws2_32.lib
//
// Command Line Parameters/Arguments:
// iphdrinc [-fp:int] [-fi:str] [-tp:int] [-ti:str] [-n:int]
// [-m:str]
//
// -fp:int From (sender) port number
// -fi:IP From (sender) IP address
// -tp:int To (recipient) port number
// -ti:IP To (recipient) IP address
// -n:int Number of times to send message
// -m:str Message to send
//
#pragma pack(1)
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_MESSAGE 4068
#define MAX_PACKET 4096
//
// Setup some default values
//
#define DEFAULT_PORT 5150
#define DEFAULT_IP "10.0.0.1"
#define DEFAULT_COUNT 5
#define DEFAULT_MESSAGE "This is a test"
//
// Define the IP header. Make the version and length field one
// character since we can't declare two 4 bit fields without
// the compiler aligning them on at least a 1 byte boundary.
//
typedef struct ip_hdr
{
unsigned char ip_verlen; // IP version & length
unsigned char ip_tos; // IP type of service
unsigned short ip_totallength; // Total length
unsigned short ip_id; // Unique identifier
unsigned short ip_offset; // Fragment offset field
unsigned char ip_ttl; // Time to live
unsigned char ip_protocol; // Protocol(TCP,UDP etc)
unsigned short ip_checksum; // IP checksum
unsigned int ip_srcaddr; // Source address
unsigned int ip_destaddr; // Destination address
} IP_HDR, *PIP_HDR, FAR* LPIP_HDR;
//
// Define the UDP header
//
typedef struct udp_hdr
{
unsigned short src_portno; // Source port number
unsigned short dst_portno; // Destination port number
unsigned short udp_length; // UDP packet length
unsigned short udp_checksum; // UDP checksum (optional)
} UDP_HDR, *PUDP_HDR;
//