Linux下RPC的hello world

来源:互联网 发布:java split函数用法 编辑:程序博客网 时间:2024/05/18 01:31

Linux 下面使用RPC需要使用到命令rpcgen.
在Linux下开发RPC程序流程如下:
1.写一个rpc程序,

       如test.x
2.使用rpcgen生成必须的文件,通常是客户端和服务器端以及头文件

       $rpcgen test.x
3.使用rpcgen生成服务器端和客户端的C语言代码

       $rpcgen -Ss -o test_server.c  test.x

       $rpcgen -Sc -o test_client.c  test.x
4.编辑源文件,加入你想要的服务等
5.使用gcc编译生成可执行文件

       $gcc -Wall -o test_server test_server.c test_clnt.c test_srv.c

       $gcc -Wall -o test_client  test_clnt.c test_client.c

6.使用rpcgen生成Makefile

       $rpcgen -Sm test.x>Makefile
7.执行测试

       $./test_server

       $./test_client 127.0.0.1
问题:
1.服务器无法启动,错误如下:
Cannot register service: RPC: Unable to receive; errno = Connection refused
unable to register (TESTPROG, VERSION, udp).


解决方法:系统没有安装portmap或者没有启动portmap端口映射。
$ls /etc/init.d/    
如果没有portmap则安装之
    $sudo apt-get install portmap
如果有了,则启动
    $sudo /etc/init.d/portmap start
还可以使用chkconfig设置系统开机启动的服务项,如将portmap加入开机启动:
    $sudo chkconfig --level 2 -s portmap on

 

原创粉丝点击