473,499 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Winsock Connection Problem

108 New Member
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?
Dec 3 '07 #1
4 1657
debasisdas
8,127 Recognized Expert Expert
Kindly post the code for reference of our experts.
Dec 3 '07 #2
romcab
108 New Member
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.
Dec 11 '07 #3
romcab
108 New Member
Kindly post the code for reference of our experts.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. 'hold the IP Address
  4. Dim ipAdd As String
  5. Dim message As String
  6.  
  7. Private Declare Sub Sleep Lib "Kernel32" (ByVal millisec As Long)
  8.  
  9.  
  10.  
  11. 'read and send the data
  12. Private Sub Command1_Click()
  13.  
  14.  
  15.     'Call ReadOnly(MyDlg.FileName)
  16.     'Call ReadFile(MyDlg.FileName)
  17.     Exit Sub
  18.  
  19. End Sub
  20.  
  21. Private Sub Command2_Click()
  22.  
  23.     Call MyConnect_Click
  24. End Sub
  25.  
  26. 'Debug
  27. Private Sub MyConnect_Click()
  28.  
  29.     'MySock.RemoteHost = ipAdd
  30.     MySock.RemoteHost = "192.168.20.65"
  31.     MySock.RemotePort = 4003
  32.     MySock.Protocol = sckTCPProtocol
  33.  
  34.     If Not MySock.State = sckConnected Then
  35.         MySock.Connect
  36.     End If
  37.  
  38.     Exit Sub
  39.  
  40. End Sub
  41.  
  42. Private Sub Send_Click()
  43.  
  44.     Do
  45.     DoEvents
  46.     'if connected
  47.     If MySock.State = sckConnected Then
  48.  
  49.         'Debug
  50.         MsgBox ("sending...")
  51.         MySock.SendData message
  52.         Sleep (70)
  53.  
  54.         'Debug
  55.         'MsgBox (message)
  56.         'Exit Sub
  57.  
  58.     End If
  59.  
  60.     If MySock.State = 9 Then
  61.         MsgBox ("No Connection")
  62.         Unload Me
  63.     End If
  64.  
  65.     Loop Until MySock.State = 7 Or MySock.State = 9
  66.  
  67.     Exit Sub
  68.  
  69. End Sub
  70.  
  71. Private Sub Close_Click()
  72.  
  73.     If MySock.State = sckConnected Then
  74.         MySock.Close
  75.     End If
  76.  
  77.     Exit Sub
  78.  
  79. End Sub
  80.  
  81. 'Open Menu is click
  82. Private Sub MyOpenMenu_Click()
  83.  
  84.     'open dialog box
  85.     MyDlg.FileName = vbNullString
  86.     MyDlg.Filter = "CSV(*.csv)|*.csv"
  87.     MyDlg.ShowOpen
  88.  
  89.     If Not MyDlg.FileName = "" Then
  90.  
  91.         'close any connection
  92.         If MySock.State = sckConnected Then
  93.             MySock.Close
  94.         End If
  95.  
  96.         'call read file
  97.         Call Command1_Click
  98.  
  99.     End If
  100.  
  101. End Sub
  102.  
  103.  
  104.  
  105. 'Read the file per line
  106. Private Sub ReadFile(ByRef aFile As String)
  107.  
  108.     'declare local var
  109.     Dim fs As New FileSystemObject
  110.     Dim ts As TextStream
  111.     Dim szread As String
  112.  
  113.     Dim aMes As String
  114.     Dim ictr As Integer
  115.  
  116.     ictr = 0
  117.     If fs.FileExists(aFile) Then
  118.  
  119.         'Open file for reading
  120.         Set ts = fs.OpenTextFile(aFile, ForReading, False)
  121.  
  122.         'while not EOF
  123.         Do While Not (ts.AtEndOfStream)
  124.  
  125.             szread = vbNullString
  126.             ipAdd = vbNullString
  127.             message = vbNullString
  128.  
  129.             'read the line
  130.             szread = ts.ReadLine
  131.  
  132.             'skip 1st line
  133.             If ictr = 0 Then GoTo Loopback
  134.  
  135.             'if data is invalid
  136.             If CheckFormat(szread) = False Then GoTo Loopback
  137.  
  138.             'MsgBox (szread)
  139.             'format the text to zprotocol
  140.             Call FormaText(szread)
  141.  
  142.             'disp data read in list
  143.             szread = Replace(szread, ",", "  ")
  144.  
  145.             'add item in textbox
  146.             List2.AddItem (szread)
  147.  
  148.             If Not MySock.State = sckConnected Then
  149.                 'connect to machine
  150.                 Call TryToConnect
  151.             End If
  152.  
  153.             'Send the message
  154.             Call MySend
  155.  
  156. Loopback:
  157.             ictr = ictr + 1
  158.         Loop
  159.  
  160.         'close the file
  161.         ts.Close
  162.         MsgBox ("Finished sending data....")
  163.  
  164.  
  165.     Else
  166.         MsgBox ("File Not Exist")
  167.  
  168.     End If
  169.  
  170.     Set fs = Nothing
  171.     Set ts = Nothing
  172.  
  173. End Sub
  174.  
  175. 'format the text to zprotocol
  176. Private Sub FormaText(ByVal aText As String)
  177.  
  178.     'declare local var
  179.     Dim ArrayText() As String
  180.     Dim stemp As String
  181.  
  182.     'split the text by ","
  183.     ArrayText() = Split(aText, ",")
  184.  
  185.     stemp = ArrayText(3)
  186.  
  187.     'add double quote
  188.     stemp = Chr(&H22) & stemp & Chr(&H22)
  189.  
  190.     'Add newline
  191.     stemp = stemp & vbNewLine
  192.  
  193.     'Global
  194.     'Format the data to send
  195.     message = "UMD_SET" + " " + ArrayText(1) + " " + ArrayText(2) + " " + stemp
  196.  
  197.     'Global
  198.     ipAdd = ArrayText(0)
  199.  
  200. End Sub
  201.  
  202. 'check data
  203. Private Function CheckFormat(ByVal aText As String) As Boolean
  204.  
  205.     Dim ArrayText() As String
  206.  
  207.     'split text
  208.     ArrayText() = Split(aText, ",")
  209.  
  210.     If CheckIP(ArrayText(0)) = False Or CheckPipCmd(ArrayText(1)) = False Or CheckPipCmd(ArrayText(2)) = False Then
  211.         MsgBox ("Invalid data")
  212.         CheckFormat = False
  213.         Exit Function
  214.     End If
  215.  
  216.     CheckFormat = True
  217.  
  218. End Function
  219.  
  220. 'check IP
  221. Private Function CheckIP(ByVal ipText As String) As Boolean
  222.  
  223.     Dim str() As String
  224.     Dim i As Integer
  225.     Dim rtn As Boolean
  226.  
  227.     rtn = False
  228.  
  229.     'split ip by "."
  230.     str() = Split(ipText, ".")
  231.  
  232.     For i = 0 To UBound(str)
  233.  
  234.         If IsNum(str(i)) = False Then
  235.             CheckIP = False
  236.             Exit Function
  237.         End If
  238.  
  239.  
  240.         If (Len(str(i)) > 3 Or CInt(str(i)) > 255) Then
  241.             CheckIP = False
  242.             'MsgBox ("Exceed")
  243.             Exit Function
  244.         End If
  245.  
  246.     Next i
  247.  
  248.     CheckIP = True
  249.  
  250. End Function
  251.  
  252. 'check pip and cmd id
  253. Private Function CheckPipCmd(ByVal aText As String) As Boolean
  254.  
  255.     Dim i As Integer
  256.  
  257.     'if emptry
  258.     If aText = "" Then
  259.         CheckPipCmd = False
  260.         Exit Function
  261.     End If
  262.  
  263.     'check if all digit
  264.     If IsNum(aText) = False Then
  265.         CheckPipCmd = False
  266.         Exit Function
  267.     End If
  268.  
  269.     CheckPipCmd = True
  270.  
  271. End Function
  272.  
  273. 'close socket connection
  274. Private Sub MyClose()
  275.  
  276.     'Call event to close connection
  277.     Call Close_Click
  278.  
  279. End Sub
  280.  
  281. 'connect socket
  282. Private Sub TryToConnect()
  283.  
  284.     'Call method to connect
  285.     Call MyConnect_Click
  286.     Exit Sub
  287.  
  288. End Sub
  289. 'send data
  290. Private Sub MySend()
  291.  
  292.     'Call event to send data
  293.     Call Send_Click
  294.     Exit Sub
  295.  
  296. End Sub
  297.  
  298. 'check if number
  299. Private Function IsNum(ByVal aText As String) As Boolean
  300.  
  301.     Dim i As Integer
  302.     Dim ch As String
  303.  
  304.     For i = 1 To Len(aText)
  305.         ch = Mid$(aText, i, 1)
  306.         'MsgBox (ch)
  307.         If Not (Asc(ch) >= 48 And Asc(ch) <= 57) Then
  308.             'MsgBox (ch)
  309.             IsNum = False
  310.             Exit Function
  311.         End If
  312.     Next i
  313.  
  314.     IsNum = True
  315.  
  316. End Function
  317.  
  318. 'End Sub
  319. 'Exit application
  320. Private Sub MyExitMenu_Click()
  321.  
  322.     If MySock.State = sckConnected Then
  323.         MySock.Close
  324.     End If
  325.     Unload Me
  326.  
  327. End Sub
Dec 11 '07 #4
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?
Dec 16 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1494
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...
3
6906
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...
17
9173
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...
0
1362
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...
1
1724
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...
2
1307
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...
4
2607
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....
2
2616
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 =...
1
5099
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...
0
7134
marktang
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,...
0
7180
Oralloy
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,...
0
7225
jinu1996
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...
1
6901
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...
0
5479
agi2029
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,...
1
4920
isladogs
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...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.