Hello everyone,
I have a vb.net application that wraps the TCPListener object
in a class. The server connects to the local interface and establishes
itself on port 9900. It then polls for pending connections every 500ms.
I also have a vb6 application that uses the WinSock control at the other end
of the communication tunel. I have to work with vb6 here because it uses
less memory than .NET.
The WinSock control is not able to fully connect to the vb.net TCPListener.
The winsock.state is
constantly 6 (connecting). Thus I can never send a message to the .net
TCPlistener.
What's interesting is that:
a. I can do the reverse and connect to a vb6 WinSock control from vb.net
TCPClient and communicate without any issues.
b. According to debug information in .net the vb.net TCPListener
establishes a connection with the vb6 winsock control, but in vb6 world the
vb6 control thinks the status is "connecting "
Here is the .net Class that functions as the TCP server:
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
Imports System.Net.Sock ets
Imports System.Text
Imports System.Net
Imports Microsoft.Visua lBasic.Strings
Imports ControlChars = Microsoft.Visua lBasic.ControlC hars
Imports PrintMonitor.Da taBaseConnectio n
Public Class SocketServer
' This class will control the WinSock server
' The server will answer client requests, such as request for a list of
project numbers
' default server binding is to the first ethernet interface and port
9900
Private ipAddress As ipAddress =
Dns.Resolve(Dns .GetHostName).A ddressList(0)
Private Const PortNumber As Integer = 9900
' tcp server
Private TCPServer As TcpListener
Private IsListening As Boolean = False
Private PollTimer As New Timer()
' the client that initiates the connection
Private tcpClient As tcpClient
Private tcpSocket As Socket
' networkstream object
Private NetworkStream As NetworkStream
' Client Messages
Private Const ProjectListRequ est As String = "REQUEST:PROJEC T_LIST"
Private Const ExitRequest As String = "EXIT"
'repro database connection
Private db As New PrintMonitor.Da taBaseConnectio n()
Public Sub New()
' initialize the server
Try
TCPServer = New TcpListener(ipA ddress, PortNumber)
Catch e As System.Net.Sock ets.SocketExcep tion
MessageBox.Show (e.Message)
End Try
'initialize timer
PollTimer.Inter val = 500
PollTimer.Enabl ed = True
AddHandler PollTimer.tick, AddressOf PollTimer_Tick
End Sub
'overloaded
Public Sub New(ByVal ipAddress As ipAddress, ByVal PortNumber As
Integer)
' initialize the server
Try
TCPServer = New TcpListener(ipA ddress, PortNumber)
Catch e As System.Net.Sock ets.SocketExcep tion
MessageBox.Show (e.Message)
End Try
'initialize timer
PollTimer.Inter val = 500
PollTimer.Enabl ed = True
AddHandler PollTimer.Tick, AddressOf PollTimer_Tick
End Sub
Public Sub startListening( )
Try
TCPServer.Start ()
Console.WriteLi ne("SERVER>list ening on interface " &
ipAddress.ToStr ing & _
" on port " & PortNumber.ToSt ring & ".....")
IsListening = True
Catch e As System.Net.Sock ets.SocketExcep tion
MessageBox.Show (e.Message)
End Try
' start the timer
PollTimer.Start ()
End Sub
Public Sub stopListening()
If IsListening Then
TCPServer.Stop( )
End If
IsListening = False
End Sub
Private Sub PollTimer_Tick( ByVal sender As Object, ByVal e As
System.EventArg s)
PollTimer.Start ()
' if a conenction is pending, start a new thread and process
connection
If TCPServer.Pendi ng Then
Dim tCheckForConnec tionThread As System.Threadin g.Thread
tCheckForConnec tionThread = New
System.Threadin g.Thread(Addres sOf ProcessConnecti on)
tCheckForConnec tionThread.IsBa ckground = True
tCheckForConnec tionThread.Name = "Checking for Connection <" & _
System.DateTime .Now.ToString & ">"
tCheckForConnec tionThread.Star t()
'CheckForConnec tion()
End If
End Sub
Private Sub ProcessConnecti on()
If TCPServer.Pendi ng Then
Try
tcpSocket = TCPServer.Accep tSocket
Console.WriteLi ne("SERVER>Conn ection accepted.")
' get and convert the message to a string for processing
Dim bReceivedBytes( 1024) As Byte
If tcpSocket.Avail able > bReceivedBytes. Length Then
Console.WriteLi ne("ERROR: Received byte stream larger
than buffer. " & _
"Trancation has occured.")
End If
tcpSocket.Recei ve(bReceivedByt es)
Dim sStreamData As String =
Encoding.ASCII. GetString(bRece ivedBytes)
AnalyzeClientMe ssage(sStreamDa ta)
Catch e As System.Net.Sock ets.SocketExcep tion
MessageBox.Show (e.Message.ToSt ring)
End Try
End If
'clean up and kill the current thread
If Not IsNothing(tcpSo cket) Then
If tcpSocket.Conne cted Then
Console.WriteLi ne("Closing Connection." )
tcpSocket.Close ()
End If
End If
System.Threadin g.Thread.Curren tThread.Abort()
End Sub
Private Sub AnalyzeClientMe ssage(ByVal MessageText As String)
If InStr(MessageTe xt, ProjectListRequ est) Then
SendProjectList ()
ElseIf InStr(MessageTe xt, ExitRequest) Then
Console.WriteLi ne("SERVER>Exi t command received. Closing
connection.")
Else
Console.WriteLi ne("SERVER>Mess age not understood.")
Console.WriteLi ne("SERVER>Rece ived: " & MessageText &
ControlChars.Cr Lf)
End If
End Sub
Private Sub SendProjectList ()
Console.WriteLi ne("SERVER>Proj ect List was requested")
Dim sprojectlist As String = db.GetProjectLi st
sendMessage("RE SPONSE:PROJECT_ LIST:" & ControlChars.Cr Lf &
sprojectlist)
End Sub
Public Sub sendMessage(ByV al MessageText As String)
Try
If tcpSocket.Conne cted Then
Dim SendBytes(1024) As Byte
SendBytes = Encoding.ASCII. GetBytes(Messag eText)
tcpSocket.Send( SendBytes, SendBytes.Lengt h,
SocketFlags.Non e)
End If
Catch e As System.Net.Sock ets.SocketExcep tion
MessageBox.Show (e.Message, "Connection Error", _
MessageBoxButto ns.OK, MessageBoxIcon. Error)
End Try
Console.WriteLi ne("SERVER>: " & MessageText)
End Sub
End Class
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~
An object from this class is initialized in a form for now, and the server
is immediately started.
The vb6 winsock control is initialized as in a menu function follows:
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~
Private Sub mnuInformation_ ProjListUpdate_ Click()
'connect to server and retrieve project list
With frmPopUp.axWins ockClient
.RemoteHost = "192.168.1. 49"
.RemotePort = CLng(9900)
Dim temp As Integer
temp = ServicePointMan ager.DefaultCon nectionLimit
.Connect
Select Case axWinsockClient .State
Case 6
Debug.Print ("connecting... .")
Case 7
Debug.Print ("connected to " & .RemoteHostIP & ":" &
..RemotePort)
End Select
If axWinsockClient .State = 7 Then .SendData ("REQUEST:PROJE CT_LIST")
.Close
End With
End Sub
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~
Can anyone help me with this?
thanx,
Dmitry
I have a vb.net application that wraps the TCPListener object
in a class. The server connects to the local interface and establishes
itself on port 9900. It then polls for pending connections every 500ms.
I also have a vb6 application that uses the WinSock control at the other end
of the communication tunel. I have to work with vb6 here because it uses
less memory than .NET.
The WinSock control is not able to fully connect to the vb.net TCPListener.
The winsock.state is
constantly 6 (connecting). Thus I can never send a message to the .net
TCPlistener.
What's interesting is that:
a. I can do the reverse and connect to a vb6 WinSock control from vb.net
TCPClient and communicate without any issues.
b. According to debug information in .net the vb.net TCPListener
establishes a connection with the vb6 winsock control, but in vb6 world the
vb6 control thinks the status is "connecting "
Here is the .net Class that functions as the TCP server:
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
Imports System.Net.Sock ets
Imports System.Text
Imports System.Net
Imports Microsoft.Visua lBasic.Strings
Imports ControlChars = Microsoft.Visua lBasic.ControlC hars
Imports PrintMonitor.Da taBaseConnectio n
Public Class SocketServer
' This class will control the WinSock server
' The server will answer client requests, such as request for a list of
project numbers
' default server binding is to the first ethernet interface and port
9900
Private ipAddress As ipAddress =
Dns.Resolve(Dns .GetHostName).A ddressList(0)
Private Const PortNumber As Integer = 9900
' tcp server
Private TCPServer As TcpListener
Private IsListening As Boolean = False
Private PollTimer As New Timer()
' the client that initiates the connection
Private tcpClient As tcpClient
Private tcpSocket As Socket
' networkstream object
Private NetworkStream As NetworkStream
' Client Messages
Private Const ProjectListRequ est As String = "REQUEST:PROJEC T_LIST"
Private Const ExitRequest As String = "EXIT"
'repro database connection
Private db As New PrintMonitor.Da taBaseConnectio n()
Public Sub New()
' initialize the server
Try
TCPServer = New TcpListener(ipA ddress, PortNumber)
Catch e As System.Net.Sock ets.SocketExcep tion
MessageBox.Show (e.Message)
End Try
'initialize timer
PollTimer.Inter val = 500
PollTimer.Enabl ed = True
AddHandler PollTimer.tick, AddressOf PollTimer_Tick
End Sub
'overloaded
Public Sub New(ByVal ipAddress As ipAddress, ByVal PortNumber As
Integer)
' initialize the server
Try
TCPServer = New TcpListener(ipA ddress, PortNumber)
Catch e As System.Net.Sock ets.SocketExcep tion
MessageBox.Show (e.Message)
End Try
'initialize timer
PollTimer.Inter val = 500
PollTimer.Enabl ed = True
AddHandler PollTimer.Tick, AddressOf PollTimer_Tick
End Sub
Public Sub startListening( )
Try
TCPServer.Start ()
Console.WriteLi ne("SERVER>list ening on interface " &
ipAddress.ToStr ing & _
" on port " & PortNumber.ToSt ring & ".....")
IsListening = True
Catch e As System.Net.Sock ets.SocketExcep tion
MessageBox.Show (e.Message)
End Try
' start the timer
PollTimer.Start ()
End Sub
Public Sub stopListening()
If IsListening Then
TCPServer.Stop( )
End If
IsListening = False
End Sub
Private Sub PollTimer_Tick( ByVal sender As Object, ByVal e As
System.EventArg s)
PollTimer.Start ()
' if a conenction is pending, start a new thread and process
connection
If TCPServer.Pendi ng Then
Dim tCheckForConnec tionThread As System.Threadin g.Thread
tCheckForConnec tionThread = New
System.Threadin g.Thread(Addres sOf ProcessConnecti on)
tCheckForConnec tionThread.IsBa ckground = True
tCheckForConnec tionThread.Name = "Checking for Connection <" & _
System.DateTime .Now.ToString & ">"
tCheckForConnec tionThread.Star t()
'CheckForConnec tion()
End If
End Sub
Private Sub ProcessConnecti on()
If TCPServer.Pendi ng Then
Try
tcpSocket = TCPServer.Accep tSocket
Console.WriteLi ne("SERVER>Conn ection accepted.")
' get and convert the message to a string for processing
Dim bReceivedBytes( 1024) As Byte
If tcpSocket.Avail able > bReceivedBytes. Length Then
Console.WriteLi ne("ERROR: Received byte stream larger
than buffer. " & _
"Trancation has occured.")
End If
tcpSocket.Recei ve(bReceivedByt es)
Dim sStreamData As String =
Encoding.ASCII. GetString(bRece ivedBytes)
AnalyzeClientMe ssage(sStreamDa ta)
Catch e As System.Net.Sock ets.SocketExcep tion
MessageBox.Show (e.Message.ToSt ring)
End Try
End If
'clean up and kill the current thread
If Not IsNothing(tcpSo cket) Then
If tcpSocket.Conne cted Then
Console.WriteLi ne("Closing Connection." )
tcpSocket.Close ()
End If
End If
System.Threadin g.Thread.Curren tThread.Abort()
End Sub
Private Sub AnalyzeClientMe ssage(ByVal MessageText As String)
If InStr(MessageTe xt, ProjectListRequ est) Then
SendProjectList ()
ElseIf InStr(MessageTe xt, ExitRequest) Then
Console.WriteLi ne("SERVER>Exi t command received. Closing
connection.")
Else
Console.WriteLi ne("SERVER>Mess age not understood.")
Console.WriteLi ne("SERVER>Rece ived: " & MessageText &
ControlChars.Cr Lf)
End If
End Sub
Private Sub SendProjectList ()
Console.WriteLi ne("SERVER>Proj ect List was requested")
Dim sprojectlist As String = db.GetProjectLi st
sendMessage("RE SPONSE:PROJECT_ LIST:" & ControlChars.Cr Lf &
sprojectlist)
End Sub
Public Sub sendMessage(ByV al MessageText As String)
Try
If tcpSocket.Conne cted Then
Dim SendBytes(1024) As Byte
SendBytes = Encoding.ASCII. GetBytes(Messag eText)
tcpSocket.Send( SendBytes, SendBytes.Lengt h,
SocketFlags.Non e)
End If
Catch e As System.Net.Sock ets.SocketExcep tion
MessageBox.Show (e.Message, "Connection Error", _
MessageBoxButto ns.OK, MessageBoxIcon. Error)
End Try
Console.WriteLi ne("SERVER>: " & MessageText)
End Sub
End Class
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~
An object from this class is initialized in a form for now, and the server
is immediately started.
The vb6 winsock control is initialized as in a menu function follows:
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~
Private Sub mnuInformation_ ProjListUpdate_ Click()
'connect to server and retrieve project list
With frmPopUp.axWins ockClient
.RemoteHost = "192.168.1. 49"
.RemotePort = CLng(9900)
Dim temp As Integer
temp = ServicePointMan ager.DefaultCon nectionLimit
.Connect
Select Case axWinsockClient .State
Case 6
Debug.Print ("connecting... .")
Case 7
Debug.Print ("connected to " & .RemoteHostIP & ":" &
..RemotePort)
End Select
If axWinsockClient .State = 7 Then .SendData ("REQUEST:PROJE CT_LIST")
.Close
End With
End Sub
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~
Can anyone help me with this?
thanx,
Dmitry
Comment