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

原创 2013年12月02日 14:57:42

#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(ret <0){
  85. //error
  86. //printf("read err\n");
  87. break;
  88. }
  89. if(ret ==0){
  90. //printf(" read %d..\n",ret);
  91. // peerclose
  92. //printf(" peer close ?\n");
  93. break;
  94. }
  95. //printf("read %d...\n",ret);
  96. read_position += ret;
  97. total_lenth += ret;
  98. len_remain -= ret;
  99. if(len_remain <=0)break;
  100. }
  101. if(FD_ISSET(socketfd,&eset)){
  102. printf("readerr \n");
  103. break;
  104. }
  105. }
  106. //printf("time remain %d%d\n",timeout.tv_sec,timeout.tv_usec);
  107. returntotal_lenth;
  108. }
  109. #include
  110. #include
  111. #include
  112. #include
  113. #include
  114. #include
  115. int peer_sock_status =0;
  116. staticvoidsig_handle(intsigno)
  117. {
  118. switch(signo)
  119. {
  120. caseSIGTERM:
  121. printf("receiveSIGTERM!\n");
  122. exit(0);
  123. break;
  124. caseSIGKILL:
  125. printf("receiveSIGKILL!\n");
  126. exit(0);
  127. break;
  128. caseSIGINT:
  129. printf("\nreceive Ctrl+c!Then, the server will exit after 3seconds !\n");
  130. sleep(3);
  131. exit(0);
  132. break;
  133. caseSIGALRM:
  134. printf("receiveSIGALRM!\n");
  135. break;
  136. caseSIGILL:
  137. printf("receiveSIGILL!\n");
  138. break;
  139. caseSIGSEGV:
  140. printf("receiveSIGSEGV!\n");
  141. break;
  142. caseSIGPIPE:
  143. peer_sock_status = 0;
  144. printf("receiveSIGPIPE!\n");
  145. break;
  146. default:
  147. printf("receive unknown signal(%d)!\n",signo);
  148. break;
  149. }
  150. }
  151. int main(int argc,char*argv[])
  152. {
  153. int server_sockfd,client_sockfd;
  154. int server_len,client_len;
  155. staticint i_tmp =0;
  156. structsockaddr_unserver_address;
  157. structsockaddr_unclient_address;
  158. int i,bytes;
  159. char ch_send,ch_recv;
  160. chars_buf[MAX_TRANSMIT_DATA_LEN];
  161. charr_buf[8];
  162. unsigned intlen =0;
  163. unlink (SOCK_UNIX_FILE);//delete the file link for thefunction of bind
  164. server_sockfd = socket (AF_UNIX, SOCK_STREAM,0);
  165. server_address.sun_family = AF_UNIX;
  166. strcpy (server_address.sun_path,SOCK_UNIX_FILE);
  167. server_len = sizeof(server_address);
  168. bind (server_sockfd, (structsockaddr*)&server_address, server_len);
  169. listen (server_sockfd, 5);//the num of the client isfive
  170. printf ("Server is waiting for clientconnect...\n");
  171. client_len = sizeof(client_address);
  172. #if 1
  173. for(i=1; i<=SIGIO; i++)signal(i, sig_handle);
  174. #endif
  175. accept_again:
  176. if(i_tmp !=0)
  177. {
  178. printf ("\nServer is waiting for clientconnect...\n");
  179. }
  180. client_sockfd = accept (server_sockfd,(structsockaddr*)&server_address, (socklen_t*)&client_len);
  181. if (client_sockfd == -1){
  182. perror ("accept");
  183. exit (EXIT_FAILURE);
  184. }
  185. peer_sock_status =1;
  186. printf ("Theserver is waiting for clientdata...\n");
  187. strcpy(s_buf,"the tcp send stream tojss_server");
  188. while(1){
  189. memset(s_buf,0,MAX_TRANSMIT_DATA_LEN);
  190. memset(r_buf,0,8);
  191. sprintf(s_buf,"the %dth packet to jss_server!",i_tmp++);
  192. printf("writedata :");
  193. if ((bytes =tcp_write(client_sockfd, s_buf, MAX_TRANSMIT_DATA_LEN,S_TIME_OUT))<= 0) {
  194. perror ("WWrite");
  195. gotoaccept_again;
  196. }
  197. len += bytes;
  198. printf(" %d%d\n",bytes,len);
  199. usleep(2);
  200. printf(" %s %d The status of the client is %s\n",__FUNCTION__,__LINE__,r_buf);
  201. }
  202. close (client_sockfd);
  203. unlink ("server socket");
  204. gotoaccept_again;
  205. }
#include 
#include 
#include 
#include 
#include 
#include 


