Advertisement
C_Volume2 Internet/ HTML #72265

Get a file from a FTP server using winsock.

This function shows how to get a file from an FTP site.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Dim States(4) As Com ' initialize the command/reply array
Dim State As Integer ' tells where in the commucation process we are
Dim Total As Long ' Total data to recieve
Dim Current As Long ' Current data recieved
Dim Old As Long ' a timer1 data value
Dim server as string
dim Username as String
dim password as string
dim LocalFile as String
dim remotefile as string
Private Sub Command1_Click()
Server = "ftp.microsoft.com"
Username = "anonymous"
Password = "guest"
LocalFile = "c:\vbrun60.exe"
Remotefile = "/Softlib/MSLFILES/VBRUN60.EXE"
States(0).BackCode = "220" ' this is the welcome message from server
States(0).Command = "USER " + username ' logges in.
States(1).BackCode = "331" ' "Username ok. Need password" from server
States(1).Command = "PASS " + password ' send the password
States(2).BackCode = "230" ' "Access allowed" massage from server
States(2).Command = "TYPE I" ' Sets the type
States(3).BackCode = "200" ' "TYPE I OK" from server
States(3).Command = "PORT " ' Port command (enhanced features command button click."
States(4).BackCode = "200" ' On port OK
States(4).Command = "RETR " + remotefile ' send request for file
Winsock1.Close
Winsock2.Close
Do Until Winsock1.State = 0 And Winsock2.State = 0
DoEvents
Loop
Winsock1.RemoteHost = Server
Winsock1.RemotePort = 21
Dim nr1 As Long
Dim nr2 As Long
Randomize Timer
nr1 = Int(Rnd * 126) + 1
nr2 = Int(Rnd * 255) + 1
Winsock2.LocalPort = (nr1 * 256) + nr2
Dim IP As String
IP = Winsock2.LocalIP
Do Until InStr(IP, ".") = 0
IP = Left(IP, InStr(IP, ".") - 1) + "," + Right(IP, Len(IP) - InStr(IP, "."))
Loop
States(3).Command = "PORT " + IP + "," + Trim(Str(nr1)) + "," + Trim(Str(nr2))
Winsock2.Listen
Winsock1.Connect
Open localfile For Output As #1
End Sub
Private Sub Timer1_Timer() ' status timer (calculates speed and elabsed time.)
Dim Left As Long
Label2 = Trim(Str((Current - Old) / 512)) + " KB/s"
If (Current - Old) > 0 Then
Left = Total - Current
Label3 = Trim(Str(Left / (Current - Old))) + " Sec left."
Else
Label3 = "dunno"
End If
Old = Current
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) ' handles the ftp connection
Dim tmpS As String
Winsock1.GetData tmpS, , bytesTotal
If State < 5 Then
If Left(tmpS, 3) = States(State).BackCode Then
Winsock1.SendData States(State).Command + Chr(13) + Chr(10)
Debug.Print States(State).Command + Chr(13) + Chr(10)
State = State + 1
Else
MsgBox "Error! " + Left(tmpS, Len(tmpS) - 2), vbOKOnly + vbCritical, "FTPget"
End If
ElseIf State = 6 Then
Timer1.Enabled = False
MsgBox "Done!", vbOKOnly + vbInformation, "FTPget"
Else
If Left(tmpS, 4) = "150 " Then
Total = Val(Right(tmpS, Len(tmpS) - InStr(tmpS, "(")))
Timer1.Enabled = True
End If
State = State + 1
End If
End Sub
Private Sub Winsock2_Close() ' handles the data connection
Close #1
Winsock1.Close
End Sub
Private Sub Winsock2_ConnectionRequest(ByVal requestID As Long)
Winsock2.Close
Do Until Winsock2.State = 0
DoEvents
Loop
Winsock2.Accept requestID
End Sub
Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
Dim tmpS As String
Winsock2.GetData tmpS, , bytesTotal
Print #1, tmpS;
Current = Current + Len(tmpS)
Label1 = Trim(Str(Current)) + " / " + Trim(Str(Total))
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 500
End Sub

