00001
00002 #include <stdio.h>
00003 #include <stdarg.h>
00004 #include <cstdlib>
00005 #include <sys/stat.h>
00006 #include "tcpsocket.h"
00007 #include "myendian.h"
00008 #include "remotesocket.h"
00009
00010 int RemoteSocket::SendLine(char *fmt, ...)
00011 {
00012 char line[400];
00013 va_list ap;
00014
00015 va_start(ap,fmt);
00016 vsprintf(line, fmt, ap);
00017 va_end(ap);
00018
00019 int size = strlen(line);
00020
00021 #ifdef ANIDEBUG
00022 cout << "RemoteSocket::SendLine: Sending \"" << line << "\" [" << size << "]" << endl << flush;
00023 #endif
00024
00025 SendVar<int>(size);
00026 SendBytes(size, line);
00027
00028 return 0;
00029 }
00030
00031 int RemoteSocket::RecvLine(char *line)
00032 {
00033 int size = RecvVar<int>();
00034 int verify = RecvBytes(size, line);
00035 line[size] = '\0';
00036
00037 #ifdef ANIDEBUG
00038 cout << "RemoteSocket::RecvLine: Received \"" << line << "\" [" << size << "]" << endl << flush;
00039 #endif
00040
00041 return 0;
00042 }
00043
00044 int RemoteSocket::PrintRecvLine()
00045 {
00046 char line[400];
00047 RecvLine(line);
00048 cout << line << endl << flush;
00049
00050 return 0;
00051 }
00052
00053
00054 int RemoteSocket::SendFile(char *filename)
00055 {
00056 ifstream file(filename);
00057
00058
00059 int size = 0;
00060 unsigned char *data;
00061 if (file)
00062 {
00063 size = getFileSize(filename);
00064 ALLOC1D(&data, size);
00065 file.read((char *) data, size);
00066 cout << "Sending binary file \"" << filename << "\" [" << size << " bytes]." << endl << flush;
00067 }
00068 else
00069 {
00070 cout << "File requested by client \"" << filename << "\" not found!!" << endl << flush;
00071 ALLOC1D(&data, size);
00072 }
00073
00074 SendVar<int>(size);
00075 SendBytes(size, data);
00076 file.close();
00077 FREE1D(&data, size);
00078
00079 return 0;
00080 }
00081
00082
00083 int RemoteSocket::RecvFile(char *filename)
00084 {
00085 ofstream file;
00086 int size = RecvVar<int>();
00087
00088 if (size)
00089 {
00090 file.open(filename);
00091 cout << "Receiving binary file \"" << filename << "\" [" << size << " bytes]" << endl << flush;
00092 }
00093 else
00094 {
00095 cout << "Error! Requested file \"" << filename << "\" not available on the server-side!!" << endl << flush;
00096 }
00097
00098 unsigned char *data;
00099 ALLOC1D(&data, size);
00100 RecvBytes(size, data);
00101 if (size)
00102 {
00103 file.write((const char *) data, size);
00104 file.close();
00105 }
00106 FREE1D(&data, size);
00107
00108 return 0;
00109 }
00110