├── README.txt
├── FtpClient
├── FtpClient.suo
├── FtpClient
│ ├── pic.png
│ ├── Debug
│ │ ├── Ftp.obj
│ │ ├── Main.obj
│ │ ├── vc100.idb
│ │ ├── vc100.pdb
│ │ ├── FtpClient.obj
│ │ ├── rc.command.1.tlog
│ │ ├── FtpClient.lastbuildstate
│ │ ├── FtpClient_manifest.rc
│ │ ├── FtpClient.exe.embed.manifest.res
│ │ ├── FtpClient.exe.intermediate.manifest
│ │ ├── FtpClient.exe.embed.manifest
│ │ └── FtpClient.log
│ ├── FtpClient.vcxproj.user
│ ├── Main.cpp
│ ├── FtpClient.h
│ ├── Ftp.h
│ ├── FtpClient.vcxproj.filters
│ ├── FtpClient.cpp
│ ├── Ftp.cpp
│ └── FtpClient.vcxproj
└── FtpClient.sln
└── FtpServer
├── FtpServer.suo
├── FtpServer
├── pic.png
├── Debug
│ ├── Ftp.obj
│ ├── Main.obj
│ ├── vc100.idb
│ ├── vc100.pdb
│ ├── FtpServer.obj
│ ├── mt.read.1.tlog
│ ├── mt.write.1.tlog
│ ├── rc.read.1.tlog
│ ├── rc.write.1.tlog
│ ├── cl.command.1.tlog
│ ├── link.read.1.tlog
│ ├── link.write.1.tlog
│ ├── mt.command.1.tlog
│ ├── rc.command.1.tlog
│ ├── link.560.read.1.tlog
│ ├── link.command.1.tlog
│ ├── FtpServer.lastbuildstate
│ ├── FtpServer_manifest.rc
│ ├── link.2540.read.1.tlog
│ ├── link.2540.write.1.tlog
│ ├── link.560.write.1.tlog
│ ├── link.7032.read.1.tlog
│ ├── link.7032.write.1.tlog
│ ├── link-cvtres.read.1.tlog
│ ├── link-cvtres.write.1.tlog
│ ├── link.560-cvtres.read.1.tlog
│ ├── link.2540-cvtres.read.1.tlog
│ ├── link.2540-cvtres.write.1.tlog
│ ├── link.560-cvtres.write.1.tlog
│ ├── link.7032-cvtres.read.1.tlog
│ ├── link.7032-cvtres.write.1.tlog
│ ├── FtpServer.exe.embed.manifest.res
│ ├── FtpServer.exe.intermediate.manifest
│ ├── FtpServer.exe.embed.manifest
│ └── FtpServer.log
├── FtpServer.vcxproj.user
├── Main.cpp
├── Ftp.h
├── FtpServer.h
├── FtpServer.vcxproj.filters
├── Ftp.cpp
├── FtpServer.vcxproj
└── FtpServer.cpp
└── FtpServer.sln
/README.txt:
--------------------------------------------------------------------------------
1 | To learn some network programming I coded a basic FTP Client and Server
--------------------------------------------------------------------------------
/FtpClient/FtpClient.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpClient/FtpClient.suo
--------------------------------------------------------------------------------
/FtpServer/FtpServer.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer.suo
--------------------------------------------------------------------------------
/FtpClient/FtpClient/pic.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpClient/FtpClient/pic.png
--------------------------------------------------------------------------------
/FtpServer/FtpServer/pic.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/pic.png
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Debug/Ftp.obj:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpClient/FtpClient/Debug/Ftp.obj
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/Ftp.obj:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/Ftp.obj
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Debug/Main.obj:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpClient/FtpClient/Debug/Main.obj
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Debug/vc100.idb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpClient/FtpClient/Debug/vc100.idb
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Debug/vc100.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpClient/FtpClient/Debug/vc100.pdb
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/Main.obj:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/Main.obj
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/vc100.idb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/vc100.idb
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/vc100.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/vc100.pdb
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Debug/FtpClient.obj:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpClient/FtpClient/Debug/FtpClient.obj
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/FtpServer.obj:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/FtpServer.obj
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/mt.read.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/mt.read.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/mt.write.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/mt.write.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/rc.read.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/rc.read.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/rc.write.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/rc.write.1.tlog
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Debug/rc.command.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpClient/FtpClient/Debug/rc.command.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/cl.command.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/cl.command.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.read.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.read.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.write.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.write.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/mt.command.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/mt.command.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/rc.command.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/rc.command.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.560.read.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.560.read.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.command.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.command.1.tlog
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Debug/FtpClient.lastbuildstate:
--------------------------------------------------------------------------------
1 | #v4.0:v100
2 | Debug|Win32|C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpClient\|
3 |
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Debug/FtpClient_manifest.rc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpClient/FtpClient/Debug/FtpClient_manifest.rc
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/FtpServer.lastbuildstate:
--------------------------------------------------------------------------------
1 | #v4.0:v100
2 | Debug|Win32|C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpServer\|
3 |
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/FtpServer_manifest.rc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/FtpServer_manifest.rc
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.2540.read.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.2540.read.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.2540.write.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.2540.write.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.560.write.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.560.write.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.7032.read.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.7032.read.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.7032.write.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.7032.write.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link-cvtres.read.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link-cvtres.read.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link-cvtres.write.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link-cvtres.write.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.560-cvtres.read.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.560-cvtres.read.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.2540-cvtres.read.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.2540-cvtres.read.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.2540-cvtres.write.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.2540-cvtres.write.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.560-cvtres.write.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.560-cvtres.write.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.7032-cvtres.read.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.7032-cvtres.read.1.tlog
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/link.7032-cvtres.write.1.tlog:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/link.7032-cvtres.write.1.tlog
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Debug/FtpClient.exe.embed.manifest.res:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpClient/FtpClient/Debug/FtpClient.exe.embed.manifest.res
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/FtpServer.exe.embed.manifest.res:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TheSisb/TCP-FTP/HEAD/FtpServer/FtpServer/Debug/FtpServer.exe.embed.manifest.res
--------------------------------------------------------------------------------
/FtpClient/FtpClient/FtpClient.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/FtpServer/FtpServer/FtpServer.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Main.cpp:
--------------------------------------------------------------------------------
1 | #include "FtpClient.h"
2 |
3 | int main(int argc, char argv[]){
4 | int running = true;
5 |
6 | // Start the server and listen
7 | FtpClient* client = new FtpClient();
8 |
9 | client->run();
10 |
11 | return 0;
12 | }
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Main.cpp:
--------------------------------------------------------------------------------
1 | #include "FtpServer.h"
2 |
3 | int main(int argc, char argv[]){
4 | int running = true;
5 |
6 | // Start the server and listen
7 | FtpServer* server = new FtpServer();
8 |
9 | while (running){
10 | server->accept_new_clients();
11 | running = server->check_server_status();
12 |
13 | }
14 | return 0;
15 | }
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Debug/FtpClient.exe.intermediate.manifest:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/FtpServer.exe.intermediate.manifest:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Debug/FtpClient.exe.embed.manifest:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/FtpServer.exe.embed.manifest:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/FtpClient/FtpClient/FtpClient.h:
--------------------------------------------------------------------------------
1 | #include "Ftp.h"
2 |
3 | #define CONNECT_ON_PORT 1234
4 |
5 | class FtpClient : public Ftp {
6 |
7 | private:
8 | struct sockaddr_in ServerAddr; /* Server address */
9 | char remoteHostname[HOSTNAME_LENGTH];
10 | int sock;
11 | int debug;
12 |
13 | public:
14 | FtpClient();
15 | ~FtpClient();
16 |
17 | bool run();
18 |
19 | bool doDownload(Frame);
20 | bool doUpload(Frame);
21 | bool doShutdown();
22 |
23 |
24 |
25 | };
--------------------------------------------------------------------------------
/FtpClient/FtpClient.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 11.00
3 | # Visual Studio 2010
4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FtpClient", "FtpClient\FtpClient.vcxproj", "{53EBF012-9635-41EF-BF57-25553682ED67}"
5 | EndProject
6 | Global
7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
8 | Debug|Win32 = Debug|Win32
9 | Release|Win32 = Release|Win32
10 | EndGlobalSection
11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
12 | {53EBF012-9635-41EF-BF57-25553682ED67}.Debug|Win32.ActiveCfg = Debug|Win32
13 | {53EBF012-9635-41EF-BF57-25553682ED67}.Debug|Win32.Build.0 = Debug|Win32
14 | {53EBF012-9635-41EF-BF57-25553682ED67}.Release|Win32.ActiveCfg = Release|Win32
15 | {53EBF012-9635-41EF-BF57-25553682ED67}.Release|Win32.Build.0 = Release|Win32
16 | EndGlobalSection
17 | GlobalSection(SolutionProperties) = preSolution
18 | HideSolutionNode = FALSE
19 | EndGlobalSection
20 | EndGlobal
21 |
--------------------------------------------------------------------------------
/FtpServer/FtpServer.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 11.00
3 | # Visual Studio 2010
4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FtpServer", "FtpServer\FtpServer.vcxproj", "{C6B7AE14-C164-475E-BB2C-AE87250E104E}"
5 | EndProject
6 | Global
7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
8 | Debug|Win32 = Debug|Win32
9 | Release|Win32 = Release|Win32
10 | EndGlobalSection
11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
12 | {C6B7AE14-C164-475E-BB2C-AE87250E104E}.Debug|Win32.ActiveCfg = Debug|Win32
13 | {C6B7AE14-C164-475E-BB2C-AE87250E104E}.Debug|Win32.Build.0 = Debug|Win32
14 | {C6B7AE14-C164-475E-BB2C-AE87250E104E}.Release|Win32.ActiveCfg = Release|Win32
15 | {C6B7AE14-C164-475E-BB2C-AE87250E104E}.Release|Win32.Build.0 = Release|Win32
16 | EndGlobalSection
17 | GlobalSection(SolutionProperties) = preSolution
18 | HideSolutionNode = FALSE
19 | EndGlobalSection
20 | EndGlobal
21 |
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Ftp.h:
--------------------------------------------------------------------------------
1 | #pragma comment(lib,"Ws2_32.lib")
2 | #include
3 | #include
4 | #include
5 | #include
6 |
7 | using namespace std;
8 |
9 | #define HOSTNAME_LENGTH 30
10 | #define FILENAME_LENGTH 30
11 | #define BUFFER_SIZE 512
12 | #define LISTEN_PORT 1234
13 |
14 | typedef struct {
15 | int header;
16 | char buffer[BUFFER_SIZE];
17 | int footer;
18 | } Packet;
19 |
20 | typedef struct {
21 | int dir;
22 | char filename[FILENAME_LENGTH];
23 | } Frame;
24 |
25 | class Ftp {
26 | private:
27 | int sock; /* Socket descriptor */
28 | struct sockaddr_in ServAddr; /* server socket address */
29 | unsigned short ServPort; /* server port */
30 | WSADATA wsaData;
31 | char myHostname[HOSTNAME_LENGTH];
32 |
33 |
34 | public:
35 | void Init();
36 | int createSocket();
37 | unsigned long ResolveName(char name[]);
38 | void err_sys(char * fmt,...);
39 |
40 | bool file_exists(char * filename);
41 |
42 | bool readFileAndSend(char* name, int sock);
43 | bool recvFileAndWrite(char * filename, int sock);
44 |
45 | };
46 |
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Ftp.h:
--------------------------------------------------------------------------------
1 | #pragma comment(lib,"Ws2_32.lib")
2 | #include
3 | #include
4 | #include
5 | #include
6 |
7 | using namespace std;
8 |
9 | #define HOSTNAME_LENGTH 30
10 | #define FILENAME_LENGTH 30
11 | #define BUFFER_SIZE 512
12 | #define LISTEN_PORT 1234
13 |
14 | typedef struct {
15 | int header;
16 | char buffer[BUFFER_SIZE];
17 | int footer;
18 | } Packet;
19 |
20 | typedef struct {
21 | int dir;
22 | char filename[FILENAME_LENGTH];
23 | } Frame;
24 |
25 |
26 | class Ftp {
27 | private:
28 | int sock; /* Socket descriptor */
29 | struct sockaddr_in ServAddr; /* server socket address */
30 | unsigned short ServPort; /* server port */
31 | WSADATA wsaData;
32 | char myHostname[HOSTNAME_LENGTH];
33 |
34 |
35 | public:
36 | void Init();
37 | int createSocket();
38 | unsigned long ResolveName(char name[]);
39 | void err_sys(char * fmt,...);
40 |
41 | bool file_exists(char * filename);
42 | bool readFileAndSend(char* name, SOCKET sock);
43 | bool recvFileAndWrite(char * filename, SOCKET sock);
44 |
45 | };
46 |
--------------------------------------------------------------------------------
/FtpServer/FtpServer/FtpServer.h:
--------------------------------------------------------------------------------
1 | #include "Ftp.h"
2 |
3 | #define LISTEN_ON_PORT 1234
4 | #define MAX_CLIENTS 10
5 | // Status messages
6 | #define CLIENT_DISC 0
7 | #define CLIENT_CONN 1
8 | #define CLIENT_X 2
9 |
10 |
11 | struct Client {
12 | bool conn; //Set true if a client is connected
13 | sockaddr_in addr; //Client info like ip address
14 | SOCKET cs; //Client socket
15 | fd_set set; //used to check if there is data in the socket
16 | int i; //any piece of additional info
17 | Frame frame;
18 | bool runner;
19 | };
20 |
21 |
22 | class FtpServer : public Ftp {
23 | private:
24 | struct sockaddr_in ServerAddr; /* Server address */
25 | char servername[HOSTNAME_LENGTH];
26 | int sock;
27 |
28 | public:
29 | FtpServer();
30 | ~FtpServer();
31 |
32 | // for main loop
33 | void accept_clients();
34 |
35 | void accept_new_clients();
36 | bool accept_client(Client *); //accept client connections
37 |
38 | void getFrame(Client*);
39 | bool handleFrame(Client*);
40 |
41 | bool doUpload(Client*);
42 | bool doDownload(Client*);
43 |
44 | bool check_server_status();
45 | void updateStatus(int);
46 | bool disconnectClient(Client *cli);
47 | };
--------------------------------------------------------------------------------
/FtpClient/FtpClient/FtpClient.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | Source Files
20 |
21 |
22 | Source Files
23 |
24 |
25 | Source Files
26 |
27 |
28 |
29 |
30 | Header Files
31 |
32 |
33 | Header Files
34 |
35 |
36 |
--------------------------------------------------------------------------------
/FtpServer/FtpServer/FtpServer.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | Source Files
20 |
21 |
22 | Source Files
23 |
24 |
25 | Source Files
26 |
27 |
28 |
29 |
30 | Header Files
31 |
32 |
33 | Header Files
34 |
35 |
36 |
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Debug/FtpServer.log:
--------------------------------------------------------------------------------
1 | Build started 2/12/2012 11:04:46 PM.
2 | 1>Project "C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpServer\FtpServer\FtpServer.vcxproj" on node 2 (build target(s)).
3 | 1>InitializeBuildStatus:
4 | Creating "Debug\FtpServer.unsuccessfulbuild" because "AlwaysCreate" was specified.
5 | ClCompile:
6 | d:\VS2010\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc100.pdb" /Gd /TP /analyze- /errorReport:prompt FtpServer.cpp
7 | FtpServer.cpp
8 | 1>c:\users\flam\documents\visual studio 2010\projects\ftpserver\ftpserver\ftpserver.cpp(128): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
9 | d:\vs2010\vc\include\string.h(105) : see declaration of 'strcpy'
10 | 1>c:\users\flam\documents\visual studio 2010\projects\ftpserver\ftpserver\ftpserver.cpp(116): warning C4715: 'FtpServer::handleFrame' : not all control paths return a value
11 | ManifestResourceCompile:
12 | All outputs are up-to-date.
13 | Link:
14 | d:\VS2010\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpServer\Debug\FtpServer.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /ManifestFile:"Debug\FtpServer.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpServer\Debug\FtpServer.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpServer\Debug\FtpServer.lib" /MACHINE:X86 Debug\FtpServer.exe.embed.manifest.res
15 | Debug\Ftp.obj
16 | Debug\FtpServer.obj
17 | Debug\Main.obj
18 | Manifest:
19 | C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\mt.exe /nologo /verbose /out:"Debug\FtpServer.exe.embed.manifest" /manifest Debug\FtpServer.exe.intermediate.manifest
20 | All outputs are up-to-date.
21 | LinkEmbedManifest:
22 | All outputs are up-to-date.
23 | FtpServer.vcxproj -> C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpServer\Debug\FtpServer.exe
24 | FinalizeBuildStatus:
25 | Deleting file "Debug\FtpServer.unsuccessfulbuild".
26 | Touching "Debug\FtpServer.lastbuildstate".
27 | 1>Done Building Project "C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpServer\FtpServer\FtpServer.vcxproj" (build target(s)).
28 |
29 | Build succeeded.
30 |
31 | Time Elapsed 00:00:02.30
32 |
--------------------------------------------------------------------------------
/FtpClient/FtpClient/FtpClient.cpp:
--------------------------------------------------------------------------------
1 | #include "FtpClient.h"
2 |
3 | FtpClient::FtpClient(){
4 | // Load basic starting routine (WSAstartup & local hostname)
5 | Ftp::Init();
6 |
7 | // Create the socket
8 | sock = Ftp::createSocket();
9 |
10 | cout << "Please enter the hostname to connect to: ";
11 | cin >> remoteHostname;
12 |
13 | //connect to the server
14 | memset(&ServerAddr, 0, sizeof(ServerAddr)); /* Zero out structure */
15 |
16 | ServerAddr.sin_family = AF_INET; /* Internet address family */
17 | ServerAddr.sin_addr.s_addr = ResolveName(remoteHostname); /* Server IP address */
18 | ServerAddr.sin_port = htons(CONNECT_ON_PORT); /* Server port */
19 |
20 | if (connect(sock, (struct sockaddr *) &ServerAddr, sizeof(ServerAddr)) < 0)
21 | err_sys("Socket Creating Error");
22 | cout << "\nConnected to the server successfully\n";
23 | }
24 |
25 | FtpClient::~FtpClient(){
26 | WSACleanup();
27 | }
28 |
29 | bool FtpClient::run(){
30 | Frame fr;
31 | bool running = true;
32 | while(running){
33 | cout << "Would you like to UPLOAD (0) or DOWNLOAD (1) or EXIT (else)?\n";
34 | cin >> fr.dir;
35 |
36 | if (fr.dir != 0 && fr.dir != 1)
37 | doShutdown();
38 |
39 | cout << "\nWhich file?\n";
40 | cin >> fr.filename;
41 |
42 |
43 | cout << "Notifying server of action...\n";
44 | debug = send(sock,(char*)&fr,sizeof(Frame),0);
45 | cout << "Debug = " << debug << endl;
46 |
47 | cout << "Receiving reply: ";
48 | char rdyState[2];
49 | debug = recv(sock,rdyState,sizeof(rdyState),0);
50 | cout << debug << " " << rdyState << endl;
51 |
52 | switch (fr.dir) {
53 | case 0:
54 | cout <<"so.. we're uploading \n";
55 | Ftp::readFileAndSend(fr.filename, sock);
56 | break;
57 | case 1:
58 | cout <<"so.. we're downloading \n";
59 | Ftp::recvFileAndWrite(fr.filename, sock);
60 | break;
61 | default:
62 | doShutdown();
63 | break;
64 | }
65 | cout << "\nWould you like to go again? y/n: ";
66 | cin >> rdyState;
67 | cout << endl << rdyState << endl;
68 | if (rdyState[0] == 'n')
69 | running = false;
70 | send(sock,rdyState,sizeof(rdyState),0);
71 | }
72 | cin >> debug;
73 | return 1;
74 | }
75 |
76 | bool FtpClient::doUpload(Frame f){
77 | if (!file_exists(f.filename))
78 | //send size 0 to notify of shutdown
79 | err_sys("That file doesn't exist!\n");
80 | return 1;
81 | }
82 |
83 | bool FtpClient::doDownload(Frame f){
84 |
85 |
86 | return 1;
87 | }
88 |
89 |
90 |
91 | bool FtpClient::doShutdown(){
92 | cout << "\nClosing! <3";
93 |
94 | // shutdown the send half of the connection since no more data will be sent
95 | if (shutdown(sock, 2) == SOCKET_ERROR) {
96 | printf("shutdown failed: %d\n", WSAGetLastError());
97 | closesocket(sock);
98 | WSACleanup();
99 | return 1;
100 | }
101 |
102 | // cleanup
103 | closesocket(sock);
104 | WSACleanup();
105 | return 0;
106 | }
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Debug/FtpClient.log:
--------------------------------------------------------------------------------
1 | Build started 2/12/2012 11:04:48 PM.
2 | 1>Project "C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpClient\FtpClient\FtpClient.vcxproj" on node 2 (build target(s)).
3 | 1>InitializeBuildStatus:
4 | Creating "Debug\FtpClient.unsuccessfulbuild" because "AlwaysCreate" was specified.
5 | ClCompile:
6 | d:\VS2010\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc100.pdb" /Gd /TP /analyze- /errorReport:prompt Ftp.cpp
7 | Ftp.cpp
8 | 1>c:\users\flam\documents\visual studio 2010\projects\ftpclient\ftpclient\ftp.cpp(62): warning C4800: 'void *' : forcing value to bool 'true' or 'false' (performance warning)
9 | 1>c:\users\flam\documents\visual studio 2010\projects\ftpclient\ftpclient\ftp.cpp(89): warning C4244: '=' : conversion from 'std::streamsize' to 'int', possible loss of data
10 | 1>c:\users\flam\documents\visual studio 2010\projects\ftpclient\ftpclient\ftp.cpp(111): warning C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
11 | d:\vs2010\vc\include\string.h(110) : see declaration of 'strcat'
12 | ManifestResourceCompile:
13 | All outputs are up-to-date.
14 | Link:
15 | d:\VS2010\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpClient\Debug\FtpClient.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /ManifestFile:"Debug\FtpClient.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpClient\Debug\FtpClient.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpClient\Debug\FtpClient.lib" /MACHINE:X86 Debug\FtpClient.exe.embed.manifest.res
16 | Debug\Ftp.obj
17 | Debug\FtpClient.obj
18 | Debug\Main.obj
19 | Manifest:
20 | C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\mt.exe /nologo /verbose /out:"Debug\FtpClient.exe.embed.manifest" /manifest Debug\FtpClient.exe.intermediate.manifest
21 | All outputs are up-to-date.
22 | LinkEmbedManifest:
23 | All outputs are up-to-date.
24 | FtpClient.vcxproj -> C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpClient\Debug\FtpClient.exe
25 | FinalizeBuildStatus:
26 | Deleting file "Debug\FtpClient.unsuccessfulbuild".
27 | Touching "Debug\FtpClient.lastbuildstate".
28 | 1>Done Building Project "C:\Users\Flam\Documents\Visual Studio 2010\Projects\FtpClient\FtpClient\FtpClient.vcxproj" (build target(s)).
29 |
30 | Build succeeded.
31 |
32 | Time Elapsed 00:00:02.08
33 |
--------------------------------------------------------------------------------
/FtpClient/FtpClient/Ftp.cpp:
--------------------------------------------------------------------------------
1 | #include "Ftp.h"
2 |
3 | void Ftp::Init(){
4 | cout << "/***********************************/\n";
5 | cout << "/* Welcome to ShadiFTP (server) */\n";
6 | cout << "/***********************************/\n\n";
7 |
8 | // Initialize WinSock
9 | if (WSAStartup(0x0202,&wsaData) != 0) {
10 | WSACleanup();
11 | err_sys("Error in starting WSAStartup()\n");
12 | }
13 | cout << "Winsock started.\n";
14 | // Get and print our own hostname
15 | if(gethostname(myHostname, HOSTNAME_LENGTH) != 0 )
16 | err_sys("can not get the host name,program exit");
17 | cout << "Starting at host: " << myHostname << endl;
18 | }
19 |
20 | int Ftp::createSocket(){
21 | //Create the socket
22 | sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
23 |
24 | if(sock==INVALID_SOCKET )
25 | err_sys("Invalid Socket ",WSAGetLastError());
26 | else if(sock==SOCKET_ERROR)
27 | err_sys("Socket Error)",WSAGetLastError());
28 | else
29 | cout<<"\nSOCKET Established"<h_addr_list[0]);
44 | }
45 |
46 | //from Richard Stevens's source code
47 | void Ftp::err_sys(char * fmt,...) {
48 | int x;
49 | perror(NULL);
50 | va_list args;
51 | va_start(args,fmt);
52 | fprintf(stderr,"error: ");
53 | vfprintf(stderr,fmt,args);
54 | fprintf(stderr,"\n");
55 | va_end(args);
56 | std::cin >> x;
57 | exit(1);
58 | }
59 |
60 | bool Ftp::file_exists(char * filename){
61 | ifstream ifile(filename);
62 | return ifile;
63 | }
64 |
65 | bool Ftp::readFileAndSend(char* filename, int sock){
66 | Packet p;
67 | p.header = 1;
68 | p.footer = 0;
69 | int test;
70 |
71 | // Open
72 | cout << "Opening the file for Reading...\n";
73 | ifstream is (filename, ios::in | ios::binary);
74 |
75 | // Check if error
76 | if ( !is ) {
77 | cout << "Error opening the file to read :/\n";
78 | return 0;
79 | }
80 |
81 | // Read and Write entire contents
82 | while ( is ) {
83 | // Read
84 | is.read(p.buffer, BUFFER_SIZE);
85 | //cout << "\nInput file pointer at: " << is.tellg() << "\nAmount read: " << is.gcount();
86 |
87 | // eof + last packet
88 | if (is.gcount() < BUFFER_SIZE)
89 | p.footer = is.gcount();
90 |
91 | test = send(sock,(char*)&p,sizeof(Packet),0);
92 | //cout << test << endl;
93 |
94 | }
95 |
96 | // Close stuff
97 | cout << "closing read file.\n";
98 | is.close();
99 | return 1;
100 | }
101 |
102 |
103 |
104 | bool Ftp::recvFileAndWrite(char* filename, int sock){
105 | Packet p;
106 | p.header = 1;
107 | p.footer = 0;
108 | int test;
109 |
110 | char fname[FILENAME_LENGTH+1] = "2";
111 | strcat(fname, filename);
112 |
113 | // Open
114 | cout << "Opening the file for writing...\n";
115 | ofstream os (fname, ios::out | ios::binary);
116 |
117 | // Check if error
118 | if ( !os ) {
119 | cout << "Error opening the file to write :/\n";
120 | return 0;
121 | }
122 | int i=0;
123 | while ( true ) {
124 | //cout << i << endl;
125 | test = recv(sock,(char*)&p, sizeof(Packet),0);
126 | //cout << test;
127 |
128 | if(p.footer == 0)
129 | os.write(p.buffer, BUFFER_SIZE);
130 | else{
131 | os.write(p.buffer, p.footer);
132 | //cout << "last packet sent of size: " << p.footer << endl;
133 | break;
134 | }
135 | i++;
136 | }
137 | cout << "closing written file.\n";
138 | os.close();
139 | return 1;
140 | }
--------------------------------------------------------------------------------
/FtpServer/FtpServer/Ftp.cpp:
--------------------------------------------------------------------------------
1 | #include "Ftp.h"
2 |
3 | void Ftp::Init(){
4 | cout << "/***********************************/\n";
5 | cout << "/* Welcome to ShadiFTP (server) */\n";
6 | cout << "/***********************************/\n\n";
7 |
8 | // Initialize WinSock
9 | if (WSAStartup(0x0202,&wsaData) != 0) {
10 | WSACleanup();
11 | err_sys("Error in starting WSAStartup()\n");
12 | }
13 | cout << "Winsock started.\n";
14 | // Get and print our own hostname
15 | if(gethostname(myHostname, HOSTNAME_LENGTH) != 0 )
16 | err_sys("can not get the host name,program exit");
17 | cout << "Starting at host: " << myHostname << endl;
18 | }
19 |
20 | int Ftp::createSocket(){
21 | //Create the socket
22 | sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
23 |
24 | if(sock==INVALID_SOCKET )
25 | err_sys("Invalid Socket ",WSAGetLastError());
26 | else if(sock==SOCKET_ERROR)
27 | err_sys("Socket Error)",WSAGetLastError());
28 | else
29 | cout<<"\nSOCKET Established"<h_addr_list[0]);
44 | }
45 |
46 | //from Richard Stevens's source code
47 | void Ftp::err_sys(char * fmt,...) {
48 | int x;
49 | perror(NULL);
50 | va_list args;
51 | va_start(args,fmt);
52 | fprintf(stderr,"error: ");
53 | vfprintf(stderr,fmt,args);
54 | fprintf(stderr,"\n");
55 | va_end(args);
56 | std::cin >> x;
57 | exit(1);
58 | }
59 |
60 | bool Ftp::file_exists(char * filename){
61 | ifstream ifile(filename);
62 | return ifile;
63 | }
64 |
65 | bool Ftp::readFileAndSend(char* filename, SOCKET sock){
66 | Packet p;
67 | p.header = 1;
68 | p.footer = 0;
69 | int test;
70 |
71 | // Open
72 | cout << "Opening the file for Reading...\n";
73 | ifstream is (filename, ios::in | ios::binary);
74 |
75 | // Check if error
76 | if ( !is ) {
77 | cout << "Error opening the file to read :/\n";
78 | return 0;
79 | }
80 |
81 | // Read and Write entire contents
82 | while ( is ) {
83 | // Read
84 | is.read(p.buffer, BUFFER_SIZE);
85 | //cout << "\nInput file pointer at: " << is.tellg() << "\nAmount read: " << is.gcount();
86 |
87 | // eof + last packet
88 | if (is.gcount() < BUFFER_SIZE)
89 | p.footer = is.gcount();
90 |
91 | test = send(sock,(char*)&p,sizeof(Packet),0);
92 | //cout << test << endl;
93 |
94 | }
95 |
96 | // Close stuff
97 | cout << "closing read file.\n";
98 | is.close();
99 | return 1;
100 | }
101 |
102 |
103 |
104 | bool Ftp::recvFileAndWrite(char* filename, SOCKET sock){
105 | Packet p;
106 | p.header = 1;
107 | p.footer = 0;
108 | int test;
109 |
110 | char fname[FILENAME_LENGTH+1] = "2";
111 | strcat(fname, filename);
112 |
113 | // Open
114 | cout << "Opening the file for writing...\n";
115 | ofstream os (fname, ios::out | ios::binary);
116 |
117 | // Check if error
118 | if ( !os ) {
119 | cout << "Error opening the file to write :/\n";
120 | return 0;
121 | }
122 | int i=0;
123 | while ( true ) {
124 | //cout << i << endl;
125 | test = recv(sock,(char*)&p, sizeof(Packet),0);
126 | //cout << test;
127 | if(p.footer == 0)
128 | os.write(p.buffer, BUFFER_SIZE);
129 | else{
130 | os.write(p.buffer, p.footer);
131 | //cout << "last packet sent of size: " << p.footer << endl;
132 | break;
133 | }
134 | i++;
135 | }
136 | cout << "closing written file.\n";
137 | os.close();
138 | return 1;
139 | }
--------------------------------------------------------------------------------
/FtpClient/FtpClient/FtpClient.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 |
14 | {53EBF012-9635-41EF-BF57-25553682ED67}
15 | Win32Proj
16 | FtpClient
17 |
18 |
19 |
20 | Application
21 | true
22 | Unicode
23 |
24 |
25 | Application
26 | false
27 | true
28 | Unicode
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 | true
42 |
43 |
44 | false
45 |
46 |
47 |
48 |
49 |
50 | Level3
51 | Disabled
52 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
53 |
54 |
55 | Console
56 | true
57 |
58 |
59 |
60 |
61 | Level3
62 |
63 |
64 | MaxSpeed
65 | true
66 | true
67 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
68 |
69 |
70 | Console
71 | true
72 | true
73 | true
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
--------------------------------------------------------------------------------
/FtpServer/FtpServer/FtpServer.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 |
14 | {C6B7AE14-C164-475E-BB2C-AE87250E104E}
15 | Win32Proj
16 | FtpServer
17 |
18 |
19 |
20 | Application
21 | true
22 | Unicode
23 |
24 |
25 | Application
26 | false
27 | true
28 | Unicode
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 | true
42 |
43 |
44 | false
45 |
46 |
47 |
48 |
49 |
50 | Level3
51 | Disabled
52 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
53 |
54 |
55 | Console
56 | true
57 |
58 |
59 |
60 |
61 | Level3
62 |
63 |
64 | MaxSpeed
65 | true
66 | true
67 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
68 |
69 |
70 | Console
71 | true
72 | true
73 | true
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
--------------------------------------------------------------------------------
/FtpServer/FtpServer/FtpServer.cpp:
--------------------------------------------------------------------------------
1 | #include "FtpServer.h"
2 |
3 | // Create client Obj array
4 | Client client[MAX_CLIENTS];
5 | bool status = true;
6 | int connections = 0;
7 | int dbug;
8 |
9 | FtpServer::FtpServer(){
10 | int debug;
11 |
12 | // Load basic starting routine (WSAstartup & local hostname)
13 | Ftp::Init();
14 |
15 | /* Zero out structure */
16 | memset(&ServerAddr, 0, sizeof(ServerAddr));
17 |
18 | ServerAddr.sin_family = AF_INET; /* Internet address family */
19 | ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
20 | ServerAddr.sin_port = htons(LISTEN_ON_PORT); /* Local port */
21 |
22 | // Create the socket
23 | sock = Ftp::createSocket();
24 |
25 | //Bind the server socket
26 | debug = bind(sock,(struct sockaddr *) &ServerAddr, sizeof(ServerAddr)) ;
27 |
28 | cout << "\nBinding socket:" << debug << endl;
29 | if(debug != 0)
30 | Ftp::err_sys("BIND FAILED",WSAGetLastError());
31 | else
32 | cout<<"Socket Bound to port : "<< LISTEN_ON_PORT << endl;
33 |
34 | //Successfull bind, now listen for Server requests.
35 | debug = listen(sock,5);
36 | if (debug < 0)
37 | Ftp::err_sys("Listen error :(");
38 | else
39 | cout<< "Server Listen mode : " << debug << endl;
40 |
41 | // asynchronous server tweak
42 | //unsigned long b=1;
43 | //ioctlsocket(sock,FIONBIO,&b);
44 |
45 | cout << "Waiting for clients!\n";
46 | }
47 |
48 | FtpServer::~FtpServer(){
49 | WSACleanup();
50 | }
51 |
52 | void FtpServer::accept_new_clients(){
53 | for(int i=0; i < MAX_CLIENTS; i++) {
54 | // if open slot
55 | if( !(client[i].conn)) {
56 | // if accept client works
57 | if(accept_client(&client[i])) {
58 | client[i].runner = true;
59 | updateStatus(CLIENT_CONN);
60 |
61 | while(client[i].runner){
62 | // Get this clients request frame
63 | getFrame(&client[i]);
64 | //Handle request frame
65 | client[i].runner = handleFrame(&client[i]);
66 | }
67 | }
68 | }
69 | }
70 | }
71 | bool FtpServer::check_server_status(){
72 | return status;
73 | }
74 |
75 | bool FtpServer::doUpload(Client* cli){
76 | cout << "Sending client ready state for upload\n";
77 | char rdyState[2] = "u";
78 |
79 | dbug = send(cli->cs,rdyState,sizeof(rdyState),0);
80 | cout << dbug;
81 |
82 | recvFileAndWrite(cli->frame.filename, cli->cs);
83 |
84 | // check if we should close this client
85 | recv(cli->cs,rdyState,sizeof(rdyState),0);
86 | if (rdyState[0] == 'y')
87 | return 1;
88 |
89 | updateStatus(CLIENT_DISC);
90 | return 0;
91 | }
92 |
93 | bool FtpServer::doDownload(Client* cli){
94 | cout << "Sending client ready state for download\n";
95 | char rdyState[2] = "d";
96 |
97 | dbug = send(cli->cs,rdyState,sizeof(rdyState),0);
98 | cout << dbug;
99 |
100 | readFileAndSend(cli->frame.filename, cli->cs);
101 |
102 | // check if we should close this client
103 | recv(cli->cs,rdyState,sizeof(rdyState),0);
104 | if (rdyState[0] == 'y')
105 | return 1;
106 |
107 | updateStatus(CLIENT_DISC);
108 | return 0;
109 | }
110 |
111 | bool FtpServer::handleFrame(Client* cli){
112 | if (cli->frame.dir == 0)
113 | return doUpload(cli);
114 | else if (cli->frame.dir == 1)
115 | return doDownload(cli);
116 | }
117 |
118 | void FtpServer::getFrame(Client* cli){
119 | Frame f;
120 |
121 | dbug = recv(cli->cs,(char*)&f,sizeof(Frame),0);
122 |
123 | cout << endl << dbug;
124 | cout << endl << f.dir;
125 | cout << endl << f.filename;
126 |
127 | cli->frame.dir = f.dir;
128 | strcpy(cli->frame.filename, f.filename);
129 | }
130 |
131 |
132 | bool FtpServer::accept_client(Client *cli){
133 | cli->i = sizeof(sockaddr); // save address
134 |
135 | cli->cs = accept(sock, (struct sockaddr *)&cli->addr, &cli->i);
136 |
137 | // create client thread here
138 | //CreateThread(NULL, 0,receive_cmds, (LPVOID)cli->cs, 0, &thread);
139 |
140 | if(cli->cs != 0 && cli->cs != SOCKET_ERROR) {
141 | cli->conn = true;
142 | FD_ZERO(&cli->set);
143 | FD_SET(cli->cs,&cli->set);
144 | return true;
145 | }
146 | return false;
147 | }
148 |
149 | void FtpServer::updateStatus(int msg) {
150 | if(msg == CLIENT_CONN) {
151 | connections++;
152 | cout<<"\nWow, a client has connected. There are now " << connections << " clients connected.";
153 | }
154 | else if(msg == CLIENT_DISC) {
155 | connections--;
156 | cout<<"Wee, a client has disconnected. There are now " << connections << " clients connected.";
157 | } else {
158 | //never leave out anything
159 | cout<<"\n>>>>>>We got an unknown message :" << msg;
160 | }
161 | }
162 | bool FtpServer::disconnectClient(Client *cli){
163 | if(cli->cs)
164 | closesocket(cli->cs);
165 | cli->conn = false;
166 | cli->i = -1;
167 | updateStatus(CLIENT_DISC);
168 | return true;
169 | }
--------------------------------------------------------------------------------