Conversation
yurii-litvinov
left a comment
There was a problem hiding this comment.
Не работает — не вызываете Start для _listener, Talk думает, что он всегда сервер. И тестов нет.
| class Program | ||
| { | ||
| public static async Task Main(string[] args) |
There was a problem hiding this comment.
Это в современном .NET можно не писать. Компилятор сам поймёт по телу программы, что Main надо сгенерировать асинхронным
| { | ||
| public static async Task Main(string[] args) | ||
| { | ||
|
|
There was a problem hiding this comment.
После { пустая строка не ставится
|
|
||
| if (args == null || args.Length > 2 || args.Length == 0) | ||
| { | ||
| throw new ArgumentException(); |
There was a problem hiding this comment.
Main — это место, где ловят исключения, а не кидают (тут надо вывести дружественное к пользователю сообщение об ошибке, а не стек вызовов из одного фрейма и без пояснений, какой именно аргумент не понравился). Не пишите throw в Main.
| var port = 0; | ||
| var correct = int.TryParse(args[0], out port); |
There was a problem hiding this comment.
| var port = 0; | |
| var correct = int.TryParse(args[0], out port); | |
| var correct = int.TryParse(args[0], out int port); |
| } | ||
| if (args.Length == 1) | ||
| { | ||
| var server = new ServerAndClient(8888); |
There was a problem hiding this comment.
Значение аргумента в этом случае игнорируется и сервер всё равно запускается на порту 8888?
| { | ||
| _port = port; | ||
| _ipAddress = ipAdress; | ||
| _client = new TcpClient(ipAdress, port); |
There was a problem hiding this comment.
А я бы не смешивал понятия сервера и клиента. Потому что у одного есть TcpClient, у другого TcpListener, и они никогда не могут быть не null вместе.
| { | ||
| if (_socket == null || stream == null) | ||
| { | ||
| throw new ArgumentNullException(); |
There was a problem hiding this comment.
Но Talk() не принимает аргументов :) Тут правильнее InvalidOperationExpression
| var writer = new StreamWriter(stream); | ||
| var reader = new StreamReader(stream); | ||
| var data = reader.ReadToEnd(); | ||
| if (string.Compare(data, "exit") == 0) |
There was a problem hiding this comment.
Хм, в таком виде это то же самое, что и data == "exit"
| _socket.Shutdown(SocketShutdown.Both); | ||
| if (_client != null) | ||
| { | ||
| _client.Close(); |
There was a problem hiding this comment.
Это был бы надёжный способ закрыть соединение, не реализуя никакой IDisposable, если бы не было исключений.
| @@ -0,0 +1,70 @@ | |||
| using System.Net; | |||
| using System.Net.Sockets; | |||
| using System.Net.WebSockets; | |||
No description provided.