Go to the documentation of this file.00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <arpa/inet.h>
00004 #include "zlib.h"
00005 #include <sys/types.h>
00006 #include <sys/socket.h>
00007 #include <unistd.h>
00008 #include <string.h>
00009 #include <netdb.h>
00010 #include <iostream>
00011
00012 #define BUFLEN 60000
00013
00014
00015
00016
00017
00018
00019
00020 void getCompressedBuffer(const char* fname, Bytef** buffPtr, unsigned long& zippedSize)
00021 {
00022 FILE* pFile = fopen ( fname , "r" );
00023 if ( pFile==NULL ) { std::cerr << "Can't open " << fname << std::endl; exit(1); }
00024
00025
00026 fseek (pFile , 0 , SEEK_END);
00027 unsigned int lSize = ftell (pFile);
00028 rewind (pFile);
00029
00030
00031 void* buffer = malloc (sizeof(Bytef)*(lSize));
00032
00033 size_t result = fread (buffer, 1, lSize ,pFile);
00034 fclose(pFile);
00035 if ( !result ) { std::cerr << "Failed to read " << fname <<std::endl; exit(1); }
00036
00037
00038
00039
00040
00041
00042 unsigned int deflatedSize = compressBound(lSize) + 4;
00043 Bytef * deflatedBuff = (Bytef*) malloc (sizeof(char)*(deflatedSize));
00044 *((unsigned int*)deflatedBuff) = htonl(lSize);
00045
00046
00047 *buffPtr = deflatedBuff;
00048
00049
00050 zippedSize = deflatedSize;
00051 compress(deflatedBuff+4, &zippedSize, (const Bytef *)buffer, lSize);
00052 zippedSize +=4;
00053
00054
00055
00056
00057
00058
00059
00060 }
00061
00062 int main(int argc, char **argv)
00063 {
00064
00065 int sd = socket(AF_INET,SOCK_DGRAM, 0);
00066 if (sd < 0) { }
00067
00068
00069 struct sockaddr_in cliAddr;
00070 cliAddr.sin_family = AF_INET;
00071 cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
00072 cliAddr.sin_port = htons(0);
00073
00074 int rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
00075 if (rc < 0) {
00076 std::cerr << "Can't bind port %d " << rc << std::endl; exit(1);
00077 }
00078
00079
00080 struct hostent* h = gethostbyname("xrootd.t2.ucsd.edu");
00081 if (!h) {
00082 std::cerr << "Can't get gost ip \n"; exit(1);
00083 }
00084
00085 struct sockaddr_in remoteServAddr;
00086 remoteServAddr.sin_family = h->h_addrtype;
00087 memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
00088 remoteServAddr.sin_port = htons(9699);
00089
00090 Bytef* buff;
00091 unsigned long buffSize;
00092 getCompressedBuffer(argv[1], &buff, buffSize);
00093
00094 int res = sendto(sd, buff, buffSize, 0,
00095 (struct sockaddr *) &remoteServAddr,
00096 sizeof(remoteServAddr));
00097
00098 if (res == -1)
00099 std::cerr << "Sending report has failed." << std::endl;
00100 else
00101 std::cout << "Report has been sent." <<std::endl;
00102
00103 }