Skip to content

Commit

Permalink
add: multi client networking. shit polishing will be soon
Browse files Browse the repository at this point in the history
  • Loading branch information
Konnnst committed Oct 26, 2023
1 parent 403dae2 commit a2cb305
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 28 deletions.
3 changes: 1 addition & 2 deletions HW04FTP/CLIENT/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ internal class MyClient
{
static void Main()
{
var client = new ClientNetwork();
client.SendCommands();
ClientUI.Start();
Console.ReadKey();
}
}
15 changes: 5 additions & 10 deletions HW04FTP/CLIENT/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,19 @@ public ClientNetwork(int port, string address)
_address = address;
}

public void SendCommands()
public string GetServerResponse(string query)
{
using (var client = new TcpClient(_address, _port))
{
var stream = client.GetStream();
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
writer.WriteLine(query);
writer.Flush();

while (true)
{
Console.Write("simple-ftp > ");
var query = Console.ReadLine();
writer.WriteLine(query);
writer.Flush();
var response = reader.ReadLine();

var response = reader.ReadLine();
Console.WriteLine(response);
}
return response;

Check warning on line 45 in HW04FTP/CLIENT/Network.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu

Possible null reference return.

Check warning on line 45 in HW04FTP/CLIENT/Network.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu

Possible null reference return.

Check warning on line 45 in HW04FTP/CLIENT/Network.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Possible null reference return.

Check warning on line 45 in HW04FTP/CLIENT/Network.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Possible null reference return.
}
}
}
18 changes: 18 additions & 0 deletions HW04FTP/CLIENT/UI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Client;

public static class ClientUI
{
public static void Start()
{
var run = true;
var connection = new ClientNetwork();

while (run)
{
Console.Write("ftp_client > ");
var query = Console.ReadLine();
var response = connection.GetServerResponse(query);

Check warning on line 14 in HW04FTP/CLIENT/UI.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu

Possible null reference argument for parameter 'query' in 'string ClientNetwork.GetServerResponse(string query)'.

Check warning on line 14 in HW04FTP/CLIENT/UI.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu

Possible null reference argument for parameter 'query' in 'string ClientNetwork.GetServerResponse(string query)'.

Check warning on line 14 in HW04FTP/CLIENT/UI.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Possible null reference argument for parameter 'query' in 'string ClientNetwork.GetServerResponse(string query)'.

Check warning on line 14 in HW04FTP/CLIENT/UI.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Possible null reference argument for parameter 'query' in 'string ClientNetwork.GetServerResponse(string query)'.
Console.WriteLine(response);
}
}
}
17 changes: 17 additions & 0 deletions HW04FTP/SERVER/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
namespace Server;

/// <summary>
/// Contains commands from server commands set
/// </summary>
public static class Commands
{
/// <summary>
/// -1 if file not found, else
/// size:Long contents: Bytes
/// </summary>
/// <param name="path">Absolute or relative path of target file</param>
/// <returns>Response string</returns>
public static string Get(string path)
{
var response = "";
Expand All @@ -24,6 +33,14 @@ public static string Get(string path)
return response;
}

/// <summary>
/// Response format:
/// -1 if directory not exists else
/// size f name1 ... d nameN
/// f -- if file, d -- if directory
/// </summary>
/// <param name="path"></param>
/// <returns>Response string</returns>
public static string List(string path)
{
var response = "";
Expand Down
33 changes: 17 additions & 16 deletions HW04FTP/SERVER/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@ public ServerNetwork(int port)
{
_port = port;
}
public void RespondCommands()
public async void RespondCommands()
{
var listener = new TcpListener(IPAddress.Any, _port);
listener.Start();

using (var socket = listener.AcceptSocket())
while (true)
{
var stream = new NetworkStream(socket);
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
string response;

while (true)
{
var query = reader.ReadLine();

var socket = await listener.AcceptSocketAsync();

Task.Run(async () => {

Check warning on line 26 in HW04FTP/SERVER/Network.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 26 in HW04FTP/SERVER/Network.cs

View workflow job for this annotation

GitHub Actions / build-Ubuntu

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 26 in HW04FTP/SERVER/Network.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 26 in HW04FTP/SERVER/Network.cs

View workflow job for this annotation

GitHub Actions / build-Windows

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
var stream = new NetworkStream(socket);
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);

var query = await reader.ReadLineAsync();
string response;

if (query == null)
{
response = "Empty query";
Expand All @@ -43,11 +44,11 @@ public void RespondCommands()
response = "Incorrect query format";
}

writer.WriteLine(response);
writer.Flush();
}
}
await writer.WriteAsync(response);
await writer.FlushAsync();

listener.Stop();
socket.Close();
});
}
}
}
15 changes: 15 additions & 0 deletions HW04FTP/SERVER/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

namespace Server;


/// <summary>
/// Parse command in execution ready form
/// </summary>
public static class Parser
{
private static string? _commandType;
private static string? _path;

/// <summary>
/// Returns main command word
/// </summary>
public static string? CommandType => _commandType;
/// <summary>
/// Return path used in command
/// </summary>
public static string? Path => _path;

public static bool Parse(string rawCommand)
Expand Down Expand Up @@ -40,6 +50,11 @@ public static bool Parse(string rawCommand)
return true;
}

/// <summary>
/// Takes path string and checks for incorrect symbols
/// </summary>
/// <param name="path">Path string</param>
/// <returns>Returns true if string correct</returns>
private static bool CheckIfPathValid(string path)
{
char[] restrictedSymbols = { '<', '>', ']', '[', '(', ')', '\'', '"', ' ' };
Expand Down

0 comments on commit a2cb305

Please sign in to comment.