Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use binary reader/writer #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions ServerConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -74,26 +74,20 @@ public void StartClient(TcpClient inClientSocket)

private void Chat()
{
var reader = clientSocket.GetStream();
var reader = new BinaryReader(clientSocket.GetStream());

try
{
while (true)
{
var bytesRead = reader.Read(readBuffer, 0, bufferLength);
var message = reader.ReadString();

using (var memoryStream = new MemoryStream())
{
memoryStream.Write(readBuffer, 0, bytesRead);
var message = System.Text.Encoding.ASCII.GetString(memoryStream.ToArray());

Log.Debug("Server got this message from client: {message}", message);
Log.Debug("Server got this message from client: {message}", message);

foreach (TcpClient client in Program.GetClients())
{
var writer = new BinaryWriter(client.GetStream());
writer.Write($"Server got your message '{message}'");
}
foreach (TcpClient client in Program.GetClients())
{
var writer = new BinaryWriter(client.GetStream());
writer.Write($"Server got your message '{message}'");
}
}
}
5 changes: 2 additions & 3 deletions TcpClientLib/Client.Receiver.cs
Original file line number Diff line number Diff line change
@@ -59,9 +59,8 @@ private void Run()
}
else if (_stream.DataAvailable) // When in Xamarin or WinForms this is never true...
{
var b = new byte[1024];
var bytes = _stream.Read(b, 0, b.Length);
var responseData = Encoding.UTF8.GetString(b, 0, bytes);
var reader = new BinaryReader(_stream);
var responseData = reader.ReadString();

//Don't need this logging since the subscriber to event MainDataReceived logs it out!
//Log.Debug("Got a response from the server: " + responseData);
6 changes: 4 additions & 2 deletions TcpClientLib/Client.Sender.cs
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ public sealed partial class Client
{
private sealed class Sender
{
internal async Task<GenericResult<bool>> SendData(byte[] data)
internal async Task<GenericResult<bool>> SendData(string data)
{
var response = new GenericResult<bool>(true);

@@ -21,10 +21,12 @@ internal async Task<GenericResult<bool>> SendData(byte[] data)
if (_stream.CanWrite)
{
// transition the data to the thread and send it...
await WriteWithTimeout(_stream, data, timeoutMs: 5000);
//await WriteWithTimeout(_stream, data, timeoutMs: 5000);

// Same code as above without timeout
// await _stream.WriteAsync(data, 0, data.Length).ConfigureAwait(false);
var writer = new BinaryWriter(_stream);
writer.Write(data);
}
else
{
2 changes: 1 addition & 1 deletion TcpClientLib/Client.cs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ public async Task<GenericResult<bool>> SendData(string data)
{
try
{
return await _sender.SendData(Encoding.ASCII.GetBytes(data)).ConfigureAwait(false);
return await _sender.SendData(data).ConfigureAwait(false);
}
catch (Exception ex)
{
6 changes: 5 additions & 1 deletion WindowsForms/Form1.cs
Original file line number Diff line number Diff line change
@@ -43,7 +43,11 @@ private void OnClient_MainDataReceived(object sender, DataReceivedArgs e)
{
// This never gets called after client.SendData().
// Probably because of something like https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.invoke?view=netframework-4.7.2
txbResponseFromServer.AppendText(e.Data);
txbResponseFromServer.Invoke((MethodInvoker)delegate ()
{
txbResponseFromServer.AppendText(e.Data + Environment.NewLine);
});

}

private async void btnSendData_Click(object sender, EventArgs e)