/* by Gaurav Jain */
/* Simulation of an M/M/1 queue with the condition that 10 customers have to enter the queue before
  the service starts. once the service starts the arrivals are purely random in nature. if the 
  server is fast and the queue empties then the server has to wait again for 10 customers to 
  arrive. and the process restarts */
/* this has been simulated using 1000 customers */
/* please use gcc -lm to compile it */
#include <stdio.h>     
#include <stdlib.h>    
#include <math.h>     
/*----- Constants ----------------*/

#define BUSY 1
#define IDLE 0
#define MAXIMUM_NUMBER_OF_CUSTOMERS 1000
/*--- Function prototypes -----------------------------------------------*/
double Exponential(double x);
void write_file(double a[][2],int m);    
void Add_to_queue(int i,double time);
double arrival(int i,double current_time_index,int lamda);
double bernoulli(float a); 
double start_service(double current_time_index,int mu,int lamda);
double departure(double current_time_index,int mu,int lamda);
double arrival_time[1000];
double departure_time[1000];
double service_start_time[1000];
double waiting_time[1000]; 
double service_time [1000]; 
double filearray[5000][2];
int main_counter=0;

struct queue{
int	  customer_number;
double arrival_time_in_system;
double start_service_time;
double service_time;
}customer_queue[1000];
typedef struct queue CUST_QUEUE;

struct queue2{
int	  customer_number;
double arrival_time_in_system;
double start_service_time;
double service_time;
}ultimate_queue[1000];


int number_in_queue=0;
int server_status=0;
int queue_head=0;
int main_count=0;
int number_in_system=0;
int customer_count=0;
int customer_no=0;
int k=0,d=0;

//****************************************************************************//
//			 Main program													 
//****************************************************************************//
void main()
{
 
 int lamda;  // Mean time between arrivals
 int mu;       // Mean service time
 int i=0,j=0,m=0,z=0;
 double time = 0.0,new_time_index=0.0;      // Simulation time
 
 double sum=0;
 double average_waiting_time;
 long int n = 0;        // Number of customers in the system
 // Number of customers that have come to the system till now 
 
 double current_time_index=0;
 
 int max=MAXIMUM_NUMBER_OF_CUSTOMERS;
 int number_before_service_is_started;
 printf("Enter the arrival rate\n");
 scanf("%d",&lamda);
 printf("Enter the service rate\n");
 scanf("%d",&mu);
 printf("enter the value of K (the number of people required in the queue before the service starts\n");
 scanf("%d",&number_before_service_is_started);
 // Main simulation loop
 
 while (customer_count<max)
 {
   
	 if(server_status==IDLE){
		/* put the first k customers in the queue */	
		 queue_head=0;	 
		 filearray[d][0]=number_in_system;
		 filearray[d++][1]=current_time_index;
		 for(i=0;i<number_before_service_is_started;i++)
			{
				/* arrival event ...initially k people have to come in */
			 
			 new_time_index=arrival(customer_count,current_time_index,lamda);	 
			 Add_to_queue(customer_count-1,new_time_index);
			 //-1 because it had been incremented in arrival function
			 current_time_index=new_time_index;
				
			}
 
	 /* start service now */
	 server_status=BUSY;
	 current_time_index=start_service(current_time_index,mu,lamda);
	 }
	

	} //end while

for(i=0;i<MAXIMUM_NUMBER_OF_CUSTOMERS;i++)
{
 departure_time[i]=ultimate_queue[i].service_time+ultimate_queue[i].start_service_time;
 waiting_time[i]= ultimate_queue[i].start_service_time-ultimate_queue[i].arrival_time_in_system;
 sum+=waiting_time[i];		
}
average_waiting_time=sum/MAXIMUM_NUMBER_OF_CUSTOMERS;
printf("Average Waiting time=%f",average_waiting_time);
write_file(filearray,MAXIMUM_NUMBER_OF_CUSTOMERS);
}//end main





//****************************************************************************//
//       Begin function to generate an exponential random number
//****************************************************************************//
double Exponential(double x)
{
 double z;   
 // a uniform RV (0 < z < 1)
 do 
 {
   z = ((double) rand()/RAND_MAX);
 }
 while ((z==0) || (z==1));
 return((-1/x)*log(z));  // formula for exponential Random Variable
}


