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. Note that in UDT it does not matter which side starts first.
UDT server example
#include <iostream>
int main() { CUDT* server = new CUDT;
try //bind to any free port number int serverport = server->open(7000);
cout << "server is read at port: " << serverport << endl;
//waiting
for a client
char data[1000000]; server->send(data, 1000000);
while (server->getCurrSndBufSize() > 0) usleep(10);
server->close();
return 1; } |
This simple server tries to bind itself at port 7000. This may succeed or not, and the real port number is returned by the open method. See open reference for how to bind to a fixed port number. After connection (listen method is an unbound blocking call), the server sends out a 1000000 bytes block.
UDT client example
#include <iostream>
int main() { CUDT* client = new CUDT;
try //bind to any free port number client->open();
//connect
to a server, foo is the server IP, bar is the server port
number (may be 7000 or other)
}
char data[1000000]; client->recv(data, 1000000);
client->close();
return 1; } |
The client does not care about the port number, so the return value of open is not checked. It receives 1000000 bytes after a successful connection.