Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include<sys/socket.h>
  2. #include<netinet/in.h>
  3. #include<sys/types.h>
  4. #include<stdio.h>
  5. #include<string.h>
  6. #include<stdlib.h>
  7. #include<arpa/inet.h>
  8. #include<unistd.h>
  9. #include<inttypes.h>
  10. #include"server.h"
  11.  
  12. #define MYPORT 50000
  13. #define BUF_LENGHT 25
  14.  
  15. static uint8_t memory_buf[BUF_LENGHT];
  16.  
  17. int read_regs(uint16_t st, uint16_t n, uint8_t **val){
  18. int i;
  19. *val = (uint8_t *) malloc(n);
  20. for(i = st; i < (st + n); i++){
  21. val[i - st] = (uint8_t *) malloc(16);
  22. val[i - st] = &memory_buf[i];
  23. }
  24. return 0;
  25. }
  26.  
  27. int write_regs(uint16_t st, uint16_t n, uint8_t *val){
  28. int i;
  29. for(i = st; i < (st + n); i++)
  30. memory_buf[i] = val[i-st];
  31. return 0;
  32. }
  33.  
  34. int main(){
  35. struct sockaddr_in local, rem;
  36. int socketfd, sfd, i;
  37. socklen_t addlen = sizeof(local);
  38. uint8_t op, *val = malloc(1);
  39. uint16_t ti, st, n;
  40.  
  41. for(i = 0; i < BUF_LENGHT; i++)
  42. memory_buf[i] = i;
  43.  
  44. socketfd = socket(PF_INET,SOCK_STREAM,0);
  45.  
  46. local.sin_family = AF_INET;
  47. local.sin_port = htons(MYPORT);
  48. inet_aton("127.0.0.1",&local.sin_addr);
  49.  
  50. bind(socketfd,(struct sockaddr *)&local, addlen);
  51.  
  52. listen(socketfd,10);
  53. sfd = accept(socketfd,(struct sockaddr *) &rem, &addlen);
  54. while(1){
  55. ti = get_request(sfd, &op, &st, &n, &val);
  56. if(ti < 0)
  57. return -1;
  58. if(op == 0x10)
  59. write_regs(st,n,val);
  60. if(op == 0x03)
  61. read_regs(st,n,&val);
  62. send_response(sfd, ti, op, st, n, val);
  63. }
  64. close(socketfd);
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement