Quantcast
Viewing all articles
Browse latest Browse all 27078

VS 2010 [RESOLVED] Async Sockets - Client

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"

VB.NET Code:
  1. Imports System.Net.Sockets
  2. Imports System.Net
  3.  
  4. Namespace Sockets
  5.  
  6.     Public Class Client
  7.  
  8. #Region "Private Varibles"
  9.         Private _Client As Socket
  10.         Private _Server As IPAddress
  11.         Private _Port As Integer
  12.         Private _Bytes As Byte()
  13. #End Region
  14.  
  15. #Region "Public Events"
  16.         Public Event ReceiveMessage(ByVal message As String)
  17. #End Region
  18.  
  19. #Region "Public Properties"
  20.  
  21.         Public ReadOnly Property Connected As Boolean
  22.             Get
  23.                 Return _Client.Connected
  24.             End Get
  25.         End Property
  26.  
  27. #End Region
  28.  
  29. #Region "Routines"
  30.  
  31.         Public Sub New(ByVal Server As String, ByVal Port As Integer)
  32.             _Server = IPAddress.Parse(Server)
  33.             _Port = Port
  34.         End Sub
  35.  
  36.         Public Function Connect()
  37.  
  38.             _Client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  39.  
  40.             Dim _Endpoint As IPEndPoint = New IPEndPoint(_Server, _Port)
  41.  
  42.             _Client.BeginConnect(_Endpoint, New AsyncCallback(AddressOf OnConnect), _Client)
  43.  
  44.         End Function
  45.  
  46.         Private Sub OnConnect(ByVal ar As IAsyncResult)
  47.             _Client.EndConnect(ar)
  48.             _Client.BeginReceive(_Bytes, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), _Client)
  49.         End Sub
  50.  
  51.         Private Sub OnReceive(ByVal ar As IAsyncResult)
  52.  
  53.             _Client.EndReceive(ar)
  54.  
  55.             Dim msg As String = System.Text.ASCIIEncoding.ASCII.GetString(_Bytes)
  56.  
  57.             _Client.BeginReceive(_Bytes, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), _Client)
  58.  
  59.             RaiseEvent ReceiveMessage(msg)
  60.  
  61.         End Sub
  62.  
  63.         Public Sub SendMessage(ByVal message As String)
  64.             Dim sendbytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(message)
  65.             _Client.Send(sendbytes)
  66.         End Sub
  67.  
  68.         Public Sub SendBytes(ByVal sendbytes As Byte())
  69.             _Client.Send(sendbytes)
  70.         End Sub
  71.  
  72.         Public Function Disconnect()
  73.             _Client.Disconnect(False)
  74.             Return True
  75.         End Function
  76.  
  77.  
  78. #End Region
  79.  
  80.     End Class
  81.  
  82. 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.

Viewing all articles
Browse latest Browse all 27078

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>