The entire process of using sockets in Android NDK is divided into the following parts:
Part 1: Create socket and bind IP and port
#include <sys/>
#include <sys/>
#include <arpa/>
#define MAX_DATA_BUF_LEN 10240
int sd = INVALID_SOCKET;
sockaddr_in addr_org; // Send address
sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // Create socket
addr_org.sin_family = AF_INET;
addr_org.sin_addr.s_addr = inet_addr("192.128.0.1"); // Send IP
addr_org.sin_port = htons(8080); // Send port
bind(sd, (struct sockaddr *)&(addr_org), sizeof(struct sockaddr_in)); // Bind IP and port
Part 2: Send data
sockaddr_in addr_dst; // Receiver address
addr_dst.sin_family = AF_INET;
addr_dst.sin_addr.s_addr = inet_addr("192.128.0.2"); // Receiver IP
addr_dst.sin_port = htons(8080); // Receive port
... // Generate the data to be sent to SendDataBuff, with a length of ulLen
sendto(sd, SendDataBuff, ulLen, 0, (struct sockaddr *)&(addr_dst), sizeof(sockaddr_in)); // Send data in SendDataBuff
Part 3: Receiving data in thread (non-blocking)
void *SocketReceiveThread(void *pParam)
{
fd_set fdset;
struct timeval delayval;
unsigned long lRetVal;
delayval.tv_sec = 0;
delayval.tv_usec = 5000; /*5ms delay*/
while (!end_flag)
{
FD_ZERO(&fdset);
FD_SET(sd, &fdset);
lRetVal = select(sd+1, &fdset, NULL, NULL, &delayval); // Pay special attention to the first parameter
if (0 == lRetVal)
{
continue;
}
else if (SOCKET_ERROR == lRetVal)
{
break;
}
else if (FD_ISSET(sd, &fdset) != 0)
{
char RecvDataBuff[MAX_DATA_BUF_LEN]; // Receive data buffer
unsigned long ulLen = recvfrom(sd, RecvDataBuff, MAX_DATA_BUF_LEN, 0, NULL, NULL);
... // Process the received data
}
}
}
Part 4: Close the socket
close(sd);
Part 1: Create socket and bind IP and port
Copy the codeThe code is as follows:
#include <sys/>
#include <sys/>
#include <arpa/>
#define MAX_DATA_BUF_LEN 10240
int sd = INVALID_SOCKET;
sockaddr_in addr_org; // Send address
sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // Create socket
addr_org.sin_family = AF_INET;
addr_org.sin_addr.s_addr = inet_addr("192.128.0.1"); // Send IP
addr_org.sin_port = htons(8080); // Send port
bind(sd, (struct sockaddr *)&(addr_org), sizeof(struct sockaddr_in)); // Bind IP and port
Part 2: Send data
Copy the codeThe code is as follows:
sockaddr_in addr_dst; // Receiver address
addr_dst.sin_family = AF_INET;
addr_dst.sin_addr.s_addr = inet_addr("192.128.0.2"); // Receiver IP
addr_dst.sin_port = htons(8080); // Receive port
... // Generate the data to be sent to SendDataBuff, with a length of ulLen
sendto(sd, SendDataBuff, ulLen, 0, (struct sockaddr *)&(addr_dst), sizeof(sockaddr_in)); // Send data in SendDataBuff
Part 3: Receiving data in thread (non-blocking)
Copy the codeThe code is as follows:
void *SocketReceiveThread(void *pParam)
{
fd_set fdset;
struct timeval delayval;
unsigned long lRetVal;
delayval.tv_sec = 0;
delayval.tv_usec = 5000; /*5ms delay*/
while (!end_flag)
{
FD_ZERO(&fdset);
FD_SET(sd, &fdset);
lRetVal = select(sd+1, &fdset, NULL, NULL, &delayval); // Pay special attention to the first parameter
if (0 == lRetVal)
{
continue;
}
else if (SOCKET_ERROR == lRetVal)
{
break;
}
else if (FD_ISSET(sd, &fdset) != 0)
{
char RecvDataBuff[MAX_DATA_BUF_LEN]; // Receive data buffer
unsigned long ulLen = recvfrom(sd, RecvDataBuff, MAX_DATA_BUF_LEN, 0, NULL, NULL);
... // Process the received data
}
}
}
Part 4: Close the socket
close(sd);