当前位置:   article > 正文

嵌入式学习

嵌入式学习

作业

        TCP机械臂测试。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <pthread.h>
  9. #include <semaphore.h>
  10. #include <wait.h>
  11. #include <signal.h>
  12. #include <sys/socket.h>
  13. #include <arpa/inet.h>
  14. #include <sys/socket.h>
  15. #include <sys/ipc.h>
  16. #include <sys/sem.h>
  17. #include <semaphore.h>
  18. #include <sys/msg.h>
  19. #include <sys/shm.h>
  20. #include <sys/un.h>
  21. #define SER_PORT 8888
  22. #define SER_IP "192.168.125.172"
  23. #define CLI_PORT 6666
  24. #define CLI_IP "192.168.125.176"
  25. int main(int argc, const char *argv[])
  26. {
  27. //创建套接字标识符
  28. int cfd=socket(AF_INET,SOCK_STREAM,0);
  29. if(-1 == cfd)
  30. {
  31. perror("socket error");
  32. return -1;
  33. }
  34. printf("socket success\n");
  35. printf("cfd=%d\n",cfd);
  36. //连接服务器
  37. //准备对端结构体信息
  38. struct sockaddr_in sin;
  39. sin.sin_family=AF_INET;
  40. sin.sin_port=htons(SER_PORT);
  41. sin.sin_addr.s_addr=inet_addr(SER_IP);
  42. //连接服务器
  43. if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
  44. {
  45. perror("connect error");
  46. return -1;
  47. }
  48. printf("connect success\n");
  49. //数据收发
  50. char rbuf[5]={0xff,0x02,0x00,0x00,0xff};
  51. char bbuf[5]={0xff,0x02,0x01,0x00,0xff};
  52. send(cfd,rbuf,sizeof(rbuf),0);
  53. send(cfd,bbuf,sizeof(bbuf),0);
  54. while(1)
  55. {
  56. char c=0;
  57. int num=0;
  58. scanf(" %c %d",&c,&num);
  59. switch(c)
  60. {
  61. case 'w':
  62. case 'W':
  63. rbuf[3]+=num;
  64. send(cfd,rbuf,sizeof(rbuf),0);
  65. break;
  66. case 's':
  67. case 'S':
  68. rbuf[3]-=num;
  69. send(cfd,rbuf,sizeof(rbuf),0);
  70. break;
  71. case 'd':
  72. case 'D':
  73. bbuf[3]+=num;
  74. send(cfd,bbuf,sizeof(bbuf),0);
  75. break;
  76. case 'a':
  77. case 'A':
  78. bbuf[3]-=num;
  79. send(cfd,bbuf,sizeof(bbuf),0);
  80. break;
  81. default:
  82. printf("错误操作\n");
  83. break;
  84. }
  85. }
  86. close(cfd);
  87. return 0;
  88. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/598219
推荐阅读
相关标签
  

闽ICP备14008679号