|
| 1 | +#include <stdlib.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | +#include <netdb.h> |
| 5 | +#include <fcntl.h> |
| 6 | +#include <unistd.h> |
| 7 | + |
| 8 | +#include "../include/udt.h" |
| 9 | + |
| 10 | +#define BACKLOG 5 |
| 11 | +#define HOST "127.0.0.1" |
| 12 | +#define PORT "8000" |
| 13 | +#define BUFFER_SIZE 10240 |
| 14 | + |
| 15 | +int main(int argc, char *argv[]) |
| 16 | +{ |
| 17 | + socket_t sock, conn; |
| 18 | + int err; |
| 19 | + struct addrinfo hints, *result; |
| 20 | + |
| 21 | + udt_startup(); |
| 22 | + |
| 23 | + /* get address info */ |
| 24 | + memset(&hints, 0, sizeof(hints)); |
| 25 | + hints.ai_flags = AI_PASSIVE; |
| 26 | + hints.ai_family = AF_INET; |
| 27 | + hints.ai_socktype = SOCK_DGRAM; |
| 28 | + /*hints.ai_socktype = SOCK_STREAM;*/ |
| 29 | + |
| 30 | + if ((err = getaddrinfo(NULL, PORT, &hints, &result)) != 0) { |
| 31 | + fprintf(stderr, "Error: %s\n", gai_strerror(err)); |
| 32 | + exit(err); |
| 33 | + } |
| 34 | + |
| 35 | + /* create a socket */ |
| 36 | + sock = udt_socket(result -> ai_family, |
| 37 | + result -> ai_socktype, |
| 38 | + result -> ai_protocol); |
| 39 | + if (sock == -1) { |
| 40 | + fprintf(stderr, "Could not create socket\n"); |
| 41 | + exit(errno); |
| 42 | + } |
| 43 | + |
| 44 | + /* bind to address */ |
| 45 | + if (udt_bind(sock, result -> ai_addr, result -> ai_addrlen) == -1) { |
| 46 | + fprintf(stderr, "Could not bind socket\n"); |
| 47 | + exit(errno); |
| 48 | + } |
| 49 | + |
| 50 | + freeaddrinfo(result); |
| 51 | + |
| 52 | + if (hints.ai_socktype == SOCK_STREAM) { |
| 53 | + |
| 54 | + /* listen for connections */ |
| 55 | + if (udt_listen(sock, BACKLOG) == -1) { |
| 56 | + fprintf(stderr, "Could not listen on socket\n"); |
| 57 | + exit(errno); |
| 58 | + } else { |
| 59 | + fprintf(stdout, "Listening on %s\n", PORT); |
| 60 | + } |
| 61 | + |
| 62 | + /* get a connection */ |
| 63 | + if ((conn = udt_accept(sock, NULL, NULL)) == -1) { |
| 64 | + fprintf(stderr, "Connection failed\n"); |
| 65 | + exit(errno); |
| 66 | + } else { |
| 67 | + fprintf(stdout, "New connection\n"); |
| 68 | + } |
| 69 | + |
| 70 | + } else { |
| 71 | + conn = sock; |
| 72 | + fprintf(stdout, "Active on %s\n", PORT); |
| 73 | + } |
| 74 | + |
| 75 | + /* send, recv filename */ |
| 76 | + char filename[BUFFER_SIZE]; |
| 77 | + memset(filename, 0, sizeof(filename)); |
| 78 | + udt_recv(conn, filename, BUFFER_SIZE, 0); |
| 79 | + printf("\tFilename: %s\n\n", filename); |
| 80 | + |
| 81 | + /* recv file */ |
| 82 | + int filefd = open(filename, O_RDONLY); |
| 83 | + int64_t offset = 0; |
| 84 | + if (filefd < 0) return 2; |
| 85 | + if (udt_sendfile(conn, filefd, offset, 10, 0) < 0) return 1; |
| 86 | + close(filefd); |
| 87 | + |
| 88 | + printf("\nClient disconnected\n"); |
| 89 | + |
| 90 | + /* close connection */ |
| 91 | + if (udt_close(sock) == -1) { |
| 92 | + fprintf(stderr, "Could not close socket\n"); |
| 93 | + exit(errno); |
| 94 | + } |
| 95 | + |
| 96 | + return 0; |
| 97 | +} |
0 commit comments