简体   繁体   中英

port number assigning in UDP datagram and port number in sockaddr_in

In linux:

I'm writing a code to implement UDP socket.

I want to fill udp header fields, so I must assign source and destination port numbers.

for example:

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

Also I must prepare the struct sockaddr_in, which contains a port number field, for example:

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

Is there any relation between port number values in the sockaddr_in structure, and these in the UDP header?

What is the correct way to assign these values?

Here is my full code:

#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;
}

If you an IPPROTO_UDP socket, you don't mess about with the udp headers.

This is a snippet from something that sends UDP to 127.0.0.1 on port 6666.

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));

But you should also look at bind as well. This allows you to receive stuff (always useful), but mainly you should look at the excellent examples in Unix Network Programming by W Richard Stevens et al ISBN 0-13-141155-1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM