网络编程DAY 2机械臂(初版)

// 0xff 0x02 {x:00red, 01blue} {y:angle} 0xff
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include  // struct input_event
#include 
#include 

#define PORT    5566
#define IP      "192.168.9.72"
#define PATH    "/dev/input/event1"
#define SENSITIVITY 10
#define CONTINUANCE 0

#define ERR_MSG(msg) do{\
    fprintf(stderr, "line:%d\t", __LINE__);\
    perror(msg);\
    }while(0)

char modify_red(char y);
char modify_blue(char y, int flag);

char head = 0xff;
char next = 0x02;
char x_red = 0x00;
char x_blue = 0x01;
char y_red = 0x00;
char y_blue = 0x00;
char tail = 0xff;

int main(int argc, char const *argv[])
{
    // 1. socket
    int cfd = socket(AF_INET, SOCK_STREAM, 0);
    if(cfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    printf("socket success with %d.\n", cfd);

    // 2. connect
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);
    if(connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    {
        ERR_MSG("connect");
        return -1;
    }
    printf("connect success.\n");

    int fd_op = open(PATH, O_RDONLY);
    lseek(fd_op, 0, SEEK_END);
    struct input_event buf;// read w a s d >>> struct input_event
    char operation[5] = {head, next, x_red, y_red, tail};
    write(cfd, &operation, sizeof(operation));
    operation[2] = x_blue;
    write(cfd, &operation, sizeof(operation));
    while(1)
    {
        // 3. read[block]
        // printf("%d\n", buf.code);// test
        if(sizeof(buf) == read(fd_op, &buf, sizeof(buf)))
        {
            if(EV_KEY == buf.type)
            {
                // printf("%d %d\n", buf.code, buf.value);// test
                if(KEY_A == buf.code && CONTINUANCE >= buf.value)// a >> red -1
                {
                    y_red -= (char)SENSITIVITY;
                    y_red = (char)modify_red(y_red)&0xff;
                    operation[2] = x_red;
                    operation[3] = (char)y_red;
                    write(cfd, &operation, sizeof(operation));
                    printf("a >> red -%d >> %x\n", SENSITIVITY, (char)y_red&0xff);
                }
                else if(KEY_D == buf.code && CONTINUANCE >= buf.value)// d >> red +1
                {
                    y_red += (char)SENSITIVITY;
                    y_red = (char)modify_red(y_red)&0xff;
                    operation[2] = x_red;
                    operation[3] = (char)y_red;
                    write(cfd, &operation, sizeof(operation));
                    printf("d>> red +%d >> %x\n", SENSITIVITY, (char)y_red&0xff);
                }
                else if(KEY_W == buf.code && CONTINUANCE >= buf.value)// w >> blue -1
                {
                    y_blue -= (char)SENSITIVITY;
                    y_blue = (char)modify_blue(y_blue, 1)&0xff;
                    operation[2] = x_blue;
                    operation[3] = (char)y_blue;
                    write(cfd, &operation, sizeof(operation));
                    printf("w >> blue -%d >> %x\n", SENSITIVITY, (char)y_blue&0xff);
                }
                else if(KEY_S == buf.code && CONTINUANCE >= buf.value)// s >> blue +1
                {
                    y_blue += (char)SENSITIVITY;
                    y_blue = (char)modify_blue(y_blue, 0)&0xff;
                    operation[2] = x_blue;
                    operation[3] = (char)y_blue;
                    write(cfd, &operation, sizeof(operation));
                    printf("s >> blue +%d >> %x\n", SENSITIVITY, (char)y_blue&0xff);
                }
                else if(KEY_ESC == buf.code)
                {
                    printf("client exit...\n");
                    break;
                }
                else if(KEY_0 == buf.code)
                {
                    y_red = 0x00;
                    operation[2] = x_red;
                    operation[3] = y_red;
                    write(cfd, &operation, sizeof(operation));
                    y_blue = 0x00;
                    operation[2] = x_blue;
                    operation[3] = y_blue;
                    write(cfd, &operation, sizeof(operation));
                    printf("init...\n");
                }
            }
        }
    }

    // z. close
    close(cfd);
    getchar();
    return 0;
}

char modify_red(char y)// -90 ~ 90
{
    if((y&0xff) >= 0xa6 || (y&0xff) <= 0x5a)
    {
        return (char)(y&0xff);
    }
    else if((y&0xff) >= 0x5a && (y&0xff) <= 0x5a+SENSITIVITY)// 00[0]->5a[90]
    {
        return (char)0x5a;
    }
    else// a6[160]-ff[255]
    {
        return (char)0xa6;
    }
}
char modify_blue(char y, int flag)
{
    printf("modifing... %x\n", y&0xff);
    if((y&0xff) >= 0x00 && (y&0xff) <= 0xb4)
    {
        return (char)(y&0xff);
    }
    else if((y&0xff) <= 0x00 || ((y&0xff) >= 0x00-SENSITIVITY && flag))
    {
        return (char)0x00;
    }
    else
    {
        return (char)0xb4;
    }
}

待优化:

1. 类型转换和按位与【由待优化5.的bug引起】

2. 自定义函数封装复用【可略】

3. 连续输入同一个值(.code变化)【读写同步问题】

4. 跳到指定位置【可略】

5. 蓝色机械臂的突然增长至四字节【bug>>>影响待优化1.】

6. 其它...

你可能感兴趣的:(网络,服务器,c语言)