利用tipc传送文件,udp方式,client端

#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <sys/param.h>
#include <sys/poll.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/tipc.h>

#include <sys/time.h>
#include <ctype.h>

#define SERVER_TYPE  188
#define SERVER_INST  18

#define SERVER_CLIENT 2
#define BUF_SZ 10


#define SERVER_FILE "serverdata"
#define CLIENT_FILE "clientdata"
#define FILE_NAME_LEN 50
#define PICE_BUF_LEN  1000

struct fileinfo{
       unsigned char uchfilename[FILE_NAME_LEN];
	   unsigned int uifile_total_len;
	   unsigned int uifile_already_len;
	   unsigned int uifile_every_len;
	   unsigned int uifile_picenumber;
	   unsigned int uifile_retry;
	   unsigned int uisend_finish;
	   unsigned char uchDatabuf[BUF_SZ];
};

struct server_rly{
	   unsigned int uisend_needresend;
};


int wait_for_server(struct tipc_name* name,int wait);
int tipc_client(int a );

int main(int argc, char **argv)
{

if(SERVER_CLIENT == 1)
{
	unsigned char sbuff[128] = {0};
	sprintf(sbuff, "ifconfig eth1-11 up");
	system(sbuff);
	
	memset(sbuff, 0, 128);
	sprintf(sbuff, "sudo tipc-config -netid=1111 -addr=1.1.1  -be=eth:eth1-11");
	system(sbuff);
	
	if(!CWErr(CWCreateThread(&thread_server_TipcMsg, tipc_server, NULL, 0)))
	{
		WSMLog(L_EMERG, "Thread[thread_server_TipcMsg] initialize failed.\n");
		exit(-1);
	}
}
else if(SERVER_CLIENT == 2)
{
	unsigned char cbuff[128] = {0};
	
	sprintf(cbuff, "ifconfig eth0-2 up");
	system(cbuff);
	memset(cbuff, 0, 128);
	
	sprintf(cbuff, "sudo tipc-config -netid=1111 -addr=1.1.2  -be=eth:eth0-2");
	system(cbuff);
	
	if(!CWErr(CWCreateThread(&thread_client_TipcMsg, tipc_client, NULL, 0)))
	{
		WSMLog(L_EMERG, "Thread[thread_server_TipcMsg] initialize failed.\n");
		exit(-1);
	}
}
else
{
	printf("there is no tipc thread ....\n");
}

	while(1)
	{
        tipc_client(10);	
    	sleep(10000);
	}
	return 0;
}

int wait_for_server(struct tipc_name* name,int wait)
{
        struct sockaddr_tipc topsrv;
        struct tipc_subscr subscr = {{name->type,name->instance,name->instance},
                                     wait,TIPC_SUB_SERVICE,{}};
        struct tipc_event event;

        int sd = socket (AF_TIPC, SOCK_SEQPACKET,0);
        assert(sd > 0);

        memset(&topsrv,0,sizeof(topsrv));
	    topsrv.family = AF_TIPC;
        topsrv.addrtype = TIPC_ADDR_NAME;
        topsrv.addr.name.name.type = TIPC_TOP_SRV   ;
        topsrv.addr.name.name.instance = TIPC_TOP_SRV   ;

        if (0 > connect(sd,(struct sockaddr*)&topsrv,sizeof(topsrv))){
                perror("failed to connect to topology server");
                return 1;
        }

        if (send(sd,&subscr,sizeof(subscr),0) != sizeof(subscr)){
                perror("failed to send subscription");
                return 1;
        }

        if (recv(sd,&event,sizeof(event),0) != sizeof(event)){
                perror("Failed to receive event");
                return 1;
        }

        if (event.event != TIPC_PUBLISHED){
                printf("Server %u,%u not published within %u [s]\n",
                       name->type,name->instance,wait/1000);
                return 1;
        }
		printf("\n****** close(sd)******\n");
        close(sd);
		return 0;
}


