tcp 嵌入式linux_嵌入式linux socket tcp - CSDN
  • 嵌入式Linux实现TCP/IP通信

    千次阅读 2018-06-07 22:44:19
    实现client向server发送数据。 server里面的rev要用nsockfd client里面的send用sockfd ...tcp_server代码: #include #include #include #include #include #include #include #include #include #include

    实现client向server发送数据。


    server里面的rev要用nsockfd

    client里面的send用sockfd


    tcp_server代码:

    #include <stdlib.h>
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/wait.h>
    #include <sys/socket.h>
    #include <unistd.h>  
    #include <arpa/inet.h>       
                             
    #define PORT 		8088       		// The port which is communicate with server
    #define BACKLOG 	10
    
    #define LENGTH 512              		// Buffer length                                                                                 
    int main ()
    {  
    	int iR;
    	int sockfd;                        // Socket file descriptor
        	int nsockfd;               		// New Socket file descriptor
        	int num;
        	int sin_size;                      	// to store struct size
        	char sdbuf[LENGTH];          	// Send buffer
    		char revbuf[LENGTH];       		// Receive buffer
        	struct sockaddr_in addr_local; 
        	struct sockaddr_in addr_remote; 
        //	char sendstr[16]= {"123456789 abcde"}; 
                   
        	/* Get the Socket file descriptor */  
        	if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )  
        	{   
            	printf ("ERROR: Failed to obtain Socket Despcritor.\n");
            	return (0);
        	} else {
            	printf ("OK: Obtain Socket Despcritor sucessfully.\n");
        	}
        
        	/* Fill the local socket address struct */
        	addr_local.sin_family = AF_INET;           	// Protocol Family
        	addr_local.sin_port = htons (PORT);         	// Port number
        	addr_local.sin_addr.s_addr  = htonl (INADDR_ANY);  // AutoFill local address
        	memset (addr_local.sin_zero,0,8);          		// Flush the rest of struct
    
        	/*  Blind a special Port */
        	if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 )
        	{  
        	  	printf ("ERROR: Failed to bind Port %d.\n",PORT);
            	return (0);
        	} else {
            	printf("OK: Bind the Port %d sucessfully.\n",PORT);
        	}
       
        	/*  Listen remote connect/calling */
        	if(listen(sockfd,BACKLOG) == -1)    
        	{  
            	printf ("ERROR: Failed to listen Port %d.\n", PORT);
            	return (0);
        	} else {
            	printf ("OK: Listening the Port %d sucessfully.\n", PORT);
        	}
       
        	while(1)
        	{  
            	sin_size = sizeof(struct sockaddr_in);  
            
            	/*  Wait a connection, and obtain a new socket file despriptor for single connection */
            	if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, (socklen_t *__restrict)&sin_size)) == -1)
            	{  
                	printf ("ERROR: Obtain new Socket Despcritor error.\n");
                	continue;
            	} else {
                	printf ("OK: Server has got connect from %s.\n", inet_ntoa(addr_remote.sin_addr)); 
            	}
            
    		
            	/* Child process */
            	if(!fork())                    
            	{  
                	
                	    while (strcmp(revbuf,"exit") != 0)
                	    { 
    					//接收数据
    					memset (revbuf,0,LENGTH);
    					
    					if((num = recv(nsockfd, revbuf, LENGTH, 0))>0)
    					{
    						printf ("OK: Receviced numbytes = %d\n", num);
    						revbuf[num] = '\0';
    						printf ("OK: Receviced string is: %s\n", revbuf);
    					}
    					
    					
                	    }
             	}  
    			
             	close(nsockfd);  
             	while(waitpid(-1, NULL, WNOHANG) > 0);   
    
         }    
    }
    



    tcp_client代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    
    #define PORT 		8088    		// The port which is communicate with server
    #define LENGTH 	256          		// Buffer length
    
    int main(int argc, char *argv[])
    {
    	    int iR;					         //
    		int nsockfd;               		// New Socket file descriptor
        	int sockfd;                        	// Socket file descriptor
        	int num;                    		// Counter of received bytes  
        	char revbuf[LENGTH];       		// Receive buffer
    	char sdbuf[LENGTH];                     // Send buffer
        	struct sockaddr_in remote_addr;    	// Host address information
    
        	/* Check parameters number */
        	if (argc != 2)                     
        	{    
            	printf ("Usage: client HOST IP (ex: ./client 192.168.7.239).\n");
            	return (0);
        	}
    
        	/* Get the Socket file descriptor */
        	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        	{
            	printf("ERROR: Failed to obtain Socket Descriptor!\n");
            	return (0);
        	}
        
        	/* Fill the socket address struct */
        	remote_addr.sin_family = AF_INET;              	// Protocol Family
        	remote_addr.sin_port = htons(PORT);           		// Port number
        	inet_pton(AF_INET, argv[1], &remote_addr.sin_addr); 	// Net Address
        	memset (remote_addr.sin_zero,0,8);                 	// Flush the rest of struct
    
        	/* Try to connect the remote */
        	if (nsockfd=connect(sockfd, (struct sockaddr *)&remote_addr,  sizeof(struct sockaddr)) == -1) 
        	{
            	printf ("ERROR: Failed to connect to the host!\n");
            	return (0);
        	} else {
            	printf ("OK: Have connected to the %s\n",argv[1]);
        	}
    
    		
            
    			/* Try to connect the server */
    			printf("You can enter string to send, and press 'exit' to end the connect.\n");
    			 
    			while(strcmp(sdbuf,"exit") != 0)
    			{      
    				
    				/* 发送数据------------ */
    				iR=(int)scanf("%s",sdbuf);
    				if(iR>512)break;
    				if((num=send(sockfd,sdbuf,strlen(sdbuf),0))==-1)
    				{
    					printf("ERROR:Fail to send string\n");
    					close(nsockfd);
    					exit(1);
    				}
    				printf("OK:Sent %d bytes sucessful,please enter again.\n",num);
    			}
    			
    		
        	
    		printf("Exit connect,Byebye!\n");
        	close (sockfd);
        	return (0);
    }
    

    开发板上执行tcp_client,主机上执行tcp_server

    在Linux系统上编译tcp_client指令:arm-none-linux-gnueabi-gcc tcp_client.c -o client

    编译tcp_server指令:gcc tcp_server.c -o tcp_server



    展开全文
  • 嵌入式Linux系统下的TCP文件传输程序
  • Linux TCP_IP协议栈-高清

    2020-07-30 23:31:59
    Linux TCP_IP协议栈-高清,分享给所有需要的人.........
  • Linux 网络编程——TCP编程,相关教程如下: http://blog.csdn.net/tennysonsky/article/details/45599027
  • LinuxTCP/IP协议实现及嵌入式应用, 一本低估的好书, 讲解LINUX tcp/ip协议, 推荐给大家, 清爽版本
  • 详细描述了Linux平台下的TCP协议的使用方法,本源码分为两部分,包含TCP协议的服务端的实现和客户端的实现
  • 2012-04-09

    2012-04-09 14:06:02
    NS2中make depend失败,错误发生在 `tcp/linux/tcp_naivereno.cc'的解决方法:编辑~/ns/Makefile.in,进行一下修改。 OBJ_CC = \ ... tcp/scoreboard.o tcp/scoreboard-rq.o tcp/tcp-sack1.o tcp/tcp-fack...
    NS2中make depend失败,错误发生在 `tcp/linux/tcp_naivereno.cc'的解决方法:编辑~/ns/Makefile.in,进行一下修改。
    OBJ_CC = \

    ...

    tcp/scoreboard.o tcp/scoreboard-rq.o tcp/tcp-sack1.o tcp/tcp-fack.o \
    - tcp/linux/tcp_naivereno.o\
    - tcp/linux/src/tcp_cong.o\
    - tcp/linux/src/tcp_highspeed.o tcp/linux/src/tcp_bic.o tcp/linux /src/tcp_htcp.o tcp/linux/src/tcp_scalable.o src/tcp_cubic.o\
    - tcp/linux/src/tcp_westwood.o tcp/linux/src/tcp_vegas.o tcp/linux/src/tcp_hybla.o\
    - tcp/linux/src/tcp_illinois.o tcp/linux/src/tcp_yeah.o \
    - tcp/linux/src/tcp_veno.o tcp/linux/src/tcp_compound.o tcp/linux/src/tcp_lp.o\
    - tcp/scoreboard1.o tcp/tcp-linux.o tcp/linux/ns-linux-util.o tcp/linux/ns-linux-c.o tcp/linux/ns-linux-param.o\
    + tcp/scoreboard1.o tcp/tcp-linux.o tcp/linux/ns-linux-util.o \
    tcp/tcp-asym.o tcp/tcp-asym-sink.o tcp/tcp-fs.o \

    ...
    -OBJ_C =
    +OBJ_C = \
    + tcp/linux/tcp_naivereno.o\
    + tcp/linux/src/tcp_cong.o\
    + tcp/linux/src/tcp_highspeed.o tcp/linux/src/tcp_bic.o tcp/linux/src/tcp_htcp.o tcp/linux/src/tcp_scalable.o tcp/linux/src/tcp_cubic.o\
    + tcp/linux/src/tcp_westwood.o tcp/linux/src/tcp_vegas.o tcp/linux/src/tcp_hybla.o\
    + tcp/linux/src/tcp_illinois.o tcp/linux/src/tcp_yeah.o \
    + tcp/linux/src/tcp_veno.o tcp/linux/src/tcp_compound.o tcp/linux/src/tcp_lp.o\
    + tcp/linux/ns-linux-c.o tcp/linux/ns-linux-param.o

    -为减去,+为增加。
    展开全文
  • linux TCP_NODELAY 未定义

    千次阅读 2017-05-05 11:22:58
    转载自http://blog.hehehehehe.cn/a/15575.htm编程,居然说”TCP_NODELAY” 未定义,添加#include
    展开全文
  • 优化LinuxTCP断开等待时间

    千次阅读 2019-01-09 10:57:25
    修改/proc/sys/net/ipv4/tcp_fin_timeout中的数值,默认为60秒 修改为20,并查询
  • TCP/IP 关于TCP同时打开-无需Listener的TCP连接建立过程 锁机制 Linux锁机制
  • linux下TCPTDP调试工具 sokit-master

    热门讨论 2020-07-30 17:49:56
    linux下网馈调试工具,方便现场调试。基于qt4的图形界面,亲测在centos7下可以正常使用。
  • linux查看tcp连接状态

    万次阅读 2019-09-06 12:45:10
    netstat -an |grep tcp 查看所有tcp连接包括LISTEN状态 netstat -an |grep ESTABLISHED 查看所有正在连接中的tcp连接 如果需要统计tcp连接每种状态的连接的数量 netstat -an | awk '/^tcp/ {++S[$NF]} END ...
  • 嵌入式Linux应用程序开发-(7)TCP-IP网络通信应用程序(TCP-Client) 嵌入式Linux应用程序开发-(8)TCP-IP网络通信应用程序(TCP-Server) 相比起TCP/IP协议的可靠,面向连接,基于字节流通信这些特性,UDP协议...
  • Linux TCP IP 协议栈分析

    2020-07-30 23:30:46
    Linux TCP IP 协议栈分析Linux TCP IP 协议栈分析Linux TCP IP 协议栈分析
  • linux 查看TCP端口

    2019-07-09 03:10:54
    如有转载,不胜荣幸。http://www.cnblogs.com/aaron-agu/ netstat –nat 转载于:https://www.cnblogs.com/aaron-agu/p/5269624.html
  • Linux TCP调试利器 - nc

    万次阅读 2015-06-29 15:30:00
    比如你想给某一个endpoint发送轻松 nc
  • 该源码是在linux下运行,用C语言编写,收和发是独立的代码
  • Linux内核源码剖析 TCP-IP 实现(上下册)
  • 一个简单的linuxtcp发包程序,因为网上都是window写的,在centos 5.5下做的实验
1 2 3 4 5 ... 20
收藏数 428,981
精华内容 171,592
关键字:

tcp 嵌入式linux