#if 0
#define SOCK_UNIX_FILE "/tmp/video1_sock"
#else
#define SOCK_UNIX_FILE "/tmp/video_sock"
#endif
#define MAX_TRANSMIT_DATA_LEN 10240
#define S_TIME_OUT 2000 //2s

int tcp_write(int socketfd, char* buf, int len,unsigned int timeout_ms)
{
        int ret;
        int total_lenth = 0;
        int len_remain = len;
        char *write_position = buf;
        struct timeval timeout;
        fd_set  wset, eset;
        int status = 1;

        timeout.tv_sec =timeout_ms/1000;
        timeout.tv_usec = timeout_ms00;
                printf("%s  %d \n",__func__,__LINE__);
        while(1){
                
                FD_ZERO(&wset);
                FD_ZERO(&eset);
                FD_SET(socketfd, &eset);
                FD_SET(socketfd, &wset);        
                ret = select(socketfd+1, NULL, &wset, &eset, &timeout);
                printf("%s  %d \n",__func__,__LINE__);
                if( ( timeout.tv_sec == 0)  && ( timeout.tv_usec == 0) ){
                        //timeout
                        printf(" write timeout \n");
                        break;
                }
                printf("%s  %d \n",__func__,__LINE__);
                if(FD_ISSET(socketfd, &wset)){
                printf("%s  %d \n",__func__,__LINE__);
                        ret = send(socketfd, write_position, len_remain, 0);
                        printf("%s  %d \n",__func__,__LINE__);
                        if(ret < 0){
                                printf(" write err \n");
                                return -1;
                        }

                        if(ret == 0){
                                printf(" write ==0 ... \n");

                        }
                printf("%s  %d \n",__func__,__LINE__);
                        write_position += ret;
                        total_lenth += ret;
                        len_remain -= ret;
                        if(0 == len_remain) {
                                break;
                        }
                        
                }

                if(FD_ISSET(socketfd, &eset)){
                        printf("write err \n");
                        break;

                }


        }

        //printf("write %d \n",len -len_remain);
        return len -len_remain ;        
}


int tcp_read(int socketfd, char* buf, int len,unsigned int timeout_ms)
{
        int ret;
        int total_lenth = 0;
        int len_remain = len;
        char *read_position = buf;
        struct timeval timeout;
        fd_set  rset, eset;

        timeout.tv_sec =timeout_ms/1000;
        timeout.tv_usec = timeout_ms00;
        //printf("read timeout set %d %d\n",timeout.tv_sec,timeout.tv_usec);

                
        while(1){
                
                FD_ZERO(&rset);
                FD_ZERO(&eset);
                FD_SET(socketfd, &eset);
                FD_SET(socketfd, &rset);        

                
                ret = select(socketfd+1, &rset, NULL, &eset, &timeout);
                
                if( ( timeout.tv_sec == 0)  && ( timeout.tv_usec == 0) ){
                        //timeout
                        //printf(" read timeout \n");
                        break;
                }

                if(FD_ISSET(socketfd, &rset)){
                        ret = recv(socketfd, read_position, len_remain, 0);
                        if(ret < 0){
                                //error
                                //printf("read err\n");
                                break;
                        }
                        if(ret == 0){
                                //printf(" read %d..\n",ret);
                                // peer close
                                //printf(" peer close ?\n");
                                break;
                        }
                                //printf(" read %d...\n",ret);
                        
                        read_position += ret;
                        total_lenth += ret;
                        len_remain -= ret;
                        if(len_remain <=0) break;
                        
                }

                if(FD_ISSET(socketfd, &eset)){
                        printf("read err \n");
                        break;

                }

        }
        //printf("time remain %d %d\n",timeout.tv_sec,timeout.tv_usec);
        return total_lenth;     
}


#include 
#include 
#include 
#include 
#include 
#include 

int peer_sock_status = 0;
static void sig_handle(int signo)
{
        switch(signo)
        {

        case SIGTERM:
                printf("receive SIGTERM!\n");
                exit(0);
                break;
        case SIGKILL:
                printf("receive SIGKILL!\n");
                exit(0);
                break;                  
        case SIGINT:
                printf("\nreceive Ctrl+c!Then, the server will exit after 3 seconds !\n");
                sleep(3);
                exit(0);
                break;
        case SIGALRM:
                printf("receive SIGALRM!\n");
                break;
        case SIGILL:
                printf("receive SIGILL!\n");
                break;
        case SIGSEGV:
                        printf("receive SIGSEGV!\n");
                break;
        case SIGPIPE:
                        peer_sock_status = 0;
                        printf("receive SIGPIPE!\n");
                        break;
        default:
                printf("receive unknown signal(%d)!\n", signo);
                break;
        }
}

