Hi all, Hope you are all enjoying your weekends,
As some of you may know I have never got to grips with sockets. The past while I have been trying to look at lots of examples and figure things out. I can do basic sockets now without a problem but now has time to move onto Async Sockets. I am trying to make a library which is nice and basic which includes connecting, sending, receiving and disconnect using events to send the received messages back to the application. After trying to follow many examples and getting lost I have decided to try and write my own from scratch glancing at examples to try and figure out what certain things are. i have included the entire code of my client library below if someone could please take a glance over and tell me how wrong I am but when I call .Connect from an application I get the error on the BeginReceive line of: "Value cannot be null. Parameter name: buffers"
Note this is very basic for simplicity in getting it up and running error handling and such will be added once its all working.
As some of you may know I have never got to grips with sockets. The past while I have been trying to look at lots of examples and figure things out. I can do basic sockets now without a problem but now has time to move onto Async Sockets. I am trying to make a library which is nice and basic which includes connecting, sending, receiving and disconnect using events to send the received messages back to the application. After trying to follow many examples and getting lost I have decided to try and write my own from scratch glancing at examples to try and figure out what certain things are. i have included the entire code of my client library below if someone could please take a glance over and tell me how wrong I am but when I call .Connect from an application I get the error on the BeginReceive line of: "Value cannot be null. Parameter name: buffers"
VB.NET Code:
Imports System.Net.Sockets Imports System.Net Namespace Sockets Public Class Client #Region "Private Varibles" Private _Client As Socket Private _Server As IPAddress Private _Port As Integer Private _Bytes As Byte() #End Region #Region "Public Events" Public Event ReceiveMessage(ByVal message As String) #End Region #Region "Public Properties" Public ReadOnly Property Connected As Boolean Get Return _Client.Connected End Get End Property #End Region #Region "Routines" Public Sub New(ByVal Server As String, ByVal Port As Integer) _Server = IPAddress.Parse(Server) _Port = Port End Sub Public Function Connect() _Client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Dim _Endpoint As IPEndPoint = New IPEndPoint(_Server, _Port) _Client.BeginConnect(_Endpoint, New AsyncCallback(AddressOf OnConnect), _Client) End Function Private Sub OnConnect(ByVal ar As IAsyncResult) _Client.EndConnect(ar) _Client.BeginReceive(_Bytes, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), _Client) End Sub Private Sub OnReceive(ByVal ar As IAsyncResult) _Client.EndReceive(ar) Dim msg As String = System.Text.ASCIIEncoding.ASCII.GetString(_Bytes) _Client.BeginReceive(_Bytes, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), _Client) RaiseEvent ReceiveMessage(msg) End Sub Public Sub SendMessage(ByVal message As String) Dim sendbytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(message) _Client.Send(sendbytes) End Sub Public Sub SendBytes(ByVal sendbytes As Byte()) _Client.Send(sendbytes) End Sub Public Function Disconnect() _Client.Disconnect(False) Return True End Function #End Region End Class End Namespace
Note this is very basic for simplicity in getting it up and running error handling and such will be added once its all working.