00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <stdio.h>
00033 #include <conio.h>
00034 #include <vector>
00035
00036 #include "Receiver.h"
00037 #include "Sender.h"
00038 #include "..\common\MessageGeneric.h"
00039 #include "..\common\RNPacket.h"
00040 #include "..\common\RNSocket.h"
00041
00042
00043
00044 const int g_iPort=9050;
00045
00046
00047
00048
00049
00050
00051
00052 RNPRESULT MessageHandler( void* pvoidUserContext,
00053 void* pvoidPacket,
00054 WORD wSize)
00055 {
00056 MessageGeneric* pmsgTmp= (MessageGeneric*)pvoidPacket;
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 return RNP_BUFFER_RETURNED;
00094 }
00095
00096
00097
00098
00099
00100
00101 void senden()
00102 {
00103 RNPRESULT rnprTmp;
00104 char pcEingabe[128];
00105
00106 printf("IP >");
00107
00108 gets(pcEingabe);
00109
00110 RNPacket rnpSend;
00111
00112 if(rnpSend.initMessageCallBack(NULL,&MessageHandler) == RNP_OK)
00113 {
00114 printf("MessageHandler inited.\n");
00115 }
00116 else
00117 {
00118 return;
00119 }
00120
00121 printf("Trying to connect...");
00122
00123 do
00124 {
00125 rnprTmp=rnpSend.socketConnect(pcEingabe,g_iPort);
00126
00127 if (kbhit())
00128 {
00129 printf("\nUser aborted.\n");
00130 getch();
00131 return;
00132 }
00133
00134 Sleep(100);
00135 printf(".");
00136 } while (rnprTmp == RNP_SOCKET_NOT_CONNECTED);
00137
00138 printf("\n");
00139
00140 if (rnprTmp != RNP_OK)
00141 {
00142 printf("Error occured.\n");
00143 return;
00144 }
00145
00146 printf("Connected to %s.\n",pcEingabe);
00147
00148 DWORD dwPacketNr=0;
00149
00150 while(true)
00151 {
00152 printf("File >");
00153
00154 gets(pcEingabe);
00155
00156 if (pcEingabe[0]==0)
00157 {
00158 break;
00159 }
00160
00161 FILE* pfileIn=fopen(pcEingabe,"r");
00162
00163 if (pfileIn==NULL)
00164 {
00165 printf("File does not exist!\n");
00166 continue;
00167 }
00168
00169 MessageCharFragment msgcfTmp;
00170
00171 msgcfTmp.m_Type=MSGID_CHARFRAGMENT;
00172 msgcfTmp.m_dwNextPacketNr=c_dwFirstCharFragment;
00173
00174
00175 int iBuf;
00176
00177 do
00178 {
00179 msgcfTmp.m_dwPacketNr=msgcfTmp.m_dwNextPacketNr;
00180
00181 int iCount;
00182
00183
00184 for (iCount=0; iCount<c_dwSizeCharFragment; iCount++)
00185 {
00186 iBuf=fgetc(pfileIn);
00187 msgcfTmp.m_cBuffer[iCount]=(char)iBuf;
00188
00189 if (iBuf==EOF)
00190 {
00191 msgcfTmp.m_cBuffer[iCount]=0;
00192 }
00193
00194 }
00195
00196 if (iBuf==EOF)
00197 {
00198 msgcfTmp.m_dwNextPacketNr=c_dwLastCharFragment;
00199 }
00200 else
00201 {
00202 msgcfTmp.m_dwNextPacketNr=dwPacketNr;
00203 dwPacketNr++;
00204 }
00205
00206
00207 do
00208 {
00209 rnprTmp=rnpSend.sendMessage((void*)&msgcfTmp,sizeof(msgcfTmp));
00210
00211 if (kbhit())
00212 {
00213 break;
00214 }
00215
00216 } while (rnprTmp==RNP_SOCKET_NOT_WRITEABLE);
00217
00218 if (rnprTmp != RNP_OK)
00219 {
00220 printf("Connection closed...\n");
00221 return;
00222 }
00223
00224 if (kbhit())
00225 {
00226 printf("\nUser aborted.\n");
00227 getch();
00228 return;
00229 }
00230
00231 } while (iBuf!=EOF);
00232
00233 printf("File transmitted.\n");
00234 fclose(pfileIn);
00235 }
00236
00237 if(rnpSend.socketClose() == RNP_OK)
00238 {
00239 printf("Socket closed.\n");
00240 }
00241 else
00242 {
00243 printf("Error occured while closing Socket.\n");
00244 }
00245 }
00246
00247
00248
00249
00250
00251
00252 int main(int argc, char* argv[])
00253 {
00254
00255 if (!RNSocket::init(1,1))
00256 {
00257 printf("Winsock could not be initialised!\n");
00258 return 1;
00259 }
00260
00261 char pcEingabe[128];
00262
00263 printf("(S)end or (R)eceive >");
00264
00265 gets(pcEingabe);
00266
00267 if (pcEingabe[0]=='S' || pcEingabe[0]=='s')
00268 {
00269 Sender sndObj(g_iPort);
00270 sndObj.run();
00271 }
00272
00273 if (pcEingabe[0]=='R' || pcEingabe[0]=='r')
00274 {
00275 Receiver rcvObj(g_iPort);
00276 rcvObj.run();
00277 }
00278
00279
00280 if (!RNSocket::cleanUp())
00281 {
00282 return 1;
00283 }
00284
00285 getch();
00286
00287 return 0;
00288 }
00289