Skip to content

Instantly share code, notes, and snippets.

@cjhanks
Last active December 15, 2015 16:19
Show Gist options
  • Save cjhanks/5288264 to your computer and use it in GitHub Desktop.
Save cjhanks/5288264 to your computer and use it in GitHub Desktop.
Basic Uploader using UDT for a quick upload need
#!/bin/bash
set -e
wget http://downloads.sourceforge.net/project/udt/udt/4.11/udt.sdk.4.11.tar.gz
tar xzvf udt.sdk.4.11.tar.gz
pushd udt4
make
pushd src
sudo mv libudt.so /usr/lib
sudo mv udt.h /usr/include
popd
popd
g++ -O2 upload.cc -I/usr/include -L/usr/lib -ludt -o upload -pthread
g++ -O2 download.cc -I/usr/include -L/usr/lib -ludt -o download -pthread
#include <iostream>
#include <fstream>
#include <arpa/inet.h>
#include <string.h>
#include <stdint.h>
#include "udt.h"
using std::ios;
using std::fstream;
static int build_socket(const char *host);
int main (int argc, char *argv[]) {
UDTSOCKET sock;
fstream strm;
int64_t ___k = 0;
int64_t size;
if (1 + 1 != argc) {
fprintf(stderr, "Usage: %s filename \n", argv[0]);
return 1;
}
strm.open(argv[1], fstream::binary | fstream::out);
if (!strm.good()) {
fprintf(stderr, "Invalid file\n");
return 2;
}
if ((sock = build_socket(argv[2])) < 0) {
fprintf(stderr, "Failed to build socket\n");
return 3;
}
UDT::recv(sock, (char*) &size, sizeof(size), 0);
if (UDT::ERROR == UDT::recvfile(sock, strm, ___k, size)) {
fprintf(stderr, "Failed to send file\n");
return 4;
}
return 0;
}
static int build_socket(const char *host) {
sockaddr_in addr;
UDTSOCKET sock = UDT::socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(4000);
addr.sin_addr.s_addr = INADDR_ANY;
if (UDT::ERROR == UDT::bind(sock, (sockaddr*) &addr, sizeof(addr)))
return -1;
UDT::listen(sock, 1);
return UDT::accept(sock, 0x0, 0x0);
}
#include <iostream>
#include <fstream>
#include <arpa/inet.h>
#include <string.h>
#include <stdint.h>
#include "udt.h"
using std::ios;
using std::fstream;
static int build_socket(const char *host);
int main (int argc, char *argv[]) {
UDTSOCKET sock;
fstream strm;
int64_t size;
int64_t ___k = 0;
if (1 + 2 != argc) {
fprintf(stderr, "Usage: %s filename remotehost\n", argv[0]);
return 1;
}
strm.open(argv[1], fstream::binary | fstream::in);
if (!strm.good()) {
fprintf(stderr, "Invalid file\n");
return 2;
}
strm.seekg(0, ios::end);
size = strm.tellg();
strm.seekg(0, ios::beg);
if ((sock = build_socket(argv[2])) < 0) {
fprintf(stderr, "Failed to build socket\n");
return 3;
}
UDT::send(sock, (char*) &size, sizeof(size), 0);
if (UDT::ERROR == UDT::sendfile(sock, strm, ___k, size)) {
fprintf(stderr, "Failed to send file\n");
return 4;
}
}
static int build_socket(const char *host) {
sockaddr_in addr;
UDTSOCKET sock = UDT::socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(4000);
::inet_pton(AF_INET, host, &addr.sin_addr);
::memset(&addr.sin_zero, '\0', sizeof(addr.sin_zero));
if (UDT::ERROR == UDT::connect(sock, (sockaddr*) &addr, sizeof(addr)))
return -1;
else
return sock;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment