client and server connect
This commit is contained in:
@@ -12,7 +12,7 @@ namespace Client
|
||||
private NetworkStream stream;
|
||||
private byte[] buffer = new byte[1024];
|
||||
private int bytesReceived;
|
||||
private bool connected = false;
|
||||
private bool connected;
|
||||
|
||||
|
||||
public Client() : this("localhost", 5555)
|
||||
@@ -24,6 +24,7 @@ namespace Client
|
||||
{
|
||||
this.client = new TcpClient();
|
||||
this.bytesReceived = 0;
|
||||
this.connected = false;
|
||||
client.BeginConnect(adress, port, new AsyncCallback(OnConnect), null);
|
||||
}
|
||||
|
||||
@@ -31,6 +32,8 @@ namespace Client
|
||||
{
|
||||
this.client.EndConnect(ar);
|
||||
Console.WriteLine("Verbonden!");
|
||||
|
||||
|
||||
this.stream = this.client.GetStream();
|
||||
|
||||
//TODO File in lezen
|
||||
@@ -44,6 +47,9 @@ namespace Client
|
||||
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
|
||||
|
||||
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null);
|
||||
|
||||
//temp moet eigenlijk een ok bericht ontvangen
|
||||
this.connected = true;
|
||||
}
|
||||
|
||||
private void OnRead(IAsyncResult ar)
|
||||
@@ -91,7 +97,7 @@ namespace Client
|
||||
private void OnWrite(IAsyncResult ar)
|
||||
{
|
||||
this.stream.EndWrite(ar);
|
||||
//stuff idk
|
||||
Console.WriteLine("wrote some stuff");
|
||||
}
|
||||
|
||||
#region interface
|
||||
@@ -117,5 +123,10 @@ namespace Client
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public bool IsConnected()
|
||||
{
|
||||
return this.connected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ namespace Client
|
||||
{
|
||||
byte[] res = new byte[payload.Length + 5];
|
||||
|
||||
Array.Copy(BitConverter.GetBytes(payload.Length), 0, res, 0, 4);
|
||||
Array.Copy(BitConverter.GetBytes(payload.Length + 5), 0, res, 0, 4);
|
||||
res[4] = messageId;
|
||||
Array.Copy(payload, 0, res, 5, payload.Length);
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Hardware;
|
||||
using ProftaakRH;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
@@ -13,6 +15,15 @@ namespace Client
|
||||
|
||||
|
||||
Client client = new Client();
|
||||
|
||||
|
||||
while (!client.IsConnected())
|
||||
{
|
||||
|
||||
}
|
||||
BLEHandler bLEHandler = new BLEHandler(client);
|
||||
|
||||
bLEHandler.Connect();
|
||||
while (true)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Hardware
|
||||
/// <summary>
|
||||
/// <c>BLEHandler</c> class that handles connection and traffic to and from the bike
|
||||
/// </summary>
|
||||
class BLEHandler
|
||||
public class BLEHandler
|
||||
{
|
||||
IDataReceiver dataReceiver;
|
||||
private BLE bleBike;
|
||||
|
||||
114
Server/Client.cs
114
Server/Client.cs
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
@@ -16,6 +17,8 @@ namespace Server
|
||||
private NetworkStream stream;
|
||||
private byte[] buffer = new byte[1024];
|
||||
private byte[] totalBuffer = new byte[1024];
|
||||
private int bytesReceived;
|
||||
|
||||
|
||||
public string Username { get; set; }
|
||||
|
||||
@@ -27,55 +30,100 @@ namespace Server
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||
}
|
||||
|
||||
//private void OnRead(IAsyncResult ar)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// int receivedBytes = stream.EndRead(ar);
|
||||
// }
|
||||
// catch (IOException)
|
||||
// {
|
||||
// communication.Disconnect(this);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// int counter = 0;
|
||||
|
||||
// while (buffer.Length > counter)
|
||||
// {
|
||||
// //Console.WriteLine(buffer.Length);
|
||||
// byte[] lenghtBytes = new byte[4];
|
||||
// Array.Copy(buffer, counter, lenghtBytes, 0, 4);
|
||||
// int length = BitConverter.ToInt32(lenghtBytes);
|
||||
// Console.WriteLine(buffer[5]);
|
||||
// if (length == 0)
|
||||
// {
|
||||
// break;
|
||||
// }
|
||||
// else if(buffer[counter+4]==0x02)
|
||||
// {
|
||||
|
||||
// }
|
||||
// else if(buffer[counter+4]==0x01)
|
||||
// {
|
||||
// byte[] packet = new byte[length];
|
||||
// Console.WriteLine(Encoding.ASCII.GetString(buffer)+" "+length);
|
||||
// Array.Copy(buffer, counter+5, packet, 0, length);
|
||||
// Console.WriteLine(Encoding.ASCII.GetString(packet));
|
||||
// HandleData(Encoding.ASCII.GetString(packet));
|
||||
// }
|
||||
|
||||
// counter += length;
|
||||
// }
|
||||
|
||||
// Console.WriteLine("Done");
|
||||
|
||||
// stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||
//}
|
||||
|
||||
private void OnRead(IAsyncResult ar)
|
||||
{
|
||||
try
|
||||
int receivedBytes = this.stream.EndRead(ar);
|
||||
byte[] lengthBytes = new byte[4];
|
||||
|
||||
Array.Copy(this.buffer, 0, lengthBytes, 0, 4);
|
||||
|
||||
int expectedMessageLength = BitConverter.ToInt32(lengthBytes);
|
||||
|
||||
if (expectedMessageLength > this.buffer.Length)
|
||||
{
|
||||
int receivedBytes = stream.EndRead(ar);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
communication.Disconnect(this);
|
||||
return;
|
||||
throw new OutOfMemoryException("buffer to small");
|
||||
}
|
||||
|
||||
int counter = 0;
|
||||
|
||||
while (buffer.Length > counter)
|
||||
if (expectedMessageLength > this.bytesReceived + receivedBytes)
|
||||
{
|
||||
//Console.WriteLine(buffer.Length);
|
||||
byte[] lenghtBytes = new byte[4];
|
||||
Array.Copy(buffer, counter, lenghtBytes, 0, 4);
|
||||
int length = BitConverter.ToInt32(lenghtBytes);
|
||||
Console.WriteLine(buffer[5]);
|
||||
if (length == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if(buffer[counter+4]==0x02)
|
||||
{
|
||||
//message hasn't completely arrived yet
|
||||
this.bytesReceived += receivedBytes;
|
||||
this.stream.BeginRead(this.buffer, this.bytesReceived, this.buffer.Length - this.bytesReceived, new AsyncCallback(OnRead), null);
|
||||
|
||||
}
|
||||
else if(buffer[counter+4]==0x01)
|
||||
}
|
||||
else
|
||||
{
|
||||
//message completely arrived
|
||||
if (expectedMessageLength != this.bytesReceived + receivedBytes)
|
||||
{
|
||||
byte[] packet = new byte[length];
|
||||
Console.WriteLine(Encoding.ASCII.GetString(buffer)+" "+length);
|
||||
Array.Copy(buffer, counter+5, packet, 0, length);
|
||||
Console.WriteLine("something has gone completely wrong");
|
||||
Console.WriteLine($"expected: {expectedMessageLength} bytesReceive: {bytesReceived} receivedBytes: {receivedBytes}");
|
||||
}
|
||||
else if (buffer[4] == 0x02)
|
||||
{
|
||||
Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(5).ToArray(), 16)}");
|
||||
}
|
||||
else if (buffer[4] == 0x01)
|
||||
{
|
||||
byte[] packet = new byte[expectedMessageLength];
|
||||
Console.WriteLine(Encoding.ASCII.GetString(buffer) + " " + expectedMessageLength);
|
||||
Array.Copy(buffer, 5, packet, 0, expectedMessageLength - 5);
|
||||
Console.WriteLine(Encoding.ASCII.GetString(packet));
|
||||
HandleData(Encoding.ASCII.GetString(packet));
|
||||
}
|
||||
|
||||
counter += length;
|
||||
|
||||
}
|
||||
|
||||
Console.WriteLine("Done");
|
||||
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||
}
|
||||
|
||||
private void HandleData(string packet)
|
||||
{
|
||||
Console.WriteLine("Data "+packet);
|
||||
Console.WriteLine("Data " + packet);
|
||||
JsonConvert.DeserializeObject(packet);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user