嵌入式 简单的Linux下tcp连接示例

#include <iostream>  

#include <arpa/inet.h> 

#include <sys/socket.h>//套接信息需要的头文件 

#include <netinet/in.h> 

#include <unistd.h> 

#include <fcntl.h> 

#include <string.h>//用信号回收进程 

#include <stdlib.h>//标准库头文件 

#include <sys/wait.h>//waitpid需要的头文件

  1. #if 0
  2. #defineSOCK_UNIX_FILE "/tmp/video1_sock"
  3. #else
  4. #defineSOCK_UNIX_FILE "/tmp/video_sock"
  5. #endif
  6. #defineMAX_TRANSMIT_DATA_LEN 10240
  7. #define S_TIME_OUT 2000 //2s
  8. inttcp_write(intsocketfd,char* buf,intlen,unsignedinttimeout_ms)
  9. {
  10. int ret;
  11. int total_lenth =0;
  12. int len_remain =len;
  13. char *write_position =buf;
  14. structtimevaltimeout;
  15. fd_set wset, eset;
  16. int status =1;
  17. timeout.tv_sec =timeout_ms/1000;
  18. timeout.tv_usec = timeout_ms00;
  19. printf("%s %d\n",__func__,__LINE__);
  20. while(1){
  21. FD_ZERO(&wset);
  22. FD_ZERO(&eset);
  23. FD_SET(socketfd, &eset);
  24. FD_SET(socketfd, &wset);
  25. ret = select(socketfd+1, NULL, &wset, &eset,&timeout);
  26. printf("%s %d\n",__func__,__LINE__);
  27. if( ( timeout.tv_sec == 0)&& ( timeout.tv_usec == 0) ){
  28. //timeout
  29. printf(" writetimeout \n");
  30. break;
  31. }
  32. printf("%s %d\n",__func__,__LINE__);
  33. if(FD_ISSET(socketfd,&wset)){
  34. printf("%s %d\n",__func__,__LINE__);
  35. ret = send(socketfd, write_position, len_remain,0);
  36. printf("%s %d\n",__func__,__LINE__);
  37. if(ret <0){
  38. printf(" write err \n");
  39. return-1;
  40. }
  41. if(ret ==0){
  42. printf(" write==0 ... \n");
  43. }
  44. printf("%s %d\n",__func__,__LINE__);
  45. write_position += ret;
  46. total_lenth += ret;
  47. len_remain -= ret;
  48. if(0 == len_remain){
  49. break;
  50. }
  51. }
  52. if(FD_ISSET(socketfd,&eset)){
  53. printf("writeerr \n");
  54. break;
  55. }
  56. }
  57. //printf("write %d \n",len -len_remain);
  58. returnlen -len_remain;
  59. }
  60. inttcp_read(intsocketfd,char* buf,intlen,unsignedinttimeout_ms)
  61. {
  62. int ret;
  63. int total_lenth =0;
  64. int len_remain =len;
  65. char *read_position =buf;
  66. structtimevaltimeout;
  67. fd_set rset, eset;
  68. timeout.tv_sec =timeout_ms/1000;
  69. timeout.tv_usec = timeout_ms00;
  70. //printf("read timeout set %d%d\n",timeout.tv_sec,timeout.tv_usec);
  71. while(1){
  72. FD_ZERO(&rset);
  73. FD_ZERO(&eset);
  74. FD_SET(socketfd, &eset);
  75. FD_SET(socketfd, &rset);
  76. ret = select(socketfd+1, &rset, NULL,&eset, &timeout);
  77. if( ( timeout.tv_sec == 0)&& ( timeout.tv_usec == 0) ){
  78. //timeout
  79. //printf(" read timeout \n");
  80. break;
  81. }
  82. if(FD_ISSET(socketfd,&rset)){
  83. ret = recv(socketfd, read_position, len_remain,0);
  84. if
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值