//****************************************************************************//
//	    Begin function to simulate an arrival event
//****************************************************************************//
//pass the current_time_index and return the arrival time
double arrival(int customer_no,double current_time_index,int lamda)
{
	static int k=0;
	int i;
	double copy=0;
	double random_time;
	
  //set the arrival time for the 1000 customers in one go 
	if(k==0){
		for(i=0;i<MAXIMUM_NUMBER_OF_CUSTOMERS;i++){
		random_time=Exponential(lamda);
		arrival_time[i]=copy+random_time;
		copy=arrival_time[i];
		}
	}
	ultimate_queue[customer_count].customer_number=customer_count;
	ultimate_queue[customer_no].arrival_time_in_system=arrival_time[customer_no];
	customer_no++;
	customer_count++;
   
	filearray[d][0]=number_in_system;
	filearray[d++][1]=current_time_index;
	number_in_system++;
	main_counter++;
	k++;
	return(arrival_time[customer_no-1]);
	
}

//****************************************************************************//
//			Begin function to add a customer to queue
//****************************************************************************//
void Add_to_queue(int customer_nomber,double time)
{
	customer_queue[number_in_queue].customer_number=customer_nomber ;
  	customer_queue[number_in_queue].arrival_time_in_system=time;
	number_in_queue++;
}

//****************************************************************************************//
//	    	Begin function to Start the service
//****************************************************************************************//
double start_service(double current_time_index,int mu,int lamda)
{
	double dept_time_index;
	int i;
	
	
	while(number_in_queue)
	{
	  /* start servicing the first guy in the queue set his service start_time */
		i=customer_queue[queue_head].customer_number;
		service_start_time[i]=current_time_index;
		customer_queue[queue_head].start_service_time=current_time_index;
		ultimate_queue[main_count].start_service_time=current_time_index;
		//call the departure
		dept_time_index=departure(current_time_index,mu,lamda);
		current_time_index=dept_time_index;
		
	}
	server_status=IDLE;
	return(current_time_index);

}

//*************************************************************************************//
//	   	    	Function to start the departure
//*************************************************************************************//
double departure(double current_time_index,int mu,int lamda)
{
	int i;
	double dept_time_index,serv_time,new_time_index;
	serv_time=Exponential(mu);
	customer_queue[queue_head].service_time=serv_time;
	ultimate_queue[main_count].service_time=serv_time;
	dept_time_index=serv_time+current_time_index;
	
	//if an arrival occurs while the customer is being served then signal an arrival
	//and add him to the queue
	
	while(arrival_time[customer_count]<dept_time_index && customer_count<MAXIMUM_NUMBER_OF_CUSTOMERS){
	new_time_index=arrival(customer_count,current_time_index,lamda);
	Add_to_queue(customer_count-1,new_time_index);
	}
  
	number_in_queue--;
	filearray[d][0]=number_in_system;
	filearray[d++][1]=current_time_index;
	number_in_system--;
	
	
	//shift the queue up by one 
	for(i=1;i<MAXIMUM_NUMBER_OF_CUSTOMERS;i++){
	customer_queue[i-1].arrival_time_in_system=customer_queue[i].arrival_time_in_system;		
	customer_queue[i-1].customer_number=customer_queue[i].customer_number;
	customer_queue[i-1].service_time=customer_queue[i].service_time;
	customer_queue[i-1].start_service_time=customer_queue[i].start_service_time;
	}
	main_count++;
	return(dept_time_index);
}


//***********************************************************************************//
//	  	    	Begin function to write to a file
//***********************************************************************************//

void write_file(double filearray[][2],int m)
{
 FILE *fp;
 int i;
 fp=fopen("output.txt","w"); 
 for(i=0;i<m;i++)
 { 
  fprintf(fp,"%f\t\t %f\n",filearray[i][1],filearray[i][0]);
 } 
 fclose(fp);
}
Original Comments (3)
Recovered from Wayback Machine