Wrox Programmer Forums
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 22nd, 2007, 10:47 AM
Registered User
 
Join Date: Nov 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help Winsock

Hi every body
I am new in vb2005, and need some help.
In order ro connect two applications with each other,I used to use Winsock control in vb6.
So my questions are:
1-Can I use Winsock in vb2005?and how?
2-what is the class responsible for making connection between two applications?and how I can use it?(I found a lot of information in MSDN about sockets class but didn't understand a lot f things?

Thanks in advance:)

 
Old November 27th, 2007, 04:58 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Perhaps it would be more fruitful to have some of the things you found at MSDN explained...
 
Old January 17th, 2008, 07:14 PM
Registered User
 
Join Date: Jan 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

VB6 had a winsock control that handled everything for you. In .Net you have to do everything yourself.

If you are using UDP it is fairly simple. If you are using TCP it is a little more complicated.

In both cases, for 2-way communication you need to create a Listener thread. Its code that runs on its own thread and simply listens for new data. Sending data is simple. See below for UDP code. Sorry about the formattiing. Local port is the port that recieves data. Remote port is the port you send data to. If two applications on the same machine, local and remote ports should be different, eg, 4000 and 4001. When connecting to a remote pc, they can use the same port number.


Option Explicit On
Option Strict On
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text.Encoding
Public Class UDP
    ' ----------------------------------------------------------------------------------
    ' Module Name: clsUDP
    ' Comments:
    ' Author: Chris Van Duin - BluescopeSteel Lab Services.
    ' Created: August 2004.
    ' Revisions: 13th January, 2005 - Chris Van Duin
    ' - Converted to VB.NET 2005
    ' 04/01/2007 - C. Van Duin
    ' - Added LocalPort assignment to constructor.
    ' Local variables used:
    ' m_Client: System.Net.Sockets.UDPClient
    ' m_LocalPort: Local winsocket UDP is bound to. Default value is 4000.
    ' m_RemotePort: Remote winsocket UDP is bound to. Default value is 4000.
    ' m_RemoteHost: Remote Host computer to connect to.
    ' m_Thread: Thread.
    ' m_State: SocketStates
    ' ----------------------------------------------------------------------------------

#Region "Local variables"

    Private m_Client As UdpClient
    Private m_LocalPort As Integer = 4000
    Private m_RemotePort As Integer = 4000
    Private m_RemoteHost As String
    Private m_Thread As Thread = New Thread(AddressOf Listen)
    Private m_State As SocketStates

    Public Enum SocketStates As Integer
        CLOSED = 0
        OPENED = 1
    End Enum

    Public Event DataArrival(ByVal strData As String)

#End Region

#Region "Constructors/Destructors"

    ' ----------------------------------------------------------------------------------
    ' Name: New
    ' Comments: Creates a new instance of this class.
    ' Written By: Chris Van Duin - 21st August, 2004.
    ' Modifications: 04/01/2007 - C. Van Duin
    ' - Added LocalPort assignment to constructor.
    ' Parameters:
    ' RemoteHost: Remote Host computer to connect to.
    ' RemotePort: Remote winsocket UDP is bound to.
    ' LocalPort: Local winsocket UDP is bound to.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' ----------------------------------------------------------------------------------
    Public Sub New(ByVal RemoteHost As String, _
                   ByVal RemotePort As Integer, _
                   ByVal LocalPort As Integer)

        Me.m_RemoteHost = RemoteHost
        Me.m_RemotePort = RemotePort
        Me.m_LocalPort = LocalPort

    End Sub

    ' ------------------------------------------------------------------------------------
    ' Name: Dispose
    ' Comments:
    ' Written By: Chris Van Duin - 11th May, 2007.
    ' Modifications: None.
    ' Parameters: None.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' ------------------------------------------------------------------------------------
    Public Sub Dispose()

        Call CloseSocket()

    End Sub

#End Region

#Region "Properties"

    ' ----------------------------------------------------------------------------------
    ' Name: LocalPort
    ' Comments: Syntax: Debug.Write X.LocalPort
    ' Syntax: X.LocalPort = 4000
    ' Written By: Chris Van Duin - 21st August, 2004.
    ' Modifications: None.
    ' Parameters: Value.
    ' Returns: LocalPort
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' ----------------------------------------------------------------------------------
    Public Property LocalPort() As Integer

        Get
            Return m_LocalPort
        End Get

        Set(ByVal Value As Integer)
            m_LocalPort = Value
        End Set

    End Property

    ' ----------------------------------------------------------------------------------
    ' Name: RemotePort
    ' Comments: Syntax: Debug.Write X.RemotePort
    ' Syntax: X.RemotePort = 4000
    ' Written By: Chris Van Duin - 21st August, 2004.
    ' Modifications: None.
    ' Parameters: Value.
    ' Returns: RemotePort
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' ----------------------------------------------------------------------------------
    Public Property RemotePort() As Integer

        Get
            Return m_RemotePort
        End Get

        Set(ByVal Value As Integer)
            m_RemotePort = Value
        End Set

    End Property

    ' ----------------------------------------------------------------------------------
    ' Name: RemoteHostName
    ' Comments: Syntax: Debug.Write X.RemoteHostName
    ' Syntax: X.RemoteHostName = "Local Host"
    ' Written By: Chris Van Duin - 21st August, 2004.
    ' Modifications: None.
    ' Parameters: Value.
    ' Returns: RemoteHostName
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' ----------------------------------------------------------------------------------
    Public Property RemoteHostName() As String

        Get
            Return m_RemoteHost
        End Get

        Set(ByVal Value As String)
            m_RemoteHost = Value
        End Set

    End Property

    ' ----------------------------------------------------------------------------------
    ' Name: State
    ' Comments: Syntax: Debug.Write X.State
    ' Written By: Chris Van Duin - 21st August, 2004.
    ' Modifications: None.
    ' Parameters: None.
    ' Returns: SocketStates
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' ----------------------------------------------------------------------------------
    Public ReadOnly Property State() As SocketStates

        Get
            Return m_State
        End Get

    End Property

