Gateway source code -- tcpcli.c

/****************************************************************************/
/*                          LEC-3170 API                                     */
/*                          Copyright 2015-2020 Ls-China                    */
/****************************************************************************/
/*
**
** MODULE AUTHOR(S):    CHEN P
**
** MODULE TITLE:        tcpcli.c
**
** MODULE INPUTS:       None
**
** MODULE FUNCTION:  
**
** MODULE OUTPUTS:
**
** DOCUMENT REFERENCES:
**
******************************************************************************
**  MODIFICATION HISTORY:
**  
**  Date           Person    CR       Change     Email
**  --------       -------   ------   -------    -------------------------- 
**  3/04/2015      CHEN P      Create     cp_china@163.com
**
*****************************************************************************/
/*--------------------------------------------------------------
Date: 2012/1/1
Function: Serial port receive data  and then send to Ethenet 
--------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <arpa/inet.h>

#include "rs232.h"

#define PORT 3000
//#define PACKET_SIZE 8192 
#define PACKET_SIZE 128 

static int count=0;
static unsigned int second;
static unsigned int sum;
 
void display()
{
    printf("\n");
    printf("Transfer complete !\n");
    printf("Total size:%d !\n",sum);
    printf("Speed:%d Mbps\n",8*sum/second/0x100000);
    exit(0);
}
 
/*-----------------------------------------
client 192.168.2.111 -t 1 -p 1
argv[0] = client 
argv[1] = 192.168.0.2
argv[2] = -t
argv[3] = 1
argv[4] = -p
argv[5] = 1
------------------------------------------*/
 
main(int argc, char *argv[])
{
    int ret, portno, nWritten, nRead;
    //char buf[5] = "hell";
 
    /* Ethenet Port Parts */
    int argc_data;
    char *argv_data[6];
    int sockfd;
    int recv_bytes;
    unsigned int buf[PACKET_SIZE];
    unsigned int *tcp_buf;
    struct hostent *he;
    
    unsigned int uiip;
    unsigned int nsecond,packet_size;
    sum=0;
 
    argc_data = 6;
    argv_data[0] = "client";
    argv_data[1] = "192.168.0.2";
    argv_data[2] = "-t";
    argv_data[3] = "1";
    argv_data[4] = "-p";
    argv_data[5] = "128";
     
    portno=0;

    if(argc_data< 4 ){
        perror("Usage: client <hostIP>  -t time -p PACKET_SIZE\n");
        exit(2);
    }
    packet_size = PACKET_SIZE;
     
    second=atoi(argv_data[3]);  
    if(argc_data==6){
        packet_size = atoi(argv_data[5]);	
    } 
    printf("packet_size=%d\n",packet_size);
    
    tcp_buf = malloc(packet_size);

    he = gethostbyname(argv_data[1]);
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == -1){
	perror("Create Socket Error!\n");
	return -1;	
    }

    struct sockaddr_in srvaddr;
    bzero(&srvaddr,sizeof(srvaddr));
    srvaddr.sin_family = AF_INET;
    srvaddr.sin_port = htons(PORT);
    srvaddr.sin_addr = *((struct in_addr *)he->h_addr);
    //aiptoi(argv[1],&uiip);
    //srvaddr.sin_addr.s_addr=uiip;
 
    if(connect(sockfd, (struct sockaddr *) &srvaddr, sizeof(struct sockaddr))== -1){
        perror("Connect Error!\n");
        exit(1);
    }
    //  nsecond = htonl(second);
 
/*        
    nsecond = 0x01020304;
    if(write(sockfd,&nsecond,sizeof(nsecond))== -1){
        perror("Error when send second!\n");
        exit(1);
    }
    printf("Data Transfering!\n");
*/
 
/*  
    while (1){ 
        if((recv_bytes=read(sockfd,buf,packet_size))<=0){
            printf("Connection closed!\n");
            break;
        }   
        sum+=recv_bytes;
    }
*/

/*
    while(1)
    {
        write(sockfd, &nsecond, sizeof(nsecond));
        sleep(5);
    }
    close(sockfd);
    free(Tcp_buf);
    display();
*/
    //return 0;
    /*--------------------------------------------------------------------*/
        
    while(1)
    {
        ret=OpenCom(portno,"/dev/ttyS10",115200);
        if(ret==-1)
        {
            perror("The /dev/ttyS10 open error.");
            exit(1);
        }
    /*  while(1)
        {
            nWritten=ComWrt(portno,"abc",3);
        }
    */
        //printf("\n/dev/ttyS10 has send %d chars!\n",nWritten);
        //printf("\nRecieving data!***\n");
        fflush(stdout);
        //buf[5] = "hell";
        while(1)
        {
            /*nRead=ComRd(0,buf,256,3000);
            if(nRead>0)
            {
                printf("*****OK\n");
                nWritten = ComWrt(portno, buf, sizeof(buf));
                 
            }*/
            printf("******start***********\n");
            printf("*****ComRd*****\n");
            //ComRd(0, buf, 256, 3000);
            nRead = ComRd(0, buf, 256, 3000);
	    //nRead = 1;
	    usleep(5);
            if(nRead > 0)
            {
                printf("The Rddata is: %s\n",buf);
		//usleep(5);
                /* Serial Port Parts */
                //ComWrt(portno, buf, sizeof(buf));
                //printf("The ComWrt data is: %s\n",buf);
                 
                /* Ethenet Port Parts */
                //nsecond = 0x01020304;
                if(write(sockfd,buf,sizeof(buf))== -1)
                {
                 	perror("Error when send second!\n");
                        exit(1);
                }
                //printf("Data Transfering!\n");
                
                //write(sockfd, buf, sizeof(buf));
                printf("send to sockfd :%s\n",buf);
                sleep(1);
                 
                printf("*****************end*****************\n");
            }
        /*  else
                printf("Timeout\n");
        */
        }
        if((ret=CloseCom(portno)==-1))
        {
            perror("Close com");
            exit(1);
        }
        printf("\n\n");
    }
    printf("Exit now.\n");
     
     
    /* Ethenet Port Parts */
    close(sockfd);
    free(tcp_buf);
    display();
    /*----------------------------------*/
     
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值