核心思想
取指->译码->执行
因为是串口输入,这个想法还是可以用的。
译码函数(与前两篇相同)
尽管最终再用性,可以屏蔽无用指令,虽然最后还是发了ascll码形式。
客户端程序(删的只剩框架)
#include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <arpa/inet.h> #include <netinet/in.h> #include <string.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> int decoding(char *code) {
return -1; } int main(int argc,char **argv) {
char code[4] = {
0}; int cmd = 0x01; int s_fd; struct sockaddr_in s_addr; memset(&s_addr,0,sizeof(struct sockaddr_in)); s_fd = socket(AF_INET,SOCK_STREAM,0); if(s_fd == -1){
perror("socket");
exit(-1);
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&s_addr.sin_addr);
socklen_t len = sizeof(struct sockaddr_in);
if(connect(s_fd,(struct sockaddr *)&s_addr,len) == -1){
perror("connect");
exit(-1);
}
while(1)
{
printf(">");
memset(code,'\0',4);
gets(code);
cmd = decoding(code);
if(cmd == -1){
printf("cmd not exist\n");
continue;
}
write(s_fd,code,4);
if(cmd == 0xff){
close(s_fd);
printf("client exit\n");
exit(1);
}
}
return 0;
}