#End Region

#Region "Methods"

    ' ----------------------------------------------------------------------------------
    ' Name: CloseSocket
    ' Comments:
    ' Written By: Chris Van Duin - 21st August, 2004.
    ' Modifications: 04/01/2007 - C. Van Duin
    ' - Renamed from StopSocket.
    ' Parameters: None.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' ----------------------------------------------------------------------------------
    Public Sub CloseSocket()

        If m_State = SocketStates.OPENED Then
            m_State = SocketStates.CLOSED

            ' Send an empty message to the listener thread so that it can
            ' check the SocketState and act accordingly.
            Send("", "LocalHost", m_LocalPort)
        End If

    End Sub

    ' ----------------------------------------------------------------------------------
    ' Name: Listen
    ' Comments:
    ' Written By: Chris Van Duin - 21st August, 2004.
    ' Modifications: 04/01/2007 - C. Van Duin
    ' - Renamed from GetData.
    ' Parameters: None.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used:
    ' GetSocket: System.Net.Sockets.UDPClient
    ' RemoteComputer: System.Net.IPEndPoint
    ' myByte():
    ' msg: Message string being recieved
    ' ----------------------------------------------------------------------------------
    Private Sub Listen()

        Dim GetSocket As UdpClient
        Dim RemoteComputer As System.Net.IPEndPoint
        Dim myByte() As Byte
        Dim msg As String

        Try
            GetSocket = New UdpClient(m_LocalPort)
            RemoteComputer = New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
            Do While m_State = SocketStates.OPENED
                myByte = GetSocket.Receive(RemoteComputer)
                msg = ASCII.GetString(myByte)
                RaiseEvent DataArrival(msg)
            Loop
        Catch
        End Try

    End Sub


    ' ----------------------------------------------------------------------------------
    ' Name: OpenSocket
    ' Comments: Open socket for listening. Create Listener.
    ' Written By: Chris Van Duin - 4th January, 2007.
    ' Modifications: None.
    ' Parameters: None.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' ----------------------------------------------------------------------------------
    Public Sub OpenSocket()

        If m_State = SocketStates.CLOSED Then
            m_State = SocketStates.OPENED
            m_Thread.Start()
        End If

    End Sub

    ' ----------------------------------------------------------------------------------
    ' Name: OpenSocket
    ' Comments:
    ' Written By: Chris Van Duin - 21st August, 2004.
    ' Modifications: 04/01/2007 - C. Van Duin
    ' - Renamed from Bind.
    ' Parameters:
    ' LocalPort: Local winsocket UDP is bound to.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' ----------------------------------------------------------------------------------
    Public Sub OpenSocket(ByVal LocalPort As Integer)

        Me.LocalPort = LocalPort
        Call OpenSocket()

    End Sub

    ' ----------------------------------------------------------------------------------
    ' Name: Send
    ' Comments:
    ' Written By: Chris Van Duin - 21st August, 2004.
    ' Modifications: None.
    ' Parameters:
    ' Msg:
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' ----------------------------------------------------------------------------------
    Public Sub Send(ByVal Msg As String)

        Call Send(Msg, RemoteHostName, RemotePort)

    End Sub

    ' ----------------------------------------------------------------------------------
    ' Name: Send
    ' Comments: Send UDP message. Create a client if one does not already
    ' exist.
    ' Written By: Chris Van Duin - 21st August, 2004.
    ' Modifications: None.
    ' Parameters:
    ' Msg:
    ' RemoteHost: Remote Host computer to connect to.
    ' RemotePort: Remote winsocket UDP is bound to.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used:
    ' myByte(): Byte array.
    ' ----------------------------------------------------------------------------------
    Public Sub Send(ByVal Msg As String, ByVal RemoteHost As String, _
        ByVal RemotePort As Integer)

        Dim myByte() As Byte

        Try
            Msg = Dns.GetHostName & ":" & Msg
            myByte = ASCII.GetBytes(Msg)
            If m_Client Is Nothing Then
                m_Client = New UdpClient
            End If
            m_Client.Send(myByte, myByte.Length, RemoteHost, RemotePort)
        Catch ex As System.Net.Sockets.SocketException
            Throw New Exception("RemoteHost(" & RemoteHost & ") either not available " & _
                "or RemotePort(" & RemotePort & ") specification invalid")
        End Try

    End Sub

#End Region

End Class





Similar Threads
Thread Thread Starter Forum Replies Last Post
Winsock control bug ratheeshpkr VB Components 3 May 29th, 2007 04:46 AM
Winsock ShadowSax Pro VB 6 0 August 23rd, 2006 10:07 PM
How to send ISO8583 message with winsock octoni Pro VB 6 2 July 8th, 2006 12:18 PM
How to Handle a Winsock Control in better way tinku Beginning VB 6 0 March 8th, 2004 01:32 PM
Winsock error DriesNeyrinck Pro VB.NET 2002/2003 0 July 10th, 2003 08:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.