E
E
Evgeny Nikolaev2020-06-05 18:42:30
Delphi
Evgeny Nikolaev, 2020-06-05 18:42:30

Is it possible to send datagrams to UDP directly through the listening socket?

The situation is the following WinSocket API, the implementation in general on Delphi 6, while I'm more interested in the implementation at the Win Socket API level, although if anyone knows, it will not be worse in relation to Delphi.
The situation is the following. A UDP server socket has been created, bound to an address, waiting for messages in a separate thread.
A UDP message arrives on the server socket specified above. At this moment, I get the IP and port of the sender, after which I try to send a message using the listening server socket to the IP and port of the sender of the just received message, in general, this is the line:

SendRes:= sendto(FSocket,messageToSend[1],Length(messageToSend),0,SendAddr,SizeOf(SendAddr));//Тут значит следующие параметры указаны:

1) FSocket - the same listening socket that received the original message
2) messageToSend string type variable contains the new message being sent (from the server to the client)
3) Length (messageToSend) - the length of the message being sent, that is, the messageToSend variable
4) here 0 and so it was, I didn’t come up with it, it was like that in the examples
5) SendAddr contains the address and port to whom to send
6) the length of the SendAddr variable
When you try to send just in this line, an error occurs, so the question is whether it is even possible to do this.

I reason like this: since games over UDP behind NAT work, it means that the server presumably sends messages to the client on the same port from which they came. Or is this not the case here? (I wrote about NAT only to show how I argue, but in fact I tested it on a local PC, that is, the error occurred when both the client and server were running on the same PC, and the situation with different client ports too)

More additional question to the topic: Why do I have one client port in the client program that sends a message to the server, and another one on the server? For example, on the client program, the client port is 13574 (always large), and on the client's server port, for example, 1589 (that is, approximately the same range)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Nikolaev, 2020-06-06
@nikolaevevge

Partially understood.
Regarding the question: is it possible to send a message back to the client through the listening server socket: yes it is possible, it comes, the client receives.
With regards to why the client port on the client program is different from the client port in the server program - I don’t understand why, but they differ for me. And everything comes.
Here is a screenshot of the situation:
udp-server-and-client-echo-message.jpg
And the situation was that I didn’t send a message to the client via sendto - I found an error in the code, it turned out that I used the procedure on the button directly from the class, but I had to call it on an object instance, so it turned out that the server socket in that procedure was not initialized.
Sources of the client and server programs in Delphi and other details at the link blog.ivru.net/?id=199
__________________________________________ I'm
editing the answer because I found the reason why the ports were different:
__________________________________________
As it turned out, in the cases of the client program, I did not use ntohs to swap the high and low bytes of the port in the address variable, but in the cases of the server program I used ntohs. So we got integer numbers in one case, the junior and senior bytes went in one order and in the other in the reverse order.
The situation is described in more detail here https://ru.stackoverflow.com/questions/1137180/win...

H
Hemul GM, 2020-06-05
@HemulGM

On the client, you have a local port, and outside you see the port of a router or other NAT device.
Server ip 1.1.1.1 port 111.
Router ip 2.2.2.2 port 222.
Client ip for client 127.0.0.1 port 333.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question