#include #include #include #include #include #include #include #include int main (int argc, char *argv[]) { int i, j, k; signed short int receivefrom_port = 10068; char this_eth[] = "168.105.15.125"; struct sockaddr_in receivefrom_address; struct sockaddr_in this_address; socklen_t addrLen; char receivefrom_eth[INET_ADDRSTRLEN]; // Open/Bind Data Socket // Do this in a do/while loop because we need to call socket at // least once, and possibly more than once // If we open the socket without a problem, we don't need to delay // Otherwise we do, just to make sure we don't end up swamping the // system with socket calls bzero(&this_address, sizeof(this_address)); this_address.sin_family = AF_INET; this_address.sin_port = htons(receivefrom_port); inet_pton(AF_INET,this_eth,&this_address.sin_addr.s_addr); signed short int dataSocket; do { printf("*"); dataSocket = socket(AF_INET, SOCK_DGRAM, 0); usleep(10); } while(dataSocket == -1); signed short int errorCode; do { printf("#"); errorCode = bind(dataSocket, (struct sockaddr *)&this_address, sizeof(this_address)); usleep(10); } while (errorCode == -1); printf("\n"); addrLen = sizeof(this_address); int data_buffer_size = 2048; char data_buffer[data_buffer_size]; while(1) { signed short int data_length = recvfrom(dataSocket, data_buffer, data_buffer_size, 0, (struct sockaddr *)&receivefrom_address, (socklen_t *)&addrLen); //format the network address into dotted decimal inet_ntop(AF_INET,&receivefrom_address.sin_addr.s_addr,receivefrom_eth, sizeof(receivefrom_eth)); //make sure each byte has only 1 byte of data (some data showed strange FFFFFF at the end) unsigned short checked_data_buffer[data_buffer_size]; for(i = 0; i < data_length; i++) checked_data_buffer[i] = data_buffer[i] & 0xFF; unsigned short type = checked_data_buffer[1] | (checked_data_buffer[0] << 8); for(k = 0; k < data_length; k++) printf("%02X",checked_data_buffer[k]); printf("\n"); } return(0); }