The code is below, but as I use sockets and the problems seems to be low
level, I don't think showing my code can help much. Sockets are not shared.
I even left just two types of communication: one UDP and one TCP. So there
are only two sockets and they are different types. And still there is a
problem.
UDP code:
void getNTPTime() {
int sock = 0;
struct sockaddr_in remote_addr;
struct sockaddr_in local_addr;
ip_addr_t ipaddr;
struct hostent * host;
struct ntpPacket ntpPkt;
memset(&ntpPkt, 0, sizeof(ntpPkt));
ntpPkt.flags = 0xE3;
host = gethostbyname(settings.ntpServer);
if (!host) return;
ipaddr.addr = ((struct in_addr *)host->h_addr_list[0])->s_addr;
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) return;
int timeoutMS = 3000;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeoutMS, sizeof(timeoutMS));
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
local_addr.sin_port = htons(0);
if (bind(sock, (struct sockaddr *)&local_addr, sizeof(struct
sockaddr_in))==0){
memset((char *)&remote_addr, 0, sizeof(remote_addr));
remote_addr.sin_family = AF_INET;
remote_addr.sin_addr.s_addr = ipaddr.addr;
remote_addr.sin_port = htons(123);
if (sendto(sock, &ntpPkt, sizeof(ntpPkt), 0, (struct sockaddr
*)&remote_addr, sizeof(remote_addr))!=-1){
socklen_t slen = sizeof(remote_addr);
if (recvfrom(sock, &ntpPkt, sizeof(ntpPkt), 0, (struct sockaddr
*)&remote_addr, &slen) > 0) {
uint32_t seconds = SwapEndianness(ntpPkt.trans_ts_sec);
seconds -= 2208988800;
setUNIXTime(seconds);
printf("time set\n");
} else {
printf("ntp timeout\n");
}
}
}
closesocket(sock);
}
TCP code:
uint8_t sendDBWriterData() {
struct sockaddr_in dbWriterAddr;
struct hostent * host;
host = gethostbyname(settings.dbwriter.server);
if (!host){
return 1;
}
memset(&dbWriterAddr, 0, sizeof(dbWriterAddr));
dbWriterAddr.sin_len = sizeof(dbWriterAddr);
dbWriterAddr.sin_addr.s_addr = ((struct in_addr
*)host->h_addr_list[0])->s_addr;
dbWriterAddr.sin_port = htons(settings.dbwriter.port);
dbWriterAddr.sin_family = AF_INET;
int sock;
// socket as a stream
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
return 2;
}
int timeoutMS = 3000;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeoutMS, sizeof(timeoutMS));
if(connect(sock, (struct sockaddr *)&dbWriterAddr, sizeof(dbWriterAddr)) <
0)
{
close(sock);
return 3;
}
The rest of the TCP code is not important because the connection is never
established. There is no service running on remote host or the host is down.
Pozdrawiam,
Grzegorz Niemirowski
http://www.grzegorz.net/
----- Wiadomość oryginalna -----
Od: Robert Deschambault
Do: Mailing list for lwIP users
Wysłano: 25 lipca 2015 01:18
Temat: Re: [lwip-users] Can't process incoming UDP packets after one of
hosts is powered down
It is hard to make a judgement without seeing your code. In my own
personal experience one no-no is to make sure that the UDP sockets are not
shared. I have had no problem running multiple different UDP services in
their own threads using different netconn connections.
------------------------------------------------------------------------------
_______________________________________________
lwip-users mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/lwip-users
_______________________________________________
lwip-users mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/lwip-users