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

retr finished

parent 3e100104
Branches main
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
...@@ -12,6 +12,18 @@ namespace FTPClient ...@@ -12,6 +12,18 @@ namespace FTPClient
private static int port = 21; private static int port = 21;
static void Main(string[] args) static void Main(string[] args)
{
FtpConversation();
TftpConversation();
Console.ReadKey();
}
private static void TftpConversation()
{
TftpClient client = new TftpClient("test", 69);
}
private static void FtpConversation()
{ {
FtpClient client = new FtpClient(username, password, host, port); FtpClient client = new FtpClient(username, password, host, port);
client.Initialize(); client.Initialize();
...@@ -22,7 +34,7 @@ namespace FTPClient ...@@ -22,7 +34,7 @@ namespace FTPClient
client.Pasv(); client.Pasv();
client.Retr(); client.Retr();
client.Quit(); client.Quit();
Console.ReadKey();
} }
} }
} }
...@@ -270,9 +270,8 @@ namespace FTPClient ...@@ -270,9 +270,8 @@ namespace FTPClient
sw.Flush(); sw.Flush();
handleResponse(sr.ReadLine()); handleResponse(sr.ReadLine());
byte[] descriptor = new byte[3]; byte[] buffer = new byte[2048];
byte[] buffer = new byte[1024]; string last = string.Empty;
string file = string.Empty;
// TODO: Change default to break // TODO: Change default to break
// and cases just for statuses that need actions. // and cases just for statuses that need actions.
...@@ -284,13 +283,18 @@ namespace FTPClient ...@@ -284,13 +283,18 @@ namespace FTPClient
case "150": case "150":
while (true) while (true)
{ {
ftpSocket.Receive(descriptor);
ftpSocket.Receive(buffer); ftpSocket.Receive(buffer);
string response = Encoding.UTF8.GetString(buffer); string response = Encoding.UTF8.GetString(buffer);
file += ftpSr.ReadLine(); if(response.Equals(last))
{
handleResponse(sr.ReadLine());
CloseData();
return;
}
if (response != null) if (response != null)
{ {
sw.WriteLine(response); sw.WriteLine(response);
last = response;
} }
else { break; } else { break; }
} }
...@@ -310,6 +314,12 @@ namespace FTPClient ...@@ -310,6 +314,12 @@ namespace FTPClient
} }
private void TearDown() private void TearDown()
{
CloseFtp();
CloseData();
}
private void CloseFtp()
{ {
sw.Close(); sw.Close();
sr.Close(); sr.Close();
...@@ -317,6 +327,14 @@ namespace FTPClient ...@@ -317,6 +327,14 @@ namespace FTPClient
socket.Close(); socket.Close();
} }
private void CloseData()
{
ftpSw.Close();
ftpSr.Close();
ftpNs.Close();
ftpSocket.Close();
}
public void GetDirectory() public void GetDirectory()
{ {
try try
...@@ -338,10 +356,4 @@ namespace FTPClient ...@@ -338,10 +356,4 @@ namespace FTPClient
} }
} }
}
public class Buffer
{
public static byte descriptor;
public static Int16 size;
} }
\ No newline at end of file
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace FTPClient
{
class TftpClient
{
public static Socket socket;
public static IPEndPoint ipEndp;
public static NetworkStream ns;
public static StreamReader sr;
public static StreamWriter sw;
public static string status;
public string host { get; set; }
public int port { get; set; }
public TftpClient(string host, int port)
{
this.host = host;
this.port = port;
}
// Initialize socket and connect to server
public void Initialize()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress[] ips = Dns.GetHostAddresses(host);
IPAddress address;
try
{
address = ips[1];
}
catch (IndexOutOfRangeException)
{
address = ips[0];
}
ipEndp = new IPEndPoint(address, port);
try
{
socket.Connect(ipEndp);
}
catch (Exception e)
{
Console.Write(e);
Console.ReadKey();
}
ns = new NetworkStream(socket);
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
string response = sr.ReadLine();
handleResponse(response);
}
/// <summary>
/// Handles normal response from server, if response has dash after status then
/// ask for new line. Since it indicates multiline response.
/// </summary>
/// <param name="response">Response from server</param>
private void handleResponse(string response)
{
status = response.Substring(0, 3);
bool loop = true;
string resp = String.Empty;
Console.WriteLine(response);
if (response[3] == '-')
{
while (loop)
{
resp = sr.ReadLine();
if (resp[3] == '-')
{
Console.WriteLine(resp);
}
else
{
Console.WriteLine(resp);
loop = false;
}
}
}
}
}
}
No preview for this file type
No preview for this file type
a4e69bfc552dac666e14bfb187fa62f229299f16 c60aa8053be36ecf6792d8db05a562b0df068660
No preview for this file type
No preview for this file type
This diff is collapsed.
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