• AVR Freaks

Helpful ReplyHot!NET_PRES_SocketOpen and NET_PRES_SKT_UNENCRYPTED_DATAGRAM_CLIENT (solved)

Author
Luca Pascarella
Starting Member
  • Total Posts : 62
  • Reward points : 0
  • Joined: 2007/05/28 00:53:17
  • Location: The Netherlands
  • Status: offline
2019/01/27 10:56:09 (permalink)
0

NET_PRES_SocketOpen and NET_PRES_SKT_UNENCRYPTED_DATAGRAM_CLIENT (solved)

I am using NET_PRES_SocketOpen() for a STREAM connection. Everything works perfectly.
Recently, I saw the NET_PRES_SKT_UNENCRYPTED_DATAGRAM_CLIENT option in NET_PRES_SKT_T struct of Harmony 2.06.
Is possible to use the same API to open a DATAGRAM connection when NET_PRES is enabled? Edit, Yes it is.
 
I tried the following simplified code, the connection seems to be estabilished, but toRead is always 0.
case APP_OPEN:
socket = NET_PRES_SocketOpen(0, NET_PRES_SKT_UNENCRYPTED_DATAGRAM_CLIENT, IP_ADDRESS_TYPE_IPV4, port, (NET_PRES_ADDRESS *) &addr, NULL);
NET_PRES_SocketWasReset(socket);
if (socket == INVALID_SOCKET) { // Abort operation if no TCP socket is available
    SYS_DEBUG_PRINT(SYS_ERROR_WARNING, "Could not start connection\r\n");
    data.error = APP_RECEIVER_ERROR_OPENING_SOCKET;
    APP_Receiver_SetNewState(APP_ERROR);
}else{
    APP_Receiver_SetNewState(APP_WAIT);
}
break;
 
case APP_WAIT:
if (NET_PRES_SocketIsConnected(socket))
    APP_Receiver_SetNewState(APP_READ);
break;
 
case APP_READ:
toRead = NET_PRES_SocketReadIsReady(socket);
break;

 
Edit.
Of course, it was my mistake. For a client socket the local port is randomly picked in accord to the TCP/IP specifications.
To use a specific port because you want to both transmit and receive datagrams on the given port you must bind it. The following code addressed my issue.
 
uint16_t local_port = 8080;
NET_PRES_SocketBind(socket, IP_ADDRESS_TYPE_IPV4, local_port, 0);

 
Luca
post edited by Luca Pascarella - 2019/01/30 02:53:22
#1
rainad
Moderator
  • Total Posts : 1139
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: offline
Re: NET_PRES_SocketOpen and NET_PRES_SKT_UNENCRYPTED_DATAGRAM_CLIENT 2019/01/28 07:24:30 (permalink)
0
Are you able to send data to the server socket?
And you see the server sending data back but you cannot read it from the client socket?
 
#2
Luca Pascarella
Starting Member
  • Total Posts : 62
  • Reward points : 0
  • Joined: 2007/05/28 00:53:17
  • Location: The Netherlands
  • Status: offline
Re: NET_PRES_SocketOpen and NET_PRES_SKT_UNENCRYPTED_DATAGRAM_CLIENT 2019/01/29 01:10:54 (permalink)
0
Dear rainad,
I did some more tests and checked my Harmony configuration, as attached.
I am able to transmit UDP packets to my server using both unicast or broadcast addresses. In both cases, my server application running on a desktop receives those UDP packets.

I successfully used the following simplified code in transmission:
case APP_SEND:
if (!isTimeoutedOrDisconnected()) {
    i = NET_PRES_SocketWriteIsReady(socket, length, 0); // This must be called to recall last UDP socket
    index += NET_PRES_SocketWrite(socket, &buffer[index], min(length - index, i));
    if (index >= length) {
        NET_PRES_SocketFlush(socket); // Prevent fragmentation send only full packages
        APP_Receiver_SetNewState(APP_READ);
    }
}
break;

To give more details, all variables are declared static to preserve their values between successive calls.

Unfortunately, the reception has still problems. The following code returns always zero:
read = NET_PRES_SocketReadIsReady(socket);

Currently, I do not have my network equipment to catch packets with Wireshark but I am quite sure that the server transmits correctly (I may wrong) because I am able to send and receive packets between computers in the same network.
 
In the writing case the NET_PRES_SocketWriteIsReady() must be called before NET_PRES_SocketWrite() to retrieve the current UDP socket. Must be something similar to for the NET_PRES_SocketRead() ?
 
