Home > other >  UDP caching problem under Windows
UDP caching problem under Windows

Time:09-27

Now want to realize the UDP communication between MCU and PC, MCU send message, PC using winsock receiving, both via Ethernet cable connection, the server is the code for the demo, c + +,
Communication is no problem, but now the network cable, the server can also continuously to, feeling is message is stored in a cache, then the server is cached data has been read,
MCU send message cycle around 500 microseconds, PC receives the efficiency of 1 ~ 5 ms, ms
Because accept the low efficiency lead to slow deposit is greater than the? So the cable pull can read the message?
So how to improve the efficiency of PC to receive?
Haven't contact before communication, please help to see what's the problem, thank you,

 
# define CAN_ETH_PDU_UDP_BUF_LENGTH 1500
Void receiver () {
/* UDP socket communication */
WSADATA WSADATA;
The SOCKET sockfd;
int ret;
int len;
SOCKADDR_IN addr, cli;
Char buf [CAN_ETH_PDU_UDP_BUF_LENGTH]={0};
Int ByteReceived=0, SelectTiming, ErrorCode;

//Initialize Winsock version 2.2
If (WSAStartup (MAKEWORD (2, 2), & amp; WsaData)!=0)
{
Printf (" Server: WSAStartup failed with the error % ld \ n ", WSAGetLastError ());
}
The else
Printf (" Server: The Winsock DLL status is % s. \ n ", wsaData. SzSystemStatus);

Sockfd=socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
If (sockfd==INVALID_SOCKET)
{
Printf (" Server: the Error at the socket () : % ld \ n ", WSAGetLastError ());
//Close the socket
Closesocket (sockfd);
//Clean up
WSACleanup ();
}
The else
Printf (" Server: socket () is OK! \n");
addr.sin_family=AF_INET;
Addr. Sin_port=htons (UDP_CAM_PORT);
Addr. Sin_addr. S_addr=INADDR_ANY;
//addr. Sin_addr. S_addr=inet_addr (UDP_SERVER_IP);

//will set of interfaces in non-blocking mode "s
U_long u1=1;
The ioctlsocket (sockfd, FIONBIO, (u_long *) & amp; U1);
Int size=1 * 1024 * 1024;
The setsockopt (sockfd, SOL_SOCKET, SO_RCVBUF, (const char *) & amp; The size, sizeof (int));

Ret=: : bind (sockfd, (SOCKADDR *) & amp; Addr, sizeof (addr));
If (ret==SOCKET_ERROR)
{
Printf (" bind error \ n ");
//Close the socket
Closesocket (sockfd);
//Do the clean up
WSACleanup ();
}
The else
Printf (" Server: bind () is OK! \n");

//Some info on the receiver side...
Printf (" Server: identifiers IP (s) 2: % s \ n ", inet_ntoa (addr. Sin_addr));
Printf (" Server: identifiers port 2: % d \ n ", htons (addr. Sin_port));
Printf (" Server: I \ 'm ready to receive a datagram... \n");
Len=sizeof (cli).
While (1) {

ByteReceived=recvfrom (sockfd, buf, sizeof (buf), 0, (SOCKADDR *) & amp; Cli, & amp; Len);
If (ByteReceived & gt; 0)
{
Printf (" Server: Total Bytes received: % d \ n ", ByteReceived);
Printf (" Server: The data is \ "% s \ " n ", buf);
//continue;
}
Else if (ByteReceived & lt;=0)
{
Printf (" Server: Connection closed with the error code: % ld \ n ", WSAGetLastError ());
//continue;
}
The else
{
Printf (" Server: recvfrom () failed with the error code: % d \ n ", WSAGetLastError ());
}

}
//the When your application is finished identifiers datagrams close the socket.
Printf (" Server: Finished identifiers. Closing the listening socket... \n");
If (closesocket (sockfd)!=0)
Printf (" Server: closesocket () failed! The Error code: % ld \ n ", WSAGetLastError ());
The else
Printf (" Server: closesocket () is OK \ n ");
//the When your application is finished call WSACleanup.
Printf (" Server: Cleaning up... \n");
If (WSACleanup ()!=0)
Printf (" Server: WSACleanup () failed! The Error code: % ld \ n ", WSAGetLastError ());
The else
Printf (" Server: WSACleanup () is OK \ n ");
}

  • Related