K
K
Kirill Zhilyaev2017-12-05 15:46:09
PHP
Kirill Zhilyaev, 2017-12-05 15:46:09

How to force the sender port?

Can I force the first 2 bytes of a UDP packet? Not the datagram itself, but the UDP headers?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Dubrovin, 2017-12-05
@z3apa3a

You can by calling bind() on the socket and specifying the port in the sockaddr_in structure.

int sock;
unsigned short port = 666;
struct sockaddr_in sin;

sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
bind(sock,(struct sockaddr *)&sin, sizeof(sin));

PS didn't notice PHP in the tags, but the principle is the same.
php.net/manual/en/function.socket-bind.php
specify a non-zero port.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question