UDT Tutorial 《Hello World!》

 UDT Tutorial

Hello World!

In this section we will introduce the simplest UDT program that can transfer data in high performance.

This simple "Hello World!" example includes a server program and a client program just like any socket programming tutorial. These are the simpler version of the appserver and appclient examples in ./app directory.

To compile, use gcc -o server server.cpp -I -L -ludt -lstdc++ -lpthread. For more details, please refer to the Makefile in ./app directory.

UDT server example

#include <arpa/inet.h>
#include <udt.h>
#include <iostream.h>

using namespace std;

int main()
{
UDTSOCKET serv = UDT::socket(AF_INET, SOCK_STREAM, 0);

sockaddr_in my_addr;
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(9000);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero), '\0', 8);

if (UDT::ERROR == UDT::bind(serv, (sockaddr*)&my_addr, sizeof(my_addr)))
{
  cout << "bind: " << UDT::getlasterror().getErrorMessage();
  return 0;
}

UDT::listen(serv, 10);

int namelen;
sockaddr_in their_addr;

UDTSOCKET recver = UDT::accept(serv, (sockaddr*)&their_addr, &namelen);

char ip[16];
cout << "new connection: " << inet_ntoa(their_addr.sin_addr) << ":" << ntohs(their_addr.sin_port) << endl;

char data[100];

if (UDT::ERROR == UDT::recv(recver, data, 100, 0))
{
  cout << "recv:" << UDT::getlasterror().getErrorMessage() << endl;
  return 0;
}

cout << data << endl;

UDT::close(recver);
UDT::close(serv);

return 1;
}

This simple server tries to bind itself at port 9000. If succeed, it listens at port 9000 and accepts a client and then reads a string.

UDT client example

#include <iostream>
#include <udt.h>
#include <arpa/inet.h>

using namespace std;
using namespace UDT;

int main()
{
UDTSOCKET client = UDT::socket(AF_INET, SOCK_STREAM, 0);

sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(9000);
inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);

memset(&(serv_addr.sin_zero), '\0', 8);

// connect to the server, implict bind
if (UDT::ERROR == UDT::connect(client, (sockaddr*)&serv_addr, sizeof(serv_addr)))
{
  cout << "connect: " << UDT::getlasterror().getErrorMessage();
  return 0;
}

char* hello = "hello world!\n";
if (UDT::ERROR == UDT::send(client, hello, strlen(hello) + 1, 0))
{
  cout << "send: " << UDT::getlasterror().getErrorMessage();
  return 0;
}

UDT::close(client);

return 1;
}

The client side connects to the local address (127.0.0.1) at port 9000, and sends a "hello world!" message.

Note that in this "Hello World!" example the UDT::send and UDT::recv routines should use a loop to check return value. However, since the string length is very small and can be hold in one packet, we omit the loop part in order to give a simpler example.

 

更多具体细节参考UDT帮助文档;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值