Luca
post edited by Luca Pascarella - 2019/01/29 01:16:50

Attached Image(s)

#3
rainad
Moderator
  • Total Posts : 1139
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: offline
Re: NET_PRES_SocketOpen and NET_PRES_SKT_UNENCRYPTED_DATAGRAM_CLIENT 2019/01/29 10:34:23 (permalink)
0
Hi Luca,
 
Of course you can test calling directly NET_PRES_SocketRead() but I doubt that anything will change, the NET_PRES_SocketReadIsReady should work.
One simple experiment that I think would be useful is to call the NET_PRES_SocketGetTransportHandle() to get the UDP socket and then investigate directly the corresponding UDP socket with TCPIP_UDP_GetIsReady and call TCPIP_UDP_SocketInfoGet() that will give you more info about the socket status.
Actually, if you have a serial console, there is already a "udpinfo" command - needs to be enabled though - that you can type at run time and will give you the info for that socket.
 
Another option is to do a test by replacing the calls to NET_PRES_Socket with their corresponding TCPIP_UDP_Socket calls - they match one to one, so they are easy to change - and check if this behaves normally. This way you'll be able to tell if there is a problem at the socket level or in NET_PRES. 
 
 
 
 
#4
Luca Pascarella
Starting Member
  • Total Posts : 62
  • Reward points : 0
  • Joined: 2007/05/28 00:53:17
  • Location: The Netherlands
  • Status: offline
Re: NET_PRES_SocketOpen and NET_PRES_SKT_UNENCRYPTED_DATAGRAM_CLIENT 2019/01/29 14:10:41 (permalink)
0
After I added UDP commands, I discovered with 'udpinfo' that the socket was successfully opened. However, the local was automatically assigned.
>udpinfo
UDP sockets: 10
sktIx: 0, addType: 1, remotePort: 67, localPort: 68, rxQueueSize: 0, txSize: 512
sktIx: 1, addType: 1, remotePort: 30303, localPort: 30303, rxQueueSize: 0, txSize: 512
sktIx: 2, addType: 1, remotePort: 137, localPort: 137, rxQueueSize: 0, txSize: 0
sktIx: 3, addType: 1, remotePort: 123, localPort: 62509, rxQueueSize: 0, txSize: 512
sktIx: 4, addType: 1, remotePort: 53, localPort: 52431, rxQueueSize: 0, txSize: 512
sktIx: 5, addType: 1, remotePort: 8080, localPort: 56841, rxQueueSize: 0, txSize: 512
>

In the log, the latter is my socket and the localPort is 56841 instead of 8080. Indeed, I can receive packets when the port of my server corresponds.

I am opening the datagram connection with following parameters:
uint16_t port = 8080;
socket = NET_PRES_SocketOpen(0, NET_PRES_SKT_UNENCRYPTED_STREAM_CLIENT, IP_ADDRESS_TYPE_IPV4, port, (NET_PRES_ADDRESS *) &addr, NULL);

Where the given port is the remote port, as must be.
I would like to have the same value for the local port, like for the 30303 discover service. Do you have a suggestion for doing this?
#5
rainad
Moderator
  • Total Posts : 1139
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: offline
Re: NET_PRES_SocketOpen and NET_PRES_SKT_UNENCRYPTED_DATAGRAM_CLIENT 2019/01/29 14:22:24 (permalink) ☄ Helpfulby Luca Pascarella 2019/01/30 02:54:34
5 (1)
Yes, after opening the socket just bind it to the local port you want: 
uint16_t local_port = 8080;
NET_PRES_SocketBind(socket, IP_ADDRESS_TYPE_IPV4, local_port, 0);
use 0 for the local address, probably you don't care what interface.
 
The behavior is normal: when you open a client socket, you specify the remote port and the local one is chosen randomly.
 
#6
Luca Pascarella
Starting Member
  • Total Posts : 62
  • Reward points : 0
  • Joined: 2007/05/28 00:53:17
  • Location: The Netherlands
  • Status: offline
Re: NET_PRES_SocketOpen and NET_PRES_SKT_UNENCRYPTED_DATAGRAM_CLIENT 2019/01/30 02:47:33 (permalink)
0
I agree, the local port must be randomly picked in accord to the TCP/IP specifications.
 
After the bind my stupid issue have been addressed.
 
Luca
#7
Jump to:
© 2019 APG vNext Commercial Version 4.5