Skip to content
Open
Show file tree
Hide file tree
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
37 changes: 36 additions & 1 deletion SimpleTCP.Tests/CommTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleTCP.Tests
{
Expand Down Expand Up @@ -69,7 +70,41 @@ public void SimpleCommTest()

Assert.IsTrue(true);



}


[TestMethod]
public void MultipleClientsTransmittingToSameServerTest()
{
SimpleTcpServer server = new SimpleTcpServer().Start(8911);
server.Delimiter = Encoding.UTF8.GetBytes("0")[0];
SimpleTcpClient client1 = new SimpleTcpClient().Connect(server.GetListeningIPs().FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString(), 8911);
SimpleTcpClient client2 = new SimpleTcpClient().Connect(server.GetListeningIPs().FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString(), 8911);

System.Threading.Thread.Sleep(1000);
if (server.ConnectedClientsCount < 2)
{
Assert.Fail("Server did not register 2 connected clients");
}

string dataRecievedByServer = "";
server.DelimiterDataReceived += (sender, msg) =>
{
dataRecievedByServer += msg.MessageString;
};

client1.Write("1111");
System.Threading.Thread.Sleep(100);
client2.Write("2222");
System.Threading.Thread.Sleep(100);
client1.Write("0");

System.Threading.Thread.Sleep(1000);

Assert.AreEqual("1111", dataRecievedByServer);

server.Stop();
}
}
}
21 changes: 14 additions & 7 deletions SimpleTCP/Server/ServerListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class ServerListener
private List<TcpClient> _connectedClients = new List<TcpClient>();
private List<TcpClient> _disconnectedClients = new List<TcpClient>();
private SimpleTcpServer _parent = null;
private List<byte> _queuedMsg = new List<byte>();
private Dictionary<string, List<byte>> _clientBuffers = new Dictionary<string, List<byte>>();
private byte _delimiter = 0x13;
private Thread _rxThread = null;

Expand Down Expand Up @@ -113,12 +113,12 @@ private void RunLoopStep()

foreach (var c in _connectedClients)
{
if ( IsSocketConnected(c.Client) == false)

if (IsSocketConnected(c.Client) == false)
{
_disconnectedClients.Add(c);
}

int bytesAvailable = c.Available;
if (bytesAvailable == 0)
{
Expand All @@ -134,14 +134,21 @@ private void RunLoopStep()
c.Client.Receive(nextByte, 0, 1, SocketFlags.None);
bytesReceived.AddRange(nextByte);

string clientKey = c.Client.RemoteEndPoint.ToString();
if (!_clientBuffers.ContainsKey(clientKey))
{
_clientBuffers.Add(clientKey, new List<byte>());
}

List<byte> clientBuffer = _clientBuffers[clientKey];
if (nextByte[0] == _delimiter)
{
byte[] msg = _queuedMsg.ToArray();
_queuedMsg.Clear();
byte[] msg = clientBuffer.ToArray();
clientBuffer.Clear();
_parent.NotifyDelimiterMessageRx(this, c, msg);
} else
{
_queuedMsg.AddRange(nextByte);
clientBuffer.AddRange(nextByte);
}
}

Expand Down