int main (int argc, char *argv[])
{
        int server_sockfd, client_sockfd;
        int server_len, client_len;
        static int i_tmp = 0;
        struct sockaddr_un server_address;     
        struct sockaddr_un client_address;
        int i, bytes;
        char ch_send, ch_recv;
                                char s_buf[MAX_TRANSMIT_DATA_LEN];
                                char r_buf[8];
                                unsigned int len = 0;
        unlink (SOCK_UNIX_FILE);//delete the file link for the function of bind   

       
        server_sockfd = socket (AF_UNIX, SOCK_STREAM, 0);

       
        server_address.sun_family = AF_UNIX;

       
        strcpy (server_address.sun_path, SOCK_UNIX_FILE);

       
        server_len = sizeof (server_address);

       
        bind (server_sockfd, (struct sockaddr *)&server_address, server_len);

       
        listen (server_sockfd, 5);//the num of the client is five

        printf ("Server is waiting for client connect...\n");

        client_len = sizeof (client_address);
#if 1
        for(i=1; i<=SIGIO; i++) signal(i, sig_handle);
#endif
accept_again:
                                if(i_tmp != 0)
                                {
                printf ("\nServer is waiting for client connect...\n");
                                        }
        client_sockfd = accept (server_sockfd, (struct sockaddr *)&server_address, (socklen_t *)&client_len);
        
        if (client_sockfd == -1) {
                perror ("accept");
                exit (EXIT_FAILURE);
        }
                                peer_sock_status =1;
        printf ("The server is waiting for client data...\n");
        strcpy(s_buf,"the tcp send stream to jss_server");

        while(1){

                                                                memset(s_buf,0,MAX_TRANSMIT_DATA_LEN);
                                                                memset(r_buf,0,8);
                                                                sprintf(s_buf,"the %dth packet to jss_server !",i_tmp++);
                                                                printf("write data :");

                if ((bytes = tcp_write(client_sockfd, s_buf, MAX_TRANSMIT_DATA_LEN,S_TIME_OUT)) <= 0) {
                        perror ("WWrite");
                        goto accept_again;

                                }
                                

                                                                len += bytes;
                                                                printf(" %d %d\n",bytes,len);
                                                        
                                                                usleep(2);
                                                                printf(" %s %d The status of the client is %s \n",__FUNCTION__,__LINE__,r_buf);
        }
                close (client_sockfd);
                unlink ("server socket");
                                                                goto accept_again;
}


