Hi guys,
Need a little help with my program.
I made a C# project (windows forms) that connects to the VelbusLink in server-mode.
As long as I test the application on the PC running the VelbusLink it works without a problem.
But when I try the program on another PC in the network, most of the packets are received OK, but some packets are misformed.
For example when I run one instance of the application on the PC with VelbusLink and another instance on a remote PC I get this:
Local:
http://img31.imageshack.us/img31/8523/knipsely.png
Remote:
http://img10.imageshack.us/img10/8572/knipsel2.png
I presume this is a buffer issue, this is my code for the socket client:
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
namespace VelbusServer
{
public partial class VelbusConnector : UserControl
{
public VelbusConnector()
{
InitializeComponent();
}
private Socket s;
private byte] receivebuffer = new byte[20];
public delegate void PacketReceivedHandler(object velbusBroker, ReceivedPacketArgs packetInfo);
public event PacketReceivedHandler PacketReceived;
public void Disconnect()
{
s.Disconnect(false);
s.Close();
}
protected void OnPacketReceived(object velbusBroker, ReceivedPacketArgs packetInfo)
{
if (PacketReceived != null)
{
PacketReceived(velbusBroker, packetInfo);
}
}
public void Connect(string server, int port)
{
try
{
IPHostEntry hostEntry = null;
hostEntry = Dns.GetHostEntry(server);
IPAddress resolvedIP = null;
foreach (IPAddress ip in hostEntry.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
resolvedIP = ip;
}
}
IPEndPoint ipe = new IPEndPoint(resolvedIP, port);
s = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
s.Connect(ipe);
s.BeginReceive(receivebuffer, 0, receivebuffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null);
}
catch
{
MessageBox.Show("Kan niet verbinden, is de Server online?");
}
}
private void OnDataReceived(IAsyncResult res)
{
string strPacket = "";
for (int i = 0; i < 15; i++)
{
strPacket = strPacket + " " + receivebuffer*.ToString("X2");
}
if (s != null && s.Connected)
{
s.BeginReceive(receivebuffer, 0, receivebuffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null);
}
ReceivedPacketArgs packet = new ReceivedPacketArgs(receivebuffer);
OnPacketReceived(this, packet);
}
public class ReceivedPacketArgs : EventArgs
{
public ReceivedPacketArgs(byte] packet)
{
this.packet = packet;
}
public readonly byte] packet;
}
public void SendPacket(byte] packet)
{
s.Send(packet);
}
}
}
[/code]
I tried with different buffer sizes but result is the same.
Can someone shed some light on how to correctly receive the packets over a socket connection?
Thanks!*