Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<sys/socket.h>
- #include<netinet/in.h>
- #include<sys/types.h>
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<arpa/inet.h>
- #include<unistd.h>
- #include<inttypes.h>
- #include"server.h"
- #define MYPORT 50000
- #define BUF_LENGHT 25
- static uint8_t memory_buf[BUF_LENGHT];
- int read_regs(uint16_t st, uint16_t n, uint8_t **val){
- int i;
- *val = (uint8_t *) malloc(n);
- for(i = st; i < (st + n); i++){
- val[i - st] = (uint8_t *) malloc(16);
- val[i - st] = &memory_buf[i];
- }
- return 0;
- }
- int write_regs(uint16_t st, uint16_t n, uint8_t *val){
- int i;
- for(i = st; i < (st + n); i++)
- memory_buf[i] = val[i-st];
- return 0;
- }
- int main(){
- struct sockaddr_in local, rem;
- int socketfd, sfd, i;
- socklen_t addlen = sizeof(local);
- uint8_t op, *val = malloc(1);
- uint16_t ti, st, n;
- for(i = 0; i < BUF_LENGHT; i++)
- memory_buf[i] = i;
- socketfd = socket(PF_INET,SOCK_STREAM,0);
- local.sin_family = AF_INET;
- local.sin_port = htons(MYPORT);
- inet_aton("127.0.0.1",&local.sin_addr);
- bind(socketfd,(struct sockaddr *)&local, addlen);
- listen(socketfd,10);
- sfd = accept(socketfd,(struct sockaddr *) &rem, &addlen);
- while(1){
- ti = get_request(sfd, &op, &st, &n, &val);
- if(ti < 0)
- return -1;
- if(op == 0x10)
- write_regs(st,n,val);
- if(op == 0x03)
- read_regs(st,n,&val);
- send_response(sfd, ti, op, st, n, val);
- }
- close(socketfd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement