UDP 数据报中分配的端口号和 sockaddr_in 中的端口号

port number assigning in UDP datagram and port number in sockaddr_in

在linux中:

我正在编写代码来实现 UDP 套接字。

我想填写 udp header 字段,所以我必须分配源和目标端口号。

例如:

struct udphdr *udph    
//UDP header
udph->source = htons (6666);
udph->dest = htons (8622);

另外我必须准备结构sockaddr_in,其中包含一个端口号字段,例如:

//internet socket structure
struct sockaddr_in sin; 
sin.sin_family = AF_INET;
sin.sin_port = htons(80);

sockaddr_in 结构中的端口号值与 UDP header 中的端口号值之间是否存在任何关系?

分配这些值的正确方法是什么?

这是我的完整代码:

#include <stdio.h> 
#include <unistd.h> //sleep         
//socket headers
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h> //when socket creation failed
//#include <netinet/tcp.h>   //Provides declarations for tcp header
#include <netinet/udp.h>   //Provides declarations for udp header
#include <netinet/ip.h>    //Provides declarations for ip header
#include <stdlib.h> //for exit();
#include <string.h> //memset
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{
    //declarations
    int sokt_fd;
    //buffers
    char datagram[80];// 80 bytes datagram
    char *data;//buffer for payload
    //code here
    //Create a udp socket
    if((sokt_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) <0)
    {
        //socket creation failed, may be because of non-root privileges
        perror("Failed to create socket");
        exit(1);
    }
    //zero out the packet buffer
    memset (datagram, 0, 80);
    //UDP header
    struct udphdr *udph = (struct udphdr *) datagram;//assign startof udp
    //Data part
    data = datagram + sizeof(struct udphdr);//assigin location of data
    strcpy(data , "ABC");
    //UDP header
    udph->source = htons (6666);
    udph->dest = htons (8622);
    udph->len = htons(8 + strlen(data)); //UDP header size
    //disable checksum
    udph->check = 0; //leave checksum 0 now, filled later by pseudo header
    //internet socket structure
    //destination IP address, and port
    //http://www.beej.us/guide/bgnet/output/html/singlepage/bgnet.html#sendtorecv
    struct sockaddr_in sin; 
    sin.sin_family = AF_INET;
    sin.sin_port = htons(80);
    sin.sin_addr.s_addr = inet_addr ("10.0.0.2"); //destenation IP address,

    //sending the datagram:
    while(1)
    {
        if (sendto (sokt_fd,datagram,udph->len,0/*int flags*/,(struct sockaddr *) &sin, sizeof (sin))<0)
        {
            perror("sendto failed");
        }
        else
        {
            printf ("Packet Send. Length : %d \n" , udph->len);

        }
        sleep(5);
    }
    return 0;
}

如果你有一个 IPPROTO_UDP 套接字,你就不会乱用 udp headers。

这是在端口 6666 上向 127.0.0.1 发送 UDP 的片段。

struct in_addr in;
struct sockaddr_in sdest;

memset(&sdest, 0, sizeof(sdest));
sdest.sin_family = AF_INET;
sdest.sin_port = htons(6666);

inet_aton("127.0.0.1", &in);
sdest.sin_addr.s_addr = in.s_addr;
r = sendto(sock, data, size, 0, (struct sockaddr *) &sdest, (socklen_t) sizeof(sdest));

但是您也应该看看 bind。这允许您接收东西(总是有用的),但主要是您应该查看 W Richard Stevens 等人 ISBN 0-13-141155-1 中 Unix Network Programming 中的优秀示例。