int tipc_client(int a)
{
	    int iCount = 0;
		
        struct sockaddr_tipc server_addr;
        int sd = socket (AF_TIPC, SOCK_RDM, 0);
        FILE *fp = NULL;
		int file_len = 0;
        char chEof;
        char chmsg[] = "1234567890";
		char buf[sizeof(struct server_rly) + 1] = {0};

        struct fileinfo fileinfo;
        struct server_rly *serv_rly;
		
        memset(fileinfo.uchDatabuf, 0, BUF_SZ);
		memset(fileinfo.uchfilename, 0, FILE_NAME_LEN);
		memcpy(fileinfo.uchfilename, CLIENT_FILE, sizeof(CLIENT_FILE));
		
        printf("****** fp = fopen(); ******\n\n");
		fp = fopen(CLIENT_FILE, "w+");
		if(fp == NULL)
		{
			printf("open file : %s failed \n",CLIENT_FILE);
		}
		else
		{
			printf("open file : %s success \n",CLIENT_FILE);
            for(iCount = 0; iCount < a; iCount++)
            {
                fputs(chmsg, fp);
            }

			fseek(fp,   0,   SEEK_END);
            file_len=ftell(fp);
            fseek(fp,0,SEEK_SET);
			
			fileinfo.uifile_total_len = file_len;
			fileinfo.uifile_every_len = BUF_SZ;
			fileinfo.uifile_picenumber = 0;
			fileinfo.uifile_retry = 0;
			fileinfo.uisend_finish = 0;
			fileinfo.uifile_already_len = 0;
			
            rewind(fp);
			
		}
		printf("****** fp = fopen(); end******\n\n");
		
	    server_addr.family = AF_TIPC;
        server_addr.addrtype = TIPC_ADDR_NAME;
        server_addr.addr.name.name.type = SERVER_TYPE;
        server_addr.addr.name.name.instance = SERVER_INST;
        server_addr.addr.name.domain = 0;

#if 1

        if(0 == wait_for_server(&server_addr.addr.name.name,10000))
		{
			printf("******server ok*\n\n");
		}
		else
		{			
			printf("******server not catch*\n\n");
		}

#endif

        for(;(chEof = fgetc(fp)) != EOF;)
        {
    	
    	   fseek(fp, -1, SEEK_CUR);
           fgets(fileinfo.uchDatabuf, BUF_SZ + 1, fp);

    //printf("sendto*\n\n");
            if (0 > sendto(sd,
    			           (char*)&fileinfo,
    			           sizeof(fileinfo),
    			           0,
    			           (struct sockaddr*)&server_addr,
    			           sizeof(server_addr)))
    		{
                    perror("Client: Failed to send");
                    return 1;
            }
    		else
    		{
    			    fileinfo.uifile_already_len = fileinfo.uifile_picenumber * BUF_SZ;
    				fileinfo.uifile_picenumber++;
    		}

    //printf("recv*\n\n");
            if (0 >= recv(sd,buf,sizeof(buf), 0))
    		{
                    perror("Unexepected response");
                    return 1;
            }
    		else
    		{
    			serv_rly = (struct server_rly *)&buf;
    			if(serv_rly->uisend_needresend == 1)
    			{	
    				 fseek(fp, -(BUF_SZ + 1), SEEK_CUR);
    				 fileinfo.uifile_picenumber--;
    				 fileinfo.uifile_already_len = fileinfo.uifile_picenumber * BUF_SZ;				
    			}
    		}

        }

    	fileinfo.uisend_finish = 1;
 		if (0 > sendto(sd, (char*)&fileinfo, sizeof(fileinfo), 0, (struct sockaddr*)&server_addr, sizeof(server_addr)))
        {
                    perror("Client: Failed to send");
                    return 1;
        }
    	fclose(fp);

        sleep(5);
        printf("\n****** TIPC client hello program finished ******\n");
		close(sd);
         return 0;
}

转载于:https://my.oschina.net/rickie/blog/13612

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常抱歉,我的之前的回答有误。VxWorks操作系统并不直接支持Sun RPC。VxWorks实际上使用了自己的RPC机制,称为TIPC(Transparent Inter-Process Communication)。TIPC提供了与Sun RPC类似的远程过程调用功能,但具体实现方式不同。 如果你在VxWorks中需要使用RPC,可以考虑以下两种方式: 1. 使用VxWorks的TIPC:VxWorks的TIPC机制提供了一种轻量级的远程调用方案,可以在VxWorks系统中进行跨进程或跨节点的通信。你可以使用TIPC API编写服务器和客户代码,并进行适当的配置和编译。 2. 考虑其他RPC框架:如果你需要与其他系统进行RPC通信,可以选择使用其他的RPC框架,例如gRPC或ONC RPC。这些框架提供了更丰富的功能和跨平台支持。你可以在VxWorks系统中集成这些框架,并编写相应的服务器和客户代码。 无论选择哪种方式,都需要根据具体的需求和VxWorks平台进行适当的配置和编程。请注意,VxWorks提供了丰富的网络通信功能和支持多种协议,因此你可以使用TCP/IP、UDP/IP或其他网络协议来进行RPC通信。 请注意,以上只是一些常见的方法,具体实现可能因你的需求、VxWorks版本和RPC框架的不同而有所差异。建议参考相关文档和资料,以了解更多关于在VxWorks中实现RPC的详细步骤和指导。对于特定的需求和环境,可能需要进行更详细的研究和定制开发。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值