Hi guys,
Just want to ask your help about the error I encountered in Winsock connection.
Here is the problem.
When I added a button that has an event to connect to the server, I don't encounter a problem connecting to the server.
When I call a procedures that connect to a server then send data, the value of state is always 6 which means connecting. I try to loop using DoEvents but the value do not change. How can I solve this problem?
4 1657
Kindly post the code for reference of our experts.
Hi guys,
I have problem with socket connection in vb6. I translated a code from vb.net to vb6 and it works perfectly in .net. But to my surprise, vb6 cannot sometimes cannot connect to the remote computer. Besides, I noticed that to make connection works, it should be in an event. Is that right? Hope you can help me.
Kindly post the code for reference of our experts.
-
Option Explicit
-
-
'hold the IP Address
-
Dim ipAdd As String
-
Dim message As String
-
-
Private Declare Sub Sleep Lib "Kernel32" (ByVal millisec As Long)
-
-
-
-
'read and send the data
-
Private Sub Command1_Click()
-
-
-
'Call ReadOnly(MyDlg.FileName)
-
'Call ReadFile(MyDlg.FileName)
-
Exit Sub
-
-
End Sub
-
-
Private Sub Command2_Click()
-
-
Call MyConnect_Click
-
End Sub
-
-
'Debug
-
Private Sub MyConnect_Click()
-
-
'MySock.RemoteHost = ipAdd
-
MySock.RemoteHost = "192.168.20.65"
-
MySock.RemotePort = 4003
-
MySock.Protocol = sckTCPProtocol
-
-
If Not MySock.State = sckConnected Then
-
MySock.Connect
-
End If
-
-
Exit Sub
-
-
End Sub
-
-
Private Sub Send_Click()
-
-
Do
-
DoEvents
-
'if connected
-
If MySock.State = sckConnected Then
-
-
'Debug
-
MsgBox ("sending...")
-
MySock.SendData message
-
Sleep (70)
-
-
'Debug
-
'MsgBox (message)
-
'Exit Sub
-
-
End If
-
-
If MySock.State = 9 Then
-
MsgBox ("No Connection")
-
Unload Me
-
End If
-
-
Loop Until MySock.State = 7 Or MySock.State = 9
-
-
Exit Sub
-
-
End Sub
-
-
Private Sub Close_Click()
-
-
If MySock.State = sckConnected Then
-
MySock.Close
-
End If
-
-
Exit Sub
-
-
End Sub
-
-
'Open Menu is click
-
Private Sub MyOpenMenu_Click()
-
-
'open dialog box
-
MyDlg.FileName = vbNullString
-
MyDlg.Filter = "CSV(*.csv)|*.csv"
-
MyDlg.ShowOpen
-
-
If Not MyDlg.FileName = "" Then
-
-
'close any connection
-
If MySock.State = sckConnected Then
-
MySock.Close
-
End If
-
-
'call read file
-
Call Command1_Click
-
-
End If
-
-
End Sub
-
-
-
-
'Read the file per line
-
Private Sub ReadFile(ByRef aFile As String)
-
-
'declare local var
-
Dim fs As New FileSystemObject
-
Dim ts As TextStream
-
Dim szread As String
-
-
Dim aMes As String
-
Dim ictr As Integer
-
-
ictr = 0
-
If fs.FileExists(aFile) Then
-
-
'Open file for reading
-
Set ts = fs.OpenTextFile(aFile, ForReading, False)
-
-
'while not EOF
-
Do While Not (ts.AtEndOfStream)
-
-
szread = vbNullString
-
ipAdd = vbNullString
-
message = vbNullString
-
-
'read the line
-
szread = ts.ReadLine
-
-
'skip 1st line
-
If ictr = 0 Then GoTo Loopback
-
-
'if data is invalid
-
If CheckFormat(szread) = False Then GoTo Loopback
-
-
'MsgBox (szread)
-
'format the text to zprotocol
-
Call FormaText(szread)
-
-
'disp data read in list
-
szread = Replace(szread, ",", " ")
-
-
'add item in textbox
-
List2.AddItem (szread)
-
-
If Not MySock.State = sckConnected Then
-
'connect to machine
-
Call TryToConnect
-
End If
-
-
'Send the message
-
Call MySend
-
-
Loopback:
-
ictr = ictr + 1
-
Loop
-
-
'close the file
-
ts.Close
-
MsgBox ("Finished sending data....")
-
-
-
Else
-
MsgBox ("File Not Exist")
-
-
End If
-
-
Set fs = Nothing
-
Set ts = Nothing
-
-
End Sub
-
-
'format the text to zprotocol
-
Private Sub FormaText(ByVal aText As String)
-
-
'declare local var
-
Dim ArrayText() As String
-
Dim stemp As String
-
-
'split the text by ","
-
ArrayText() = Split(aText, ",")
-
-
stemp = ArrayText(3)
-
-
'add double quote
-
stemp = Chr(&H22) & stemp & Chr(&H22)
-
-
'Add newline
-
stemp = stemp & vbNewLine
-
-
'Global
-
'Format the data to send
-
message = "UMD_SET" + " " + ArrayText(1) + " " + ArrayText(2) + " " + stemp
-
-
'Global
-
ipAdd = ArrayText(0)
-
-
End Sub
-
-
'check data
-
Private Function CheckFormat(ByVal aText As String) As Boolean
-
-
Dim ArrayText() As String
-
-
'split text
-
ArrayText() = Split(aText, ",")
-
-
If CheckIP(ArrayText(0)) = False Or CheckPipCmd(ArrayText(1)) = False Or CheckPipCmd(ArrayText(2)) = False Then
-
MsgBox ("Invalid data")
-
CheckFormat = False
-
Exit Function
-
End If
-
-
CheckFormat = True
-
-
End Function
-
-
'check IP
-
Private Function CheckIP(ByVal ipText As String) As Boolean
-
-
Dim str() As String
-
Dim i As Integer
-
Dim rtn As Boolean
-
-
rtn = False
-
-
'split ip by "."
-
str() = Split(ipText, ".")
-
-
For i = 0 To UBound(str)
-
-
If IsNum(str(i)) = False Then
-
CheckIP = False
-
Exit Function
-
End If
-
-
-
If (Len(str(i)) > 3 Or CInt(str(i)) > 255) Then
-
CheckIP = False
-
'MsgBox ("Exceed")
-
Exit Function
-
End If
-
-
Next i
-
-
CheckIP = True
-
-
End Function
-
-
'check pip and cmd id
-
Private Function CheckPipCmd(ByVal aText As String) As Boolean
-
-
Dim i As Integer
-
-
'if emptry
-
If aText = "" Then
-
CheckPipCmd = False
-
Exit Function
-
End If
-
-
'check if all digit
-
If IsNum(aText) = False Then
-
CheckPipCmd = False
-
Exit Function
-
End If
-
-
CheckPipCmd = True
-
-
End Function
-
-
'close socket connection
-
Private Sub MyClose()
-
-
'Call event to close connection
-
Call Close_Click
-
-
End Sub
-
-
'connect socket
-
Private Sub TryToConnect()
-
-
'Call method to connect
-
Call MyConnect_Click
-
Exit Sub
-
-
End Sub
-
'send data
-
Private Sub MySend()
-
-
'Call event to send data
-
Call Send_Click
-
Exit Sub
-
-
End Sub
-
-
'check if number
-
Private Function IsNum(ByVal aText As String) As Boolean
-
-
Dim i As Integer
-
Dim ch As String
-
-
For i = 1 To Len(aText)
-
ch = Mid$(aText, i, 1)
-
'MsgBox (ch)
-
If Not (Asc(ch) >= 48 And Asc(ch) <= 57) Then
-
'MsgBox (ch)
-
IsNum = False
-
Exit Function
-
End If
-
Next i
-
-
IsNum = True
-
-
End Function
-
-
'End Sub
-
'Exit application
-
Private Sub MyExitMenu_Click()
-
-
If MySock.State = sckConnected Then
-
MySock.Close
-
End If
-
Unload Me
-
-
End Sub
Dököll 2,364
Recognized Expert Top Contributor
Hi guys,
Just want to ask your help about the error I encountered in Winsock connection.
Here is the problem.
When I added a button that has an event to connect to the server, I don't encounter a problem connecting to the server.
When I call a procedures that connect to a server then send data, the value of state is always 6 which means connecting. I try to loop using DoEvents but the value do not change. How can I solve this problem?
Why do you need the value to change, romcab?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: John Benson |
last post by:
Thanks much for help with the Oracle db connection problem. That, plus some
local assistance getting the sqlnet.ora default domain right, got everything
fixed. Now I'm connected, and have been able...
|
by: minying |
last post by:
Hi everyone!
I am experiencing a problem when trying to establish a TCP connection
between a
program running locally and another program running on the Pocket PC
emulator
provided by the...
|
by: Danieltbt05 |
last post by:
just installed SQL server 2000 and using my client , i can't locate
the server. I used SQL query analyzer to search but no servers were
found. Error message is as below
Server : Msg17,level...
|
by: kgoods |
last post by:
Hope someone has some ideas on this one because I'm fresh out! :)
I have a SQL server on our internal network behind a linux firewall and
Proxy server 2.0 (WinNT 4.0). It is the backend for an...
|
by: Robert Jones |
last post by:
I am trying to set up an ASP site on a Windows 2003 server and am having
problems connecting to the database.
To isolate the problem I created a simple ASP test page which simply created
an ADO...
| |
by: junal |
last post by:
I developed an web application using ASP.NET. i used MS SQL DB 2000. it worked fine in my local host.Kept the backup of DB when i reinstalled my win XP. problem is i insstalled .NET2005 again like b4...
|
by: penkomitev |
last post by:
Hello! First, excuse me if I have any mistakes because Bulgarian is my native language and English is a foreign one. So the problem is:
I have a chat program with to parts: server and client ones....
|
by: dc |
last post by:
I have a baffling connection problem from my C# console app to a sql
server express database.
The console application opens the sql database using the following
code:-
vDataSource = "server =...
|
by: sherifbk |
last post by:
Problem description
==============
- I have 4 clients and 1 server (SQL server)
- 3 clients are Monitoring console 1 client is operation console
- Monitoring console collects some data from...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |