Sockets + Classes
Sockets + Classes
Sockets + Classes
Sockets + Classes
Sockets + Classes
Sockets + Classes Sockets + Classes Sockets + Classes Sockets + Classes Sockets + Classes Sockets + Classes Sockets + Classes Sockets + Classes
Sockets + Classes Sockets + Classes
Sockets + Classes
Go Back  Xtreme Visual Basic Talk > > > Sockets + Classes


Reply
 
Thread Tools Display Modes
  #1  
Old 04-16-2005, 07:12 PM
3D-Reality 3D-Reality is offline
Regular
 
Join Date: Apr 2003
Posts: 62
Default Sockets + Classes


:-(

I've got the following console application I wrote around a client class I also wrote. If I close the console with the console close handler on, it crashes the VS.NET Environment. This only happens if a connection is active. I've tried finalize in the class with no luck. It closes before I can pass commands to it.

Anybody? Help?

Code:
Code:
Imports System.Console Module Module1 Public WithEvents A As clsClient = New clsClient Sub Main() SetConsoleCtrlHandler(AddressOf ConsoleCtrlCheck, True) A.Connect("www.google.com", 80) Do Loop Until ShuttingDown = True End Sub Private Sub A_conConnect() Handles A.conConnect Console.WriteLine("Connected") End Sub End Module Module Mod2 Public ShuttingDown As Boolean Delegate Function HandlerRoutine(ByVal dwCtrlType As Integer) As Boolean Public Declare Function SetConsoleCtrlHandler Lib "kernel32" (ByVal pFunc As HandlerRoutine, ByVal badd As Boolean) As Boolean Public Function ConsoleCtrlCheck(ByVal dwctrltype As Integer) As Boolean 'A.Close() WriteLine() WriteLine("Received console signal " & dwctrltype & ", exiting...") ShuttingDown = True End Function End Module

Last edited by herilane; 04-17-2005 at 08:50 AM. Reason: [vb] tags
Reply With Quote
  #2  
Old 04-17-2005, 08:57 AM
Iceplug's Avatar
IceplugSockets + Classes Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

What is clsClient?
Also, when you are using an API or an unknown class that has callbacks, you should always make sure that you properly shutdown the class or API call loop before exiting - otherwise, you'll get disastrous effects (in your case, VS closes, which is typical).
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 04-17-2005, 09:15 AM
3D-Reality 3D-Reality is offline
Regular
 
Join Date: Apr 2003
Posts: 62
Default

clsClient is a class I wrote.

It has a private variable (a Socket object) and another (DataBuffer)

While Socket is active it crashes the app as soon as it is closed and prior to ConsoleCtrlCheck being called.

clsClient has no loops at all, just events.

How do I shutdown the class at the right time?

:-S
Reply With Quote
  #4  
Old 04-17-2005, 10:19 AM
Iceplug's Avatar
IceplugSockets + Classes Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

I guess you would be able to shutdown all of this in the Application_Exit event. I wouldn't be using the Console if it was vital to run a shutdown procedure before the program ends.

I have an example of using Application_Exit in some thread somewhere... .
Internet Cafe Software issues
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #5  
Old 04-17-2005, 10:41 AM
3D-Reality 3D-Reality is offline
Regular
 
Join Date: Apr 2003
Posts: 62
Default

Sockets Code:

Code:
Imports System.Net.Sockets Imports System.Net Public Class clsClient #Region " Helper Functions " Public Function StringToBytes(ByVal Data As String) As Byte() Dim tBytes() As Byte Dim I As Long ReDim tBytes(Len(Data) - 1) For I = 1 To Len(Data) tBytes(I - 1) = Asc(Mid(Data, I, 1)) Next Return tBytes Erase tBytes System.GC.Collect() End Function Public Function BytestoString(ByVal Data As Byte()) As String Dim TMP As String Dim I As Long For I = 0 To UBound(Data) TMP = TMP & Chr(Data(I)) Next Return TMP End Function #End Region #Region " Public Properties " Public Property Connected() As Boolean Get Try Return Socket.Connected Catch ' End Try End Get Set(ByVal Value As Boolean) End Set End Property Public Property GetLocalIP() As String Get Dim IP As IPAddress IP = Dns.Resolve(Dns.GetHostName).AddressList(0) Return IP.ToString IP = Nothing System.GC.Collect() End Get Set(ByVal Value As String) End Set End Property Public Property RemoteHostIP() As String Get Try Dim IPEP As IPEndPoint IPEP = Socket.RemoteEndPoint Return IPEP.Address.ToString IPEP = Nothing System.GC.Collect() Catch ' End Try End Get Set(ByVal Value As String) End Set End Property Public Property RemoteHostPort() As Long Get Try Dim IPEP As IPEndPoint IPEP = Socket.RemoteEndPoint Return IPEP.Port IPEP = Nothing System.GC.Collect() Catch ' End Try End Get Set(ByVal Value As Long) End Set End Property #End Region #Region " Private Properties " Dim DataBuffer() As Byte Dim Socket As Socket #End Region #Region " Events " Event conError(ByVal sReason As String) Event conClose() Event conConnect() Event conDataArrival(ByVal sData As String) #End Region #Region " Data Arrival / Close " Private Sub onDataArrival(ByVal ar As IAsyncResult) Dim bytesTotal As Long bytesTotal = Socket.EndReceive(ar) ar = Nothing System.GC.Collect() If bytesTotal = 0 Then Try Socket.Shutdown(SocketShutdown.Both) Socket.Close() Catch End Try RaiseEvent conClose() Socket = Nothing Erase DataBuffer System.GC.Collect() Else ReDim Preserve DataBuffer(bytesTotal - 1) RaiseEvent conDataArrival(BytestoString(DataBuffer)) Erase DataBuffer ReDim DataBuffer(32767) Socket.BeginReceive(DataBuffer, 0, 32768, 0, AddressOf onDataArrival, 0) System.GC.Collect() End If End Sub #End Region #Region " Connect " Private Sub onConnect(ByVal ar As IAsyncResult) Socket.EndConnect(ar) ar = Nothing System.GC.Collect() If Socket.Connected = False Then RaiseEvent conError("Connection forcefully rejected.") Try Socket.Shutdown(SocketShutdown.Both) Socket.Close() Catch End Try Socket = Nothing System.GC.Collect() Else RaiseEvent conConnect() ReDim DataBuffer(32767) Socket.BeginReceive(DataBuffer, 0, 32768, 0, AddressOf onDataArrival, 0) System.GC.Collect() End If End Sub #End Region #Region " Methods " Public Function Connect(ByVal sIP As String, ByVal vPort As Integer) Dim IP As IPEndPoint = New IPEndPoint(Dns.Resolve(sIP).AddressList(0), vPort) Try If Socket.Connected = True Then Exit Function Catch ' End Try Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Socket.BeginConnect(IP, AddressOf onConnect, 0) IP = Nothing System.GC.Collect() End Function Public Function Close() Try Socket.Shutdown(SocketShutdown.Both) Socket.Close() Socket = Nothing Erase DataBuffer System.GC.Collect() Catch System.GC.Collect() End Try End Function Public Function SendData(ByVal sData As String) As Boolean Try Socket.Send(StringToBytes(sData), Len(sData), SocketFlags.None) SendData = True Catch ' End Try End Function #End Region End Class

Help, Anyone?

Last edited by 3D-Reality; 04-17-2005 at 01:00 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Sockets + Classes
Sockets + Classes
Sockets + Classes Sockets + Classes
Sockets + Classes
Sockets + Classes
Sockets + Classes Sockets + Classes Sockets + Classes Sockets + Classes Sockets + Classes Sockets + Classes Sockets + Classes
Sockets + Classes
Sockets + Classes
 
Sockets + Classes
Sockets + Classes
 
-->