client.c:

  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #if1
  8. #define SOCK_UNIX_FILE "/tmp/video1_sock"
  9. #else
  10. #define SOCK_UNIX_FILE "/tmp/video_sock"
  11. #endif
  12. #define MAX_TRANSMIT_DATA_LEN 10240
  13. #defineC_TIME_OUT 2000 //2s
  14. inttcp_read(intsocketfd,char* buf,intlen,unsignedinttimeout_ms)
  15. {
  16. int ret;
  17. int total_lenth =0;
  18. int len_remain =len;
  19. char *read_position =buf;
  20. structtimevaltimeout;
  21. fd_set rset, eset;
  22. timeout.tv_sec =timeout_ms/1000;
  23. timeout.tv_usec = timeout_ms00;
  24. //printf("read timeout set %d%d\n",timeout.tv_sec,timeout.tv_usec);
  25. do{
  26. FD_ZERO(&rset);
  27. FD_ZERO(&eset);
  28. FD_SET(socketfd, &eset);
  29. FD_SET(socketfd, &rset);
  30. ret = select(socketfd+1, &rset, NULL, &eset,&timeout);
  31. if( ( timeout.tv_sec == 0)&& ( timeout.tv_usec == 0) ){
  32. //timeout
  33. //printf("read timeout \n");
  34. break;
  35. }
  36. if(FD_ISSET(socketfd,&rset)){
  37. ret = recv(socketfd, read_position,len_remain, 0);
  38. if(ret <0){
  39. //error
  40. //printf("read err\n");
  41. break;
  42. }
  43. if(ret ==0){
  44. //printf("read %d..\n",ret);
  45. // peer close
  46. //printf("peer close ?\n");
  47. break;
  48. }
  49. //printf(" read %d...\n",ret);
  50. read_position += ret;
  51. total_lenth += ret;
  52. len_remain -= ret;
  53. if(len_remain <=0)break;
  54. }
  55. if(FD_ISSET(socketfd,&eset)){
  56. printf("read err \n");
  57. break;
  58. }
  59. }while(0);
  60. //printf("time remain %d%d\n",timeout.tv_sec,timeout.tv_usec);
  61. returntotal_lenth;
  62. }
  63. inttcp_write(intsocketfd,char* buf,intlen,unsignedinttimeout_ms)
  64. {
  65. int ret;
  66. int total_lenth =0;
  67. int len_remain =len;
  68. char *write_position =buf;
  69. structtimevaltimeout;
  70. fd_set wset, eset;
  71. timeout.tv_sec =timeout_ms/1000;
  72. timeout.tv_usec = timeout_ms00;
  73. while(1){
  74. FD_ZERO(&wset);
  75. FD_ZERO(&eset);
  76. FD_SET(socketfd, &eset);
  77. FD_SET(socketfd, &wset);
  78. ret = select(socketfd+1, NULL, &wset,&eset, &timeout);
  79. if( ( timeout.tv_sec == 0)&& ( timeout.tv_usec == 0) ){
  80. //timeout
  81. printf(" write timeout\n");
  82. break;
  83. }
  84. if(FD_ISSET(socketfd,&wset)){
  85. ret = send(socketfd, write_position, len_remain,0);
  86. if(ret <0){
  87. printf(" write err \n");
  88. }
  89. if(ret ==0){
  90. printf(" write==0 ... \n");
  91. }
  92. write_position += ret;
  93. total_lenth += ret;
  94. len_remain -= ret;
  95. if(0 == len_remain){
  96. break;
  97. }
  98. }
  99. if(FD_ISSET(socketfd,&eset)){
  100. printf(" err\n");
  101. break;
  102. }
  103. }
  104. //printf("write %d \n",len -len_remain);
  105. returnlen -len_remain;
  106. }
  107. int main(int argc,char*argv[])
  108. {
  109. structsockaddr_unaddress;
  110. intsockfd;
  111. int len;
  112. int i,bytes;
  113. intresult;
  114. char ch_recv,ch_send;
  115. charr_buf[MAX_TRANSMIT_DATA_LEN];
  116. chars_buf[8];
  117. strcpy(s_buf,"LIVING");
  118. len = 0;
  119. if ((sockfd =socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  120. perror ("socket");
  121. exit (EXIT_FAILURE);
  122. }
  123. address.sun_family = AF_UNIX;
  124. strcpy (address.sun_path,SOCK_UNIX_FILE);
  125. len = sizeof(address);
  126. result = connect (sockfd, (structsockaddr*)&address, len);
  127. if (result == -1){
  128. printf ("ensure the server isup\n");
  129. perror ("connect");
  130. exit (EXIT_FAILURE);
  131. }
  132. printf("%s %dConnect succeed!\n",__FUNCTION__,__LINE__);
  133. while(1){
  134. //memset(r_buf,0,MAX_TRANSMIT_DATA_LEN);
  135. bytes = tcp_read (sockfd, r_buf,MAX_TRANSMIT_DATA_LEN,C_TIME_OUT);
  136. //bytes =recv(sockfd, r_buf, 10240, 0);
  137. if (bytes <= 0){
  138. perror ("read");
  139. exit (EXIT_FAILURE);
  140. }
  141. len +=bytes;
  142. printf("read data %d,%d\n",bytes,len);
  143. //sleep(5);
  144. }
  145. close (sockfd);
  146. return(0);
  147. }

相关文章推荐

嵌入式 Linux下curl库API简单介绍

1:CURLcode curl_global_init(long flags);函数,这个函数全局需要调用一次(多次调用也可以,不过没有必要), 所以这也是把Curlplus设计成单体类的原因,cur...
  • skdkjxy
  • skdkjxy
  • 2014年10月21日 17:01
  • 1013

嵌入式第一天(linux下minicom连接开发板)

本人使用linux,而天嵌科技的开发板的开发工具全都是基于windows的,本人又不想重回windows, 所以板子买来之后一直搁置了好几个月,基于各种原因,今天特意花了一点时间来研究linux下如...

Linux下的微型嵌入式GUI

  • 2007年09月22日 03:39
  • 279KB
  • 下载

嵌入式 linux下多线程同步问题

现在流行的进程线程同步互斥的控制机制,其实是由最原始最基本的4种方法实现的。由这4种方法组合优化就有了.Net和Java下灵活多变的,编程简便的线程进程控制手段。 1临界区:通过对多线程的串行化...
  • skdkjxy
  • skdkjxy
  • 2014年01月02日 14:44
  • 645

嵌入式C精华及Linux下的C编程基础

  • 2008年07月23日 08:42
  • 2.87MB
  • 下载

嵌入式samba功能的实现,linux下samba的移植

1、samba 下载地址: 新版本下载: https://download.samba.org/pub/samba/ 老版本下载: https://download.samba.org/pub...

Linux下的嵌入式编程

  • 2010年12月23日 17:18
  • 177KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:嵌入式 简单的Linux下tcp连接示例
举报原因:
原因补充:

(最多只允许输入30个字)