By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,986 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,986 IT Pros & Developers. It's quick & easy.

Winsock Connection Problem

100+
P: 108
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
Share this Question
Share on Google+
4 Replies


debasisdas
Expert 5K+
P: 8,127
Kindly post the code for reference of our experts.
Dec 3 '07 #2

100+
P: 108
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

100+
P: 108
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
Expert 100+
P: 2,364
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

Post your reply

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