Professional Documents
Culture Documents
Lab 3 Report
(MacTella Peer-to-Peer System)
Submitted by:
Gagandeep Chambal – 0541763
Rohan Thivy – 0461139
Introduction:
QuickTime™ and a
decompressor
are needed to see this picture.
Mactella Commands
Command Description
LIST Lists the shared files in the host’s current
directory.
LISTR <ip_address> Lists the shared files in remote peer
<port_number> specified by ip address and port number
SEARCH <fileName > <TTL> Sends a request to other peers with the
specified TTL (integer value).
The search results are printed on screen
with <ip address> and <port number> of
each peer that has the file.
DISCOVER <depth> Updates the list of active hosts and
displays them up to a certain depth
specified by <depth>
GET <filename> <ip_address> Retrieves the file from the peer specified
<port_number> by the ip address and port number
QUIT Exits from the host Mactella peer
Program Flowcharts
Client Connection
Start
Create Control
Socket
Connect to
Server
Create Data
Socket
Connect Data
Port
END
LIST/LISTR COMMAND
START
If IP address = NULL
Open Directory
List Directory
Contents
END
DISCOVER COMMAND
START
Get Depth
Create Discover
Port
Create Discover
Get Packet
Packet
Receive Discover
Packets
Disconnect
YES
IP already
Close Thread
encountered
NO
Add IP to Peer IP
list
Close Thread
GET COMMAND
Start
Create Control
Socket
Connect to
Server
Create Data
Socket
Connect Data
Port
Send Control
Packet
Close connection
END
Source Code:
return clntSock;
}
return sock;
}
Handle TCP Client:
#include <winsock2.h> /* for socket(),... */
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define RCVBUFSIZE 125 /* Size of receive buffer */
#define MESSAGESIZEMAX 125
#define MAXPENDING 50
char LISTFILE[100],LIST[100];
char messageSizeChar[MESSAGESIZEMAX];
char message[MESSAGESIZEMAX];
char command[100];
char file[100];
char fileName[100];
char downloadDirectory[150];
char fileDirectory[150];
char discovered[100];
char searched[100];
char localIP[100] = "127.0.0.1";
char ip_addr[100];
int flagttl=0;
int clntLen;
int transmissionSize;
int transmissionStringSize;
strcpy(LISTFILE,directory);
strcat(LISTFILE,"\\");
strcpy(LIST,LISTFILE);
strcat(LIST,"List.txt");
strcpy(downloadDirectory,directory);
echoBuffer[recvMsgSize] = '\0';
if (strstr(echoBuffer,"NOT") != NULL){
check = 1;
dataPort = atoi(&echoBuffer[3]);
}
else{
dataPort = atoi(echoBuffer);
messageSize = sprintf(message,"----Welcome to MacTella-----\nOPTIONS--\n1)
LIST \n2) LISTR \n3) SEARCH \n4) DISCOVER\n5) GET\n6) QUIT\n\n");
sprintf(messageSizeChar,"%d",messageSize);
memset(&echoServAddr,0,sizeof(echoServAddr));
echoServAddr.sin_family = AF_INET;
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
echoServAddr.sin_port = htons(dataPort);
if (listen(dataSocket,MAXPENDING) < 0)
DieWithError("listen() failed");
clntLen = sizeof(echoClntAddr);
if ((clntDataSocket = accept(dataSocket, (struct sockaddr
*)&echoClntAddr,&clntLen)) < 0)
DieWithError("accept() failed");
strcpy(command,echoBuffer);
while(strcmp(command,"QUIT")!=0)
{
if(strcmp(command,"LIST") == 0)
{
printf("\nSending List...\n\n");
transmissionSize = getSize(LIST);
transmissionStringSize = sprintf(echoBuffer, "%d",
transmissionSize);
byteSent=0;
while(!feof(ufp))
{
transmissionSize = fread(echoBuffer,
sizeof(char),RCVBUFSIZE,ufp);
echoBuffer[transmissionSize]='\0';
printf("%s",echoBuffer);
}
fclose(ufp);
}
else if (strcmp(command,"SEARCH") == 0){
memset(&echoBuffer, 0, sizeof(echoBuffer));
if ((getTypeSize = recv(clntSocket,echoBuffer, RCVBUFSIZE - 1,0))
<= 0)
DieWithError("recv() failed");
echoBuffer[getTypeSize] = '\0';
strcpy(fileName,echoBuffer);
strcat(LISTFILE,fileName);
memset(&echoBuffer,0,sizeof(echoBuffer));
if ((getTypeSize = recv(clntSocket,echoBuffer, RCVBUFSIZE - 1,0))
<= 0)
DieWithError("recv() failed");
echoBuffer[getTypeSize] = '\0';
ttl = atoi(echoBuffer);
ttl--;
if (ttl == 0){
totalBytesRcvd = 0;
memset(&command, 0, sizeof(command));
strcpy(command,"NOT");
echoStringLen = strlen(command);
if (send(sock1, command, echoStringLen, 0) !=
echoStringLen)
DieWithError("send() failed");
srand(time(NULL));
memset(&echoBuffer, 0, sizeof(echoBuffer));
sprintf(echoBuffer,"%d",rand()%99999);
echoStringLen = strlen(echoBuffer); /*
Determine input length */
portNumber = atoi(echoBuffer);
memset(&dataEchoServAddr1, 0,
sizeof(dataEchoServAddr1)); /* Zero out structure */
dataEchoServAddr1.sin_family = AF_INET;
/* Internet address family */
dataEchoServAddr1.sin_addr.s_addr =
inet_addr(knownIP); /* Server IP address */
dataEchoServAddr1.sin_port =
htons(portNumber); /* Server port */
if (connect(clntdataSocket1, (struct sockaddr *)
&dataEchoServAddr1, sizeof(dataEchoServAddr1)) < 0)
DieWithError("connect() client failed");
memset(&command, 0, sizeof(command));
strcpy(command,"SEARCH");
echoStringLen = strlen(command);
if (send(sock1, command, echoStringLen, 0) !=
echoStringLen)
DieWithError("send() failed");
echoStringLen = strlen(fileName);
Sleep(200);
memset(&echoBuffer,0,sizeof(echoBuffer));
strcpy(echoBuffer,fileName);
echoStringLen = strlen(echoBuffer);
if (send(sock1,echoBuffer,echoStringLen,0) !=
echoStringLen)
DieWithError("send() failed");
Sleep(200);
memset(&command,0,sizeof(command));
sprintf(command,"%d",ttl);
echoStringLen = strlen(command);
if (send(sock1,command,echoStringLen,0) !=
echoStringLen)
DieWithError("send() failed");
if (send(clntSocket,echoBuffer,rcvMsgSize,0)
!= rcvMsgSize)
DieWithError("send() failed");
}
else{
flagttl=1;
strcpy(ip_addr,echoBuffer);
if ((rcvMsgSize = recv(clntdataSocket1,
echoBuffer, RCVBUFSIZE,0)) <= 0)
DieWithError("recv() failed");
echoBuffer[rcvMsgSize]='\0';
port_number = atoi(echoBuffer);
}
}else
filefound=1;
if((check == 1)&&(filefound == 1)){
memset(&echoBuffer,0,sizeof(echoBuffer));
strcpy(echoBuffer,localIP);
transmissionSize = strlen(echoBuffer);
Sleep(1000);
if (send(clntDataSocket, echoBuffer, transmissionSize, 0) !
= transmissionSize)
DieWithError("send() failed");
memset(&echoBuffer,0,sizeof(echoBuffer));
sprintf(echoBuffer,"%d",localPort);
transmissionSize = strlen(echoBuffer);
if (send(clntDataSocket, echoBuffer, transmissionSize, 0) !
= transmissionSize)
DieWithError("send() failed");
}
else if (filefound == 1){
memset(&echoBuffer,0,sizeof(echoBuffer));
strcpy(echoBuffer,localIP);
transmissionSize = strlen(echoBuffer);
if (send(clntDataSocket, echoBuffer, transmissionSize, 0) !
= transmissionSize)
DieWithError("send() failed");
memset(&echoBuffer,0,sizeof(echoBuffer));
sprintf(echoBuffer,"%d",localPort);
transmissionSize = strlen(echoBuffer);
if (send(clntDataSocket, echoBuffer, transmissionSize, 0) !
= transmissionSize)
DieWithError("send() failed");
}
else if(flagttl==0){
memset(&echoBuffer,0,sizeof(echoBuffer));
strcpy(echoBuffer,"Cannot Find File");
transmissionSize = strlen(echoBuffer);
if (send(clntDataSocket, echoBuffer, transmissionSize, 0) !
= transmissionSize)
DieWithError("send() failed");
}
else
{
memset(&echoBuffer,0,sizeof(echoBuffer));
strcpy(echoBuffer,ip_addr);
transmissionSize = strlen(echoBuffer);
Sleep(1000);
if (send(clntDataSocket, echoBuffer, transmissionSize, 0) !
= transmissionSize)
DieWithError("send() failed");
memset(&echoBuffer,0,sizeof(echoBuffer));
sprintf(echoBuffer,"%d",port_number);
transmissionSize = strlen(echoBuffer);
if (send(clntDataSocket, echoBuffer, transmissionSize, 0) !
= transmissionSize)
DieWithError("send() failed");
}
closesocket(sock1);
closesocket(clntdataSocket1);
if(filefound)
fclose(ufp);
}
totalBytesRcvd = 0;
memset(&command, 0, sizeof(command));
strcpy(command,"NOT");
echoStringLen = strlen(command);
if (send(sock1, command, echoStringLen, 0) !=
echoStringLen)
DieWithError("send() failed");
srand(time(NULL));
memset(&echoBuffer, 0, sizeof(echoBuffer));
sprintf(echoBuffer,"%d",rand()%99999);
echoStringLen = strlen(echoBuffer); /*
Determine input length */
portNumber = atoi(echoBuffer);
memset(&dataEchoServAddr1, 0,
sizeof(dataEchoServAddr1)); /* Zero out structure */
dataEchoServAddr1.sin_family = AF_INET;
/* Internet address family */
dataEchoServAddr1.sin_addr.s_addr =
inet_addr(knownIP); /* Server IP address */
dataEchoServAddr1.sin_port =
htons(portNumber); /* Server port */
if (connect(clntdataSocket1, (struct sockaddr *)
&dataEchoServAddr1, sizeof(dataEchoServAddr1)) < 0)
DieWithError("connect() client failed");
memset(&command, 0, sizeof(command));
strcpy(command,"DISCOVER");
echoStringLen = strlen(command);
Sleep(200);
if (send(sock1, command, echoStringLen, 0) !=
echoStringLen)
DieWithError("send() failed");
Sleep(200);
memset(&command,0,sizeof(command));
sprintf(command,"%d",depth);
echoStringLen = strlen(command);
if (send(sock1,command,echoStringLen,0) !=
echoStringLen)
DieWithError("send() failed");
echoBuffer[rcvMsgSize]='\0';
strcat(discovered,echoBuffer);
strcat(discovered,"\n");
}
memset(&echoBuffer,0,sizeof(echoBuffer));
strcat(discovered,localIP);
strcat(discovered," ");
memset(&echoBuffer,0,sizeof(echoBuffer));
sprintf(echoBuffer,"%d",localPort);
strcat(discovered,echoBuffer);
strcat(discovered,"\n");
memset(&echoBuffer,0,sizeof(echoBuffer));
strcpy(echoBuffer,discovered);
transmissionSize = strlen(echoBuffer);
if (send(clntDataSocket, echoBuffer, transmissionSize, 0) !=
transmissionSize)
DieWithError("send() failed");
closesocket(sock1);
closesocket(clntdataSocket1);
}
else if (strcmp(command,"GET") == 0)
{
memset(&echoBuffer,0,sizeof(echoBuffer));
memset(&fileName,0,sizeof(fileName));
if ((fileSize = recv(clntSocket, echoBuffer, RCVBUFSIZE - 1, 0)) <=
0)
DieWithError("recv() failed");
echoBuffer[fileSize] = '\0';
strcpy(fileName,echoBuffer);
strcpy(fileDirectory,LISTFILE);
strcat(fileDirectory,fileName);
transmissionSize = getSize(fileDirectory);
transmissionStringSize = sprintf(echoBuffer, "%d",
transmissionSize);
if(transmissionSize != 0)
{
if ((ufp = fopen(fileDirectory,"rb")) == NULL)
printf("Cannot read File!\n");
byteSent = 0;
totByteSent = 0;
recv(clntSocket, echoBuffer, RCVBUFSIZE - 1, 0);
while(!feof(ufp))
{
transmissionSize = fread(echoBuffer, sizeof(char),
RCVBUFSIZE - 1, ufp);
echoBuffer[transmissionSize] = '\0';
totByteSent += byteSent;
}
fclose(ufp);
}
else
printf("File does not exist. Restart");
}
else
printf("Invalid command\n");
if (check == 1) break;
Sleep(100);
printf("Waiting for next client command...\n");
echoBuffer[commandSize] = '\0';
strcpy(command,echoBuffer);
}
if (check != 1) {
flag=0;
printf("Client has exited\n");
closesocket(clntSocket);
printf("Are you sure you want to Quit?\n");
gets(echoBuffer);
if (strcmp(echoBuffer,"Yes")==0){
closesocket(clntDataSocket);
WSACleanup();
*flag=1;
}
}
else{
closesocket(clntSocket);
}
else
{
fseek (fileptr , 0 , SEEK_END);
rewind(fileptr);
fclose(fileptr);
}
return(fileSize);
}
struct ThreadArgs
{
int clntSock; /* Socket descriptor for client */
char directory[1000];
int knownPort;
char knownIP[100];
int flag;
int portnumber;
};
void *ThreadMain2(void *threadArgs);
int sock;
int echoServPort; /* Echo server port */
int sock1; /* Socket descriptor */
int echoServPort1; /* Echo server port */
int echoStringLen; /* Length of string to echo */
int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv() and total bytes
read */
int rcvMsgSize;
int portNumber;
int portNumberLength;
int instructionSize;
int clntdataSocket;
int clntdataSocket1;
int totBytesReceived;
int transSize;
int transStringSize;
int bytesReceived;
int totByteSent;
int byteSent;
int port_number;
FILE *ufp;
FILE *dfp;
// printf("port:%d",echoServPort);
totalBytesRcvd = 0;
srand(time(NULL));
sprintf(echoBuffer,"%d",rand()%9999);
echoStringLen = strlen(echoBuffer); /* Determine input length */
portNumber = atoi(echoBuffer);
portNumberLength = sizeof(portNumber);
echoBuffer[rcvMsgSize] = '\0';
instructionSize = atoi(echoBuffer);
echoBuffer[rcvMsgSize] = '\0';
strcpy(instructions,echoBuffer);
printf("%s",instructions);
gets(echoBuffer);
strcpy(command,echoBuffer);
echoStringLen = strlen(command);
if (strcmp(command,"LISTR") != 0){
if (send(sock, command, echoStringLen, 0) != echoStringLen)
DieWithError("send() failed");
}
while(strcmp(command,"QUIT") != 0)
{
if ((strcmp(command,"LIST") == 0)||(strcmp(command,"LISTR") == 0)||
(strcmp(command,"SEARCH") == 0)||(strcmp(command,"DISCOVER") == 0)||
(strcmp(command,"GET") == 0))
{
if ((strcmp(command,"LIST") == 0))
{
totBytesReceived = 0;
if ((rcvMsgSize = recv(clntdataSocket, echoBuffer,
RCVBUFSIZE, 0)) <= 0)
DieWithError("recv() failed");
echoBuffer[rcvMsgSize] = '\0';
transSize = atoi(echoBuffer);
while (totBytesReceived < transSize)
{
if ((bytesReceived = recv(clntdataSocket,
echoBuffer, RCVBUFSIZE, 0)) <= 0)
DieWithError("recv() failed");
totBytesReceived += bytesReceived;
echoBuffer[bytesReceived] = '\0';
}
}
else if (strcmp(command,"LISTR") == 0)
{
printf("\nPlease enter ip_address\n");
gets(echoBuffer);
printf("%s\n",echoBuffer);
strcpy(ip_addr,echoBuffer);
printf("%s\n",ip_addr);
echoStringLen = strlen(ip_addr);
printf("%s\n",ip_addr);
port_number = atoi(echoBuffer);
totalBytesRcvd = 0;
srand(time(NULL));
sprintf(echoBuffer,"%d",rand()%9999);
echoStringLen = strlen(echoBuffer); /* Determine
input length */
portNumber = atoi(echoBuffer);
portNumberLength = sizeof(portNumber);
echoBuffer[rcvMsgSize] = '\0';
instructionSize = atoi(echoBuffer);
echoBuffer[rcvMsgSize] = '\0';
strcpy(instructions,echoBuffer);
memset(&dataEchoServAddr1, 0, sizeof(dataEchoServAddr1));
/* Zero out structure */
dataEchoServAddr1.sin_family = AF_INET; /*
Internet address family */
dataEchoServAddr1.sin_addr.s_addr = inet_addr(ip_addr);
/* Server IP address */
dataEchoServAddr1.sin_port = htons(portNumber); /*
Server port */
strcpy(command,"LIST");
echoStringLen = strlen(command);
totBytesReceived = 0;
if ((rcvMsgSize = recv(clntdataSocket1, echoBuffer,
RCVBUFSIZE, 0)) <= 0)
DieWithError("recv() failed");
echoBuffer[rcvMsgSize] = '\0';
transSize = atoi(echoBuffer);
totBytesReceived += bytesReceived;
echoBuffer[bytesReceived] = '\0';
printf("\n%s\n",echoBuffer);
}
}
else if (strcmp(command,"SEARCH") == 0)
{
printf("\nPlease enter fileName\n");
gets(echoBuffer);
strcpy(fileName1,echoBuffer);
memset(&echoBuffer,0,sizeof(echoBuffer));
echoStringLen = strlen(fileName1);
sprintf(echoBuffer,"%d",echoStringLen);
strcpy(echoBuffer,fileName1);
if (send(sock,fileName1,echoStringLen,0) != echoStringLen)
DieWithError("send() failed");
strcpy(ttl,echoBuffer);
echoStringLen = strlen(ttl);
if (send(sock,ttl,echoStringLen,0) != echoStringLen)
DieWithError("send() failed");
memset(&echoBuffer,0,sizeof(echoBuffer));
if ((rcvMsgSize = recv(clntdataSocket, echoBuffer,
RCVBUFSIZE,0)) <= 0)
DieWithError("recv() failed");
strcpy(depth,echoBuffer);
echoStringLen = strlen(echoBuffer);
if (send(sock,echoBuffer,echoStringLen,0) != echoStringLen)
DieWithError("send() failed");
memset(&echoBuffer,0,sizeof(echoBuffer));
if ((rcvMsgSize = recv(clntdataSocket, echoBuffer,
RCVBUFSIZE,0)) <= 0)
DieWithError("recv() failed");
strcpy(ip_addr,echoBuffer);
}
else if (strcmp(command,"GET") == 0)
{
memset(&echoBuffer,0,sizeof(echoBuffer));
printf("\n<filname>\n");
gets(echoBuffer);
strcpy(fileName1,echoBuffer);
memset(&echoBuffer,0,sizeof(echoBuffer));
printf("\n<ip_address>\n");
gets(echoBuffer);
strcpy(ip_addr,echoBuffer);
memset(&echoBuffer,0,sizeof(echoBuffer));
printf("\n<port_number>\n");
gets(echoBuffer);
port_number = atoi(echoBuffer);
totalBytesRcvd = 0;
srand(time(NULL));
sprintf(echoBuffer,"%d",rand()%9999);
echoStringLen = strlen(echoBuffer); /* Determine
input length */
portNumber = atoi(echoBuffer);
portNumberLength = sizeof(portNumber);
echoBuffer[rcvMsgSize] = '\0';
instructionSize = atoi(echoBuffer);
echoBuffer[rcvMsgSize] = '\0';
strcpy(instructions,echoBuffer);
memset(&dataEchoServAddr1, 0, sizeof(dataEchoServAddr1));
/* Zero out structure */
dataEchoServAddr1.sin_family = AF_INET; /*
Internet address family */
dataEchoServAddr1.sin_addr.s_addr = inet_addr(ip_addr);
/* Server IP address */
dataEchoServAddr1.sin_port = htons(portNumber); /*
Server port */
memset(&command, 0, sizeof(command));
strcpy(command,"GET");
echoStringLen = strlen(command);
Sleep(200);
echoStringLen = strlen(fileName1);
if (send(sock1,fileName1,echoStringLen,0) != echoStringLen)
DieWithError("send() failed");
memset(&echoBuffer,0,sizeof(echoBuffer));
if ((rcvMsgSize = recv(clntdataSocket1, echoBuffer,
RCVBUFSIZE,0)) <= 0)
DieWithError("recv() failed");
echoBuffer[rcvMsgSize]='\0';
transSize = atoi(echoBuffer);
if (transSize != 0)
{
memset(&listFile,0,sizeof(listFile));
strcat(listFile,rootDirectory);
strcat(listFile,"\\");
strcat(listFile,fileName1);
totBytesReceived = 0;
if (send(sock1,"PATHSET",sizeof(char)*7,0) !=
sizeof(char)*7)
DieWithError("send() failed");
fwrite(echoBuffer, sizeof(char),
bytesReceived, dfp);
totBytesReceived += bytesReceived;
}
fclose(dfp);
}
else
printf("File requested does not exist.
Restart\n\n");
memset(&listFile,0,sizeof(listFile));
strcat(listFile,rootDirectory);
strcat(listFile,"\\");
strcat(listFile,"List.txt");
}
else
printf("Invalid command please try again\n\n");
printf("\n\n%s", instructions);
printf("\nEnter your next command...\n");
gets(echoBuffer);
strcpy(command,echoBuffer);
echoStringLen = strlen(command);
if (send(sock,command,echoStringLen,0) != echoStringLen)
DieWithError("send() failed");
}
printf("You have exited\n");
closesocket(sock);
WSACleanup();
getchar();
return(0);
}
servSock = CreateTCPServerSocket(echoServPort);
Experimental Results:
LIST COMMAND used for Peer operating on Socket 6010
LISTR Command Listing Files under Peer 6010 using Peer 6020
TA:
We Demoed with TA Zhongliang on Friday, 8th April 2010.
Conclusion:
This lab was a good practice of creating a peer-to-peer file-sharing program. We
were amazed as to how quickly big video files were getting transferred to other
students’ computers. As lots of time was spent on coding and debugging the
program, I guess we won’t be taking the common file sharing programs we use
every day for granted. Actually, we decided that we would improve the code even
further and use the program created by us to transfer files among ourselves from
now on instead of transferring files via msn and other programs.