Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TIES323
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
joalhelk
TIES323
Commits
1a7faa01
Commit
1a7faa01
authored
3 years ago
by
joalhelk
Browse files
Options
Downloads
Patches
Plain Diff
rrq finished
parent
c4962ab2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
FTPClient/FTPClient/TftpClient.cs
+32
-3
32 additions, 3 deletions
FTPClient/FTPClient/TftpClient.cs
TFTPServer/TFTPServer/Server.cs
+48
-25
48 additions, 25 deletions
TFTPServer/TFTPServer/Server.cs
with
80 additions
and
28 deletions
FTPClient/FTPClient/TftpClient.cs
+
32
−
3
View file @
1a7faa01
...
...
@@ -11,10 +11,12 @@ namespace FTPClient
public
static
Socket
socket
;
public
static
IPEndPoint
ipEndp
;
public
static
EndPoint
endpoint
;
public
static
string
status
;
private
static
int
OpCode
;
public
static
NetworkStream
ns
;
public
static
StreamReader
sr
;
public
static
StreamWriter
sw
;
public
static
string
status
;
/// <summary>
/// Enum for packet types
...
...
@@ -38,7 +40,7 @@ namespace FTPClient
}
/// <summary>
/// Initialize socket and send out a
write
request
/// Initialize socket and send out a
read
request
/// </summary>
public
void
Initialize
()
{
...
...
@@ -50,7 +52,7 @@ namespace FTPClient
string
filename
=
"README"
;
byte
[]
packet
=
new
byte
[
4
+
filename
.
Length
+
mode
.
Length
];
packet
[
1
]
=
(
byte
)
PACKET_TYPE
.
RRQ
;
OpCode
=
(
int
)
PACKET_TYPE
.
RRQ
;
int
index
=
2
;
foreach
(
char
letter
in
filename
)
{
...
...
@@ -66,6 +68,33 @@ namespace FTPClient
}
socket
.
SendTo
(
packet
,
endpoint
);
handleReadRequest
();
}
private
void
handleReadRequest
()
{
byte
[]
buffer
=
new
byte
[
256
];
socket
.
ReceiveFrom
(
buffer
,
ref
endpoint
);
int
opCode
=
(
int
)(
buffer
[
1
]
<<
8
|
buffer
[
0
]);
if
(!
CheckOpCode
(
opCode
))
return
;
}
private
static
bool
CheckOpCode
(
int
op
)
{
if
(!
op
.
Equals
(
OpCode
))
{
SendError
(
"OpCode is incorrect!"
);
return
false
;
}
return
true
;
}
private
static
void
SendError
(
string
error
)
{
Console
.
WriteLine
(
error
);
// TODO: Send error message
}
/// <summary>
...
...
This diff is collapsed.
Click to expand it.
TFTPServer/TFTPServer/Server.cs
+
48
−
25
View file @
1a7faa01
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Net
;
using
System.Net.Sockets
;
using
System.Text
;
...
...
@@ -12,6 +13,8 @@ namespace TFTPServer
public
static
IPEndPoint
iep
;
public
static
List
<
EndPoint
>
clients
;
public
static
int
port
=
69
;
private
static
int
OpCode
=
2
;
private
const
string
FILE_DIR
=
@"C:\temp"
;
/// <summary>
/// Enum for packet types
...
...
@@ -25,7 +28,19 @@ namespace TFTPServer
ERROR
}
private
static
void
ReadRequest
(
byte
[]
buffer
)
enum
ERROR_CODE
{
Not_defined
,
File_not_found
,
Access_violation
,
Disk_full
,
Illegal_TFTP_operation
,
Unknown_transfer_ID
,
File_already_exists
,
No_such_user
}
private
static
void
ReadRequest
(
byte
[]
buffer
,
EndPoint
remote
)
{
string
filename
=
string
.
Empty
;
int
index
=
2
;
...
...
@@ -35,6 +50,37 @@ namespace TFTPServer
index
+=
1
;
}
while
(
buffer
[
index
]
!=
0
);
Console
.
WriteLine
(
$"Read request for file :
{
filename
}
"
);
string
file_path
=
Path
.
Combine
(
FILE_DIR
,
filename
);
if
(!
String
.
IsNullOrEmpty
(
filename
)
&&
File
.
Exists
(
file_path
))
{
SendData
();
}
else
{
string
message
=
"File not found."
;
SendError
(
remote
,
(
int
)
ERROR_CODE
.
File_not_found
,
message
);
}
}
private
static
void
SendData
()
{
// TODO: Send file data
}
private
static
void
SendError
(
EndPoint
remote
,
int
error_code
,
string
message
)
{
byte
[]
messageBytes
=
Encoding
.
ASCII
.
GetBytes
(
message
);
byte
[]
packet
=
new
byte
[
4
+
messageBytes
.
Length
+
1
];
packet
[
0
]
=
(
byte
)
OpCode
;
packet
[
1
]
=
(
byte
)(
OpCode
>>
8
);
packet
[
2
]
=
(
byte
)
error_code
;
packet
[
3
]
=
(
byte
)(
error_code
>>
8
);
int
index
=
4
;
for
(
int
i
=
0
;
i
<
messageBytes
.
Length
;
i
++)
{
packet
[
index
]
=
messageBytes
[
i
];
}
socket
.
SendTo
(
packet
,
remote
);
}
private
static
void
WriteRequest
(
byte
[]
buffer
)
...
...
@@ -85,7 +131,7 @@ namespace TFTPServer
int
opcode
=
buffer
[
0
]
+
buffer
[
1
];
switch
(
opcode
)
{
case
(
int
)
PACKET_TYPE
.
RRQ
:
ReadRequest
(
buffer
);
ReadRequest
(
buffer
,
remote
);
break
;
case
(
int
)
PACKET_TYPE
.
WRQ
:
WriteRequest
(
buffer
);
...
...
@@ -102,29 +148,6 @@ namespace TFTPServer
default
:
break
;
}
String
rec_string
=
Encoding
.
ASCII
.
GetString
(
buffer
,
0
,
received
);
char
[]
delim
=
{
';'
};
String
[]
palat
=
rec_string
.
Split
(
delim
,
2
);
if
(
palat
.
Length
<
2
)
{
Console
.
WriteLine
(
"Kirjoita viesti muodossa nimi;teksti"
);
}
else
{
if
(!
clients
.
Contains
(
remote
))
{
clients
.
Add
(
remote
);
Console
.
WriteLine
(
"Uusi asiakas: [{0}:{1}]"
,
((
IPEndPoint
)
remote
).
Address
,
((
IPEndPoint
)
remote
).
Port
);
}
Console
.
WriteLine
(
"{0}: {1}"
,
palat
[
0
],
palat
[
1
]);
foreach
(
EndPoint
client
in
clients
)
{
//String kaikille = palat[0] + ": " + palat[1];
// lähetä rec_string
socket
.
SendTo
(
Encoding
.
ASCII
.
GetBytes
(
rec_string
),
client
);
}
}
}
Console
.
ReadKey
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment