Hi Daniel,
I am now shutting down socket after I successfully receive UDP frame (shutdown(sock, FLAG_ABORT_CONNECTION)). For transmitting I am opening new socket which I am closing after I send data. But even after this change only one message is sent. When I am trying to send another message I see in Wireshark ICMP message stated "port not found". If I use another target port than source port from request (incoming message) then is communication working fine, but after several hours occurs that receiving get stuck.
I need get communication working. If PC is sending request from its port (6050 - source port) to K60's port (6055 - target port). K60 responses. So response goes from K60's port (6055 - source port) to PC's port (6050 - target port). K60 has static IP address. PC can have any IP address according to net mask. K60 has to be able to receive data from any IP but always from same port and PC's IP can vary with every message. Is there any code sample which solves this issue?
// MAIN -------------------------------------------
InitializeNetworking(3, 2, 2, 0);
// ------------------------------------------------
// ------------------------------------------------
// Client Task
// ------------------------------------------------
void discoveryTask(uint_32 initial_data)
{
uint_32 sock;
uint_32 result;
uint_32 count;
uint_16 local_len = sizeof(local_sin);
_task_id taskID;
uint_16 i;
while(TRUE)
{
sock = socket(AF_INET, SOCK_DGRAM, 0); // create socket
if (sock == RTCS_SOCKET_ERROR)
{
printf("\nError, socket create failed");
return;
}
memset((char *) &local_sin, 0, sizeof(local_sin));
local_sin.sin_family = AF_INET;
local_sin.sin_port = SERVER_BROADCAST_PORT;
local_sin.sin_addr.s_addr = INADDR_ANY;
result = bind(sock, &local_sin, sizeof (sockaddr_in));
if (result != RTCS_OK)
{
printf("\nError, bind() failed with error code %lx", result);
}
sock = RTCS_selectall(0);
if (sock == 0)
{
printf("TimeOut expired. No data received. Trying again\n");
}
else
{
GPIOE_PCOR = PORTE5; // LED 2
count = recvfrom(sock, myBuffer, sizeof(myBuffer), 0, &local_sin, &local_len);
if (count == RTCS_ERROR)
{
printf("\nrecvfrom() failed with error %lx", RTCS_geterror(sock));
}
else
{
if (result == 0)
{
end = 0;
remoteIP = local_sin.sin_addr.s_addr;
if(flagPrvniPrijem) // first reception
taskID = _task_create(0, CLIENT_TASK, 0);
flagPrvniPrijem = 0;
for(i = 0; i < count; i++) // copy frame
{
buff_RX_uart_B_PC[i] = myBuffer[i];
}
pocet_prijatych_bajtu_B_PC = count;
Vyhodnoceni_uart_B(); // evaluation of the received frame
}
}
}
shutdown(sock, FLAG_ABORT_CONNECTION); // termination socket
}
}
void clientTask(uint_32 initial_data)
{
static sockaddr_in addr/*, server_addr*/;
static sockaddr_in rem_addr/*, server_addr*/;
static uint_32 sock, error/*, value, mask*/;
while(TRUE){
if(flagodeslatData) // the received data are correct. I send a response.
{
sock = socket(PF_INET, SOCK_DGRAM, 0); // create socket
if (sock == RTCS_SOCKET_ERROR)
{
remoteIP = 0;
return;
}
addr.sin_family = AF_INET;
addr.sin_port = 6049; // 6050
addr.sin_addr.s_addr = INADDR_ANY;
error = bind(sock, &addr, sizeof(addr));
if (error != RTCS_OK)
{
remoteIP = 0;
return;
}
// remoteIP = REMOTE_IP; // I use an address from the received frame.
rem_addr.sin_family = AF_INET;
rem_addr.sin_port = DESTPORT;
rem_addr.sin_addr.s_addr = remoteIP;
error = connect(sock, &rem_addr, sizeof(rem_addr));
if (error != RTCS_OK)
{
shutdown(sock, FLAG_ABORT_CONNECTION);
return;
}
flagodeslatData = 0;
GPIOE_PSOR = PORTE5; // LED 2
error = sendto(sock, TX_buff_uart_B_send, pocet_bajtu_TX, 0, &rem_addr, sizeof(rem_addr)); // send frame
if (error <= 0)
{
printf("There was an error trying to send to the server\n");
return;
}
remoteIP = 0;
shutdown(sock, FLAG_ABORT_CONNECTION); // termination socket
}
}
return;
}
Regards
Jaroslav