Skip to content
Snippets Groups Projects
Commit 1a7faa01 authored by joalhelk's avatar joalhelk
Browse files

rrq finished

parent c4962ab2
No related branches found
No related tags found
No related merge requests found
......@@ -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>
......
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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment