www.pudn.com > tcpc1.zip > tcpc1.c


/******************************************************************************* 
File name:	tcpc1.c 
 
Description: 
	This program demo how to use socket lib coding TCP Client program on UC7000 series. 
	 
	The Hardware connection is following: 
	[UC7000 series]--- ~~LAN~~ ---PC Host Example with Windows 
	 
	If data received from Ethernet,send them all back to the LAN port. 
	If received 'q', the program will disconnect and close socket. 
	1. Create socket. 
	2. Connect to remote host with specified IP address and TCP port. 
	3. R/W data. 
	4. if receive 'q', disconnect. 
	 
Usage: 
    	1. Make downloading file for UC7000 series  
    	    1.1 Create executable file from source code(*.c)  
    	    	1.1.1 Modify tcpc1.c:  
    	    		change "DEST_IP" following the PC Host's IP address. 
    	    		change "TCPPORT" if needed.	 
    	    	1.1.2 Compiler and link to make tcpc1-release (no debug message) or tcpc1-debug (with debug message) 
	2. Download executable program to UC7000 series  
     	    2.1 Using ftp download tcpc1-release or tcpc1-debug to a UC7000 series unit. 
    	3. Execute TCP Server example for PC.[tcpserver.exe or frmServer.exe].  
    	   input the TCP Port number equal to "TCPPORT" 
	4. Run tcpc1-release or tcpc1-debug on UC7000 series. 
    	5. user can check if the sended data received back.  
    	6. Type 'q' to disconnect.  
    	   [for frmServer.exe, user must press button"Stop Server" to disconnect from PC host.] 
 
History:     
    	Version		Author		Date		Comment 
        1.0		Victor Yu.	01-14-2004      Wrote it. 
*******************************************************************************/ 
#include	 
#include	 
#include	 
#include	 
#include	 
#include	 
 
#define DEST_IP	"192.168.4.140" 
#define TCPPORT	4001 
 
#define TESTLEN	1024 
 
char buf[TESTLEN]; 
 
int main() 
{ 
	int			sfd, size, flag; 
	int			i, len; 
	struct	sockaddr_in	des; 
 
	printf("create socket......"); 
	if ( (sfd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 ) { 
    		printf("fail [%d]\n", errno); 
       		return 0; 
	} else 
		printf("OK\n"); 
 
	des.sin_addr.s_addr = inet_addr(DEST_IP); 
	des.sin_family = AF_INET; 
	des.sin_port = htons(TCPPORT);	 
	size = sizeof(des); 
	 
	printf("connect......"); 
	if ( connect(sfd, (struct sockaddr*)&des, size) < 0 ) { 
    		printf("fail [%d]\n", errno); 
       		return 0; 
	} else 
		printf("OK\n"); 
 
	flag = 1; 
    	printf("ready to recv & write data\n"); 
	do { 
		len = recv(sfd, buf, TESTLEN, 0); 
		if ( len > 0 ) { 
			if ( send(sfd,buf,len,0) < 0 ) { 
				printf("socket send data fail [%d]\n", errno); 
				break; 
			} 
			for ( i=0; i