Example #1
0
// ---------------- MAIN PROGRAM -----------------
// Simple usage: client IP port, or client IP (use default port) 
int main(int argc, char *argv[]) {

   //********************************************************************
   // WSSTARTUP
   //********************************************************************
   
   #if defined _WIN32 
      if (WSAStartup(WSVERS, &wsadata) != 0) {
         WSACleanup();
         printf("WSAStartup failed\n");
      }
   #endif

   //*******************************************************************
   // Initialization
   //*******************************************************************

   //My own
   int SN_Base   = 0;
   int SN_Next   = 0;
   timer.count   = 0;
   timer.enabled = 1;
   char sendpacket[12] [BUFFERSIZE];
   
   memset(&localaddr, 0, sizeof(localaddr));//clean up
   int localPort=1234;
   localaddr.sin_family = AF_INET;
   localaddr.sin_addr.s_addr = INADDR_ANY;//server address should be local
   localaddr.sin_port = htons(localPort);
   memset(&remoteaddr, 0, sizeof(remoteaddr));//clean up

   //localaddr.sin_addr.s_addr = inet_addr(localIP);
   //char localIP[INET_ADDRSTRLEN]="127.0.0.1";
  
   randominit();
  
   //********************************************************************
   
   #if defined __unix__ || defined __APPLE__
      int s;
   #elif defined _WIN32
      SOCKET s;
   #endif
   
   char send_buffer[BUFFERSIZE],receive_buffer[BUFFERSIZE];
   remoteaddr.sin_family = AF_INET;
   
   //*******************************************************************
   //	Dealing with user's arguments
   //*******************************************************************
   
   if (argc != 5) {
      printf("2011 code USAGE: client remote_IP-address port  lossesbit(0 or 1) damagebit(0 or 1)\n");
      exit(1);
   }
   
   remoteaddr.sin_addr.s_addr = inet_addr(argv[1]);//IP address
   remoteaddr.sin_port = htons((u_short)atoi(argv[2]));//always get the port number
   packets_lostbit=atoi(argv[3]);
   packets_damagedbit=atoi(argv[4]);
  
   if (packets_damagedbit<0 || packets_damagedbit>1 || packets_lostbit<0 || packets_lostbit>1){
      printf("2011 code USAGE: client remote_IP-address port  lossesbit(0 or 1) damagebit(0 or 1)\n");
      exit(0);
   }
  
   //*******************************************************************
   //CREATE CLIENT'S SOCKET 
   //*******************************************************************
   
   s = socket(AF_INET, SOCK_DGRAM, 0);//this is a UDP socket
   
   if (s < 0) {
      printf("socket failed\n");
      exit(1);
   }

   //***************************************************************
   //NONBLOCKING OPTION for Windows
   //***************************************************************

   #if defined _WIN32
      u_long iMode=1;
      ioctlsocket(s,FIONBIO,&iMode);
   #endif

   //*******************************************************************
   //SEND A TEXT FILE 
   //*******************************************************************
  
   #if defined __unix__ || defined __APPLE__
      FILE *fin=fopen("file1.txt","r");
   #elif defined _WIN32
      FILE *fin=fopen("file1_Windows.txt","r");
   #endif
  
   if(fin==NULL) {
      printf("cannot open file\n");
      exit(0);
   }

   while (1) {

      //Run the timer if enabled
      if(timer.enabled) {
         timer.count++;
      }
      //--------
      memset(send_buffer, 0, sizeof(send_buffer));//clean up the send_buffer before reading the next line
      
      if(SN_Next < (SN_Base + N_GBN)) { //Error checking
         fgets(send_buffer,SEGMENTSIZE,fin);
      }
     
      if (!feof(fin)) {
         //Send normally
			
         if(SN_Next < (SN_Base + N_GBN)) {
            build_packet(send_buffer, SN_Next, sendpacket[SN_Next]); //Build it
            send_unreliably(s, sendpacket[SN_Next], remoteaddr);     //Send it

            if(SN_Base == SN_Next) {
               timer.enabled = 1; //enable the timer
            }
            SN_Next++;
         }

         //if timedout, send all packets as not acknowledged

         if(timer.count > TIMER_LIMIT) {
            timer.enabled = 1; // enable the timer
				int i;
            for(i = SN_Base; i < SN_Next; i++) {
               send_unreliably(s, sendpacket[i], remoteaddr);
               #if defined __unix__ || defined __APPLE__
                  sleep(1);
               #elif defined _WIN32
                  Sleep(1);
               #endif
            }
         }
         
         #if defined __unix__ || defined __APPLE__
            sleep(1);
         #elif defined _WIN32
            Sleep(1000);
         #endif
        
         //********************************************************************
         //RECEIVE
         //********************************************************************
         
         memset(receive_buffer, 0, sizeof(receive_buffer));
         recv_nonblocking(s,receive_buffer, remoteaddr);//you can replace this, but use MSG_DONTWAIT to get non-blocking recv
         printf("RECEIVE --> %s \n",receive_buffer);
         
         //when CRC check is passed

         if(CRC_check(receive_buffer)) {
            

            if(ACK_SN(receive_buffer) > -1) {
               SN_Base = ACK_SN(receive_buffer) + 1;
            }
            //Stop the timer
            if(SN_Base == SN_Next) {
               timer.enabled = 0;
               timer.count   = 0;
            }
            //Start the timer;
            else {
               timer.enabled = 1;
            }
         }

         #if defined __unix__ || defined __APPLE__
            sleep(1);//wait for a bit before trying the next packet
         #elif defined _WIN32
            Sleep(1000);
         #endif

      }
      else {
         printf("end of the file \n"); 
         memset(send_buffer, 0, sizeof(send_buffer)); 
         sprintf(send_buffer,"CLOSE \r\n");
         send_unreliably(s,send_buffer,remoteaddr);//we actually send this reliably, read UDP_supporting_functions_2016.c

         break;
      }
   }
   
   //*******************************************************************
   //CLOSESOCKET   
   //*******************************************************************
   
   printf("closing everything on the client's side ... \n");
   fclose(fin);
   
   #if defined __unix__ || defined __APPLE__
      close(s);
   #elif defined _WIN32
      closesocket(s);
   #endif

   exit(0);
}
Example #2
0
/* MAIN------------------------------------------------------------------------*/
int main(int argc, char *argv[]) 
{
   // WSSTARTUP
	if (WSAStartup(WSVERS, &wsadata) != 0) 
	{
		WSACleanup();
		printf("WSAStartup failed\n");
	}

	// Initialization
	memset(&localaddr, 0, sizeof(localaddr));//clean up
   int localPort=1234;
   localaddr.sin_family = AF_INET;
   localaddr.sin_addr.s_addr = INADDR_ANY;//server address should be local
   localaddr.sin_port = htons(localPort);
   memset(&remoteaddr, 0, sizeof(remoteaddr));//clean up  
   randominit();

   SOCKET s;

   char send_buffer[BUFFESIZE],receive_buffer[BUFFESIZE];
   remoteaddr.sin_family = AF_INET;

	//	Dealing with user's arguments
   if (argc != 5) 
	{
	   printf("2011 code USAGE: client remote_IP-address port  lossesbit(0 or 1) damagebit(0 or 1)\n");
      exit(1);
   }
   remoteaddr.sin_addr.s_addr = inet_addr(argv[1]);//IP address
   remoteaddr.sin_port = htons((u_short)atoi(argv[2]));//always get the port number
   packets_lostbit=atoi(argv[3]);
   packets_damagedbit=atoi(argv[4]);
   if (packets_damagedbit<0 || packets_damagedbit>1 || packets_lostbit<0 || packets_lostbit>1)
	{
	   printf("2011 code USAGE: client remote_IP-address port  lossesbit(0 or 1) damagebit(0 or 1)\n");
	   exit(0);
   }
	

	//CREATE CLIENT'S SOCKET 
   s = socket(AF_INET, SOCK_DGRAM, 0);//this is a UDP socket
   if (s < 0) 
	{
      printf("socket failed\n");
   	exit(1);
   }
	
   //NONBLOCKING OPTION for Windows
   u_long iMode=1;
   ioctlsocket(s,FIONBIO,&iMode);


	//Open A TEXT FILE 
   int counter=0;//sequence of packets
   int ackcounter=0;
   char temp_buffer[BUFFESIZE];
	char CRC_str[BUFFESIZE];

   FILE *fin=fopen("file1_Windows.txt","r");
   if(fin==NULL)
	{
	   printf("cannot open file\n");
	   exit(0);
   }
	
   while (1)
	{
	memset(send_buffer, 0, sizeof(send_buffer));//clean up the send_buffer before reading the next line
	fgets(send_buffer,SEGMENTSIZE,fin);
	if (!feof(fin)) {
		
		//add a headert to the packet with the sequence number
		sprintf(temp_buffer,"PACKET %d ",counter);
		counter++;
		strcat(temp_buffer,send_buffer);
		strcpy(send_buffer,temp_buffer);
		
		//---------coding-------
		temp_buffer[strlen(temp_buffer)-1] = 0;
		sprintf(CRC_str,"%d ",CRCpolynomial(temp_buffer));
		strcat(CRC_str,send_buffer);
		strcpy(send_buffer, CRC_str);
		//----------------------
		
		send_unreliably(s,send_buffer, remoteaddr);		

		Sleep(1000);
		

		//RECEIVE
		memset(receive_buffer, 0, sizeof(receive_buffer));
		recv_nonblocking(s,receive_buffer, remoteaddr);//you can replace this, but use MSG_DONTWAIT to get non-blocking recv
		printf("RECEIVE -->%s__\n",receive_buffer);
		
		
		
		
		//printf("@Alan the CRC check result is %d\n",wy_CRC_check(receive_buffer));
		
		
		

		Sleep(1000);
	}
	else 
	{
		printf("end of the file \n"); 
		memset(send_buffer, 0, sizeof(send_buffer)); 
		sprintf(send_buffer,"CLOSE \r\n");
		send_unreliably(s,send_buffer,remoteaddr);//we actually send this reliably, read UDP_supporting_functions_2012.c
		
		break;
	}
   }

	//CLOSESOCKET   
   printf("closing everything on the client's side ... \n");
   fclose(fin);

   closesocket(s);
   exit(0);
}
Example #3
0
// Simple usage: client IP port, or client IP (use default port) 
int main(int argc, char *argv[]) {
#if defined __unix__ || defined __APPLE__
	
#elif defined _WIN32 
//********************************************************************
// WSSTARTUP
//********************************************************************
	if (WSAStartup(WSVERS, &wsadata) != 0) {
		WSACleanup();
		printf("WSAStartup failed\n");
	}
#endif
//*******************************************************************
// Initialization
//*******************************************************************
   memset(&localaddr, 0, sizeof(localaddr));//clean up
   //char localIP[INET_ADDRSTRLEN]="127.0.0.1";
   int localPort=1234;
   localaddr.sin_family = AF_INET;
   //localaddr.sin_addr.s_addr = inet_addr(localIP);
   localaddr.sin_addr.s_addr = INADDR_ANY;//server address should be local
//********************************************************************
   localaddr.sin_port = htons(localPort);
   memset(&remoteaddr, 0, sizeof(remoteaddr));//clean up  
   randominit();
#if defined __unix__ || defined __APPLE__
   int s;
#elif defined _WIN32
   SOCKET s;
#endif
   char send_buffer[BUFFESIZE],receive_buffer[BUFFESIZE];
   remoteaddr.sin_family = AF_INET;
//*******************************************************************
//	Dealing with user's arguments
//*******************************************************************
   if (argc != 5) {
	   printf("2011 code USAGE: client remote_IP-address port  lossesbit(0 or 1) damagebit(0 or 1)\n");
      exit(1);
   }
   remoteaddr.sin_addr.s_addr = inet_addr(argv[1]);//IP address
   remoteaddr.sin_port = htons((u_short)atoi(argv[2]));//always get the port number
   //localaddr.sin_port = htons((u_short)atoi(argv[3]));//always get the port number
   packets_lostbit=atoi(argv[3]);
   packets_damagedbit=atoi(argv[4]);
   if (packets_damagedbit<0 || packets_damagedbit>1 || packets_lostbit<0 || packets_lostbit>1){
	   printf("2011 code USAGE: client remote_IP-address port  lossesbit(0 or 1) damagebit(0 or 1)\n");
	   exit(0);
   }
//*******************************************************************
//CREATE CLIENT'S SOCKET 
//*******************************************************************
   s = socket(AF_INET, SOCK_DGRAM, 0);//this is a UDP socket
   if (s < 0) {
      printf("socket failed\n");
   	exit(1);
   }
#if defined __unix__ || defined __APPLE__

#elif defined _WIN32
   //***************************************************************//
   //NONBLOCKING OPTION for Windows
   //***************************************************************//
   u_long iMode=1;
   ioctlsocket(s,FIONBIO,&iMode);
#endif
//*******************************************************************
//SEND A TEXT FILE 
//*******************************************************************
   int counter=0;//sequence of packets
   int ackcounter=0;
   char temp_buffer[BUFFESIZE];
	char CRC_str[BUFFESIZE];
#if defined __unix__ || defined __APPLE__
   FILE *fin=fopen("file1.txt","r");
#elif defined _WIN32
   FILE *fin=fopen("file1_Windows.txt","r");
#endif
   if(fin==NULL){
	   printf("cannot open file\n");
	   exit(0);
   }
   while (1){
	memset(send_buffer, 0, sizeof(send_buffer));//clean up the send_buffer before reading the next line
	fgets(send_buffer,SEGMENTSIZE,fin);
	if (!feof(fin)) {
		
		//add a headert to the packet with the sequence number
		sprintf(temp_buffer,"PACKET %d ",counter);
		counter++;
		strcat(temp_buffer,send_buffer);
		strcpy(send_buffer,temp_buffer);
		
		//---------coding-------
		temp_buffer[strlen(temp_buffer)-1] = 0;
		sprintf(CRC_str,"%d ",CRCpolynomial(temp_buffer));
		strcat(CRC_str,send_buffer);
		strcpy(send_buffer, CRC_str);
		//printf("testing:the CRC is %d,lenth is %d \n",CRCpolynomial(temp_buffer),strlen(temp_buffer));
		//printf("len= %d    the last four :%d %d %d %d",strlen(temp_buffer),temp_buffer[34],temp_buffer[35],temp_buffer[36],temp_buffer[37]);
		//----------------------
		
		send_unreliably(s,send_buffer, remoteaddr);
		
#if defined __unix__ || defined __APPLE__
		sleep(1);
#elif defined _WIN32
		Sleep(1000);
#endif
	//********************************************************************
	//RECEIVE
	//********************************************************************
		memset(receive_buffer, 0, sizeof(receive_buffer));
		recv_nonblocking(s,receive_buffer, remoteaddr);//you can replace this, but use MSG_DONTWAIT to get non-blocking recv
		printf("RECEIVE --> %s \n",receive_buffer);
#if defined __unix__ || defined __APPLE__
		sleep(1);//wait for a bit before trying the next packet
#elif defined _WIN32
		Sleep(1000);
#endif
	}
	else {
		printf("end of the file \n"); 
		memset(send_buffer, 0, sizeof(send_buffer)); 
		sprintf(send_buffer,"CLOSE \r\n");
		send_unreliably(s,send_buffer,remoteaddr);//we actually send this reliably, read UDP_supporting_functions_2012.c
		
		break;
	}
   }
//*******************************************************************
//CLOSESOCKET   
//*******************************************************************
   printf("closing everything on the client's side ... \n");
   fclose(fin);
#if defined __unix__ || defined __APPLE__
   close(s);
#elif defined _WIN32
   closesocket(s);
#endif

   exit(0);

}