rcv2.c 760 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /************* UDP CLIENT CODE *******************/
  2. #include <stdio.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #define STR(num) #num
  8. int main(){
  9. int clientSocket, portNum, nBytes;
  10. char buffer[530];
  11. //unsigned short buffer[512];
  12. struct sockaddr_in serverAddr;
  13. socklen_t addr_size;
  14. /*Create UDP socket*/
  15. clientSocket = socket(AF_INET, SOCK_DGRAM, 6454);
  16. while(1){
  17. nBytes = recvfrom(clientSocket,buffer,530,0,NULL, NULL);
  18. printf(STR(buffer)": \n");
  19. unsigned int value = 0;
  20. int c = 0;
  21. for (int i = 0; i < 530; ++i) {
  22. c |= ((0xff & value) << 24);
  23. value = buffer[i];
  24. printf("%02X", (char)value);
  25. }
  26. printf("\b\b\n");
  27. }
  28. return 0;
  29. }