You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 line
1.9KB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <sys/un.h>
  8. #include <netinet/in.h>
  9. #include "message.h"
  10. int unix_socket_fd;
  11. struct licks_message message;
  12. struct sockaddr_un localaddr;
  13. struct sockaddr_un remoteaddr;
  14. socklen_t remoteaddr_len;
  15. char *ikd_path="/tmp/ikd";
  16. int
  17. main(int argc, char **argv) {
  18. int l,i;
  19. // create socket
  20. unix_socket_fd=socket(PF_LOCAL,SOCK_DGRAM,0);
  21. if(unix_socket_fd==-1) {
  22. perror("socket");
  23. exit(0);
  24. }
  25. printf("socket %d\n",unix_socket_fd);
  26. // bind to socket, localaddr is the pid
  27. sprintf(localaddr.sun_path,"/tmp/client%d",getpid());
  28. localaddr.sun_family=AF_LOCAL;
  29. // localaddr.sun_len=strlen(localaddr.sun_path)+1;
  30. if(bind(unix_socket_fd,(struct sockaddr *)&localaddr,sizeof(struct sockaddr_un))) {
  31. perror("bind");
  32. exit(0);
  33. }
  34. printf("bound\n");
  35. message.type=MSG_CHAR_ARRAY;
  36. for(i=0;i<32;i++) {
  37. message.char_array[i]=i;
  38. }
  39. remoteaddr.sun_family=AF_LOCAL;
  40. // remoteaddr.sun_len=strlen(ikd_path);
  41. strcpy(remoteaddr.sun_path,ikd_path);
  42. l=sendto(unix_socket_fd,&message,sizeof(message),0,(struct sockaddr *)&remoteaddr,sizeof(struct sockaddr_un));
  43. printf("sendto: %d\n",l);
  44. if(l==-1) {
  45. perror("sendto");
  46. }
  47. remoteaddr_len=sizeof(struct sockaddr_un);
  48. l=recvfrom(unix_socket_fd,&message,sizeof(struct licks_message),0,(struct sockaddr *)&remoteaddr,&remoteaddr_len);
  49. printf("recvfrom: %d\n",l);
  50. // printf("sun_len: %d\n",remoteaddr.sun_len);
  51. printf("sun_addr: %s\n",remoteaddr.sun_path);
  52. printf("message type: %d\n",message.type);
  53. for(i=0;i<32;i++) {
  54. if(!(i%8)) { printf("\n"); }
  55. printf("%02x ",message.char_array[i]);
  56. }
  57. printf("\n");
  58. close(unix_socket_fd);
  59. unlink(localaddr.sun_path);
  60. return 0;
  61. }