I have searching for a long time in different forums if it's possible to send a text to differents IP's using winsock but I can't find how.
I have try putting two different winsocks in the same form but the message of the second winsock never arrives.
This is my code:
Private Sub Form_Load()
On Error Resume Next
a = bytMsg()
wsck.RemoteHost = strNombreATMMonitor2
wsck.RemotePort = vPuertoATMMonitor2
wsck.Connect
'Conectamos el segundo
Winsock1.RemoteHost = strNombreATMMonitor
Winsock1.RemotePort = vPuertoATMMonitor
Winsock1.Connect
End Sub
Private Sub wsck_Close()
On Error Resume Next
wsck.Close
End
End Sub
Private Sub wsck_Connect()
On Error GoTo Err_wsck_Connect
wsck.SendData a
End_wsck_Connect:
Exit Sub
Err_wsck_Connect:
GrabaLog "wsck_Connect", Err.Description, " " & Err.Number & " Host: " & strNombreATMMonitor
Resume End_wsck_Connect
End Sub
Private Sub wsck_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
'si no es un recordatorio, lo grabo
If bytRecordatorio = 0 Then
GrabaOff strLineaComandos
End If
GrabaLog "wsck_Error", "RemoteHost" & " - " & wsck.RemoteHost & " - RemotePort - " & Trim(wsck.RemotePort), "Error = " & Trim(Number) & " - " & Description
End
End Sub
Private Sub wsck_SendComplete()
On Error GoTo Err_wsck_SendComplete
wsck.Close
'Graba en un log el mensaje de NCR pasado como parametro a ATMSpyNEW + FTP
TrazaLog "ATMMonitor1 - " & strLineaComandos
End_wsck_SendComplete:
End
Err_wsck_SendComplete:
GrabaLog "wsck_SendComplete", Err.Description, "" & Err.Number
Resume End_wsck_SendComplete
End Sub
Private Sub Winsock1_Close()
On Error Resume Next
Winsock1.Close
End
End Sub
Private Sub Winsock1_Connect()
On Error GoTo Err_Winsock1_Connect
Winsock1.SendData a
End_Winsock1_Connect:
Exit Sub
Err_Winsock1_Connect:
GrabaLog "Winsock1_Connect", Err.Description, " " & Err.Number & " Host: " & strNombreATMMonitor
Resume End_Winsock1_Connect
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
'si no es un recordatorio, lo grabo
If bytRecordatorio = 0 Then
GrabaOff strLineaComandos
End If
GrabaLog "Winsock1_Error", "RemoteHost" & " - " & Winsock1.RemoteHost & " - RemotePort - " & Trim(Winsock1.RemotePort), "Error = " & Trim(Number) & " - " & Description
End
End Sub
Does anybody knows how to send a piece of text to, at least, two different Ip's (RemoteHost) using one or two winsock controls?
I created a new VB6 project and added two Winsock controls onto the Form1. Namely Winsock1 and Winsock2. I then added two TextBoxes (for testing only to display input and output from the Winsocks) and set them to allow MultiLines. I added one button (cmdSendData) that will send data to both winsocks. In the Form_Load event, I made these two Winsocks connect to a common TelNet server that I already have access to (you will need to change the IP address to something you can test with). I was able to send data through both Winsocks (even though they are connected to the same machine, they behave separately and could easily change the IP of one of them to another if I knew that I have another IP waiting for a connection request).
I skipped the Error Handling since I am sure you can figure out where to put that.
Private Sub Form_Load()
Me.Winsock1.RemoteHost = "10.11.27.87"
Me.Winsock1.RemotePort = "23"
Me.Winsock1.Connect
Me.Winsock2.RemoteHost = "10.11.27.87"
Me.Winsock2.RemotePort = "23"
Me.Winsock2.Connect
End Sub
Private Sub cmdSendData_Click()
Me.Winsock1.SendData "admin" & vbCrLf
DoEvents 'This is necessary to immediatelly send the data without waiting for
' a certain buffer to be filled first
Me.Winsock2.SendData "admin" & vbCrLf
DoEvents 'same as above
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim txt As String
Me.Winsock1.GetData txt
Me.Text1.Text = Me.Text1.Text & vbCrLf & txt
End Sub
Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
Dim txt As String
Me.Winsock2.GetData txt
Me.Text2.Text = Me.Text2.Text & vbCrLf & txt
End Sub
Related
I want to store string variable from the user and then I want to search that string in files of a specific folder. I want to match the stored string within each file. If I find a match to the string, I want a list box or Combo box to be populated with the name of the files. I am working in Visual basic (VB6).
I tried Some VB.net code like getfiles() but those are not working in VB6.
If you index the target folder you can use Windows Search:
Option Explicit
Private Const INDEXED_FOLDER_PATH As String = _
"C:\Program Files\Windows Kits\10\Include\10.0.16299.0\um"
Private WithEvents CN As ADODB.Connection
Private Sub CN_ExecuteComplete( _
ByVal RecordsAffected As Long, _
ByVal pError As ADODB.Error, _
ByRef adStatus As ADODB.EventStatusEnum, _
ByVal pCommand As ADODB.Command, _
ByVal pRecordset As ADODB.Recordset, _
ByVal pConnection As ADODB.Connection)
If adStatus <> adStatusOK Then
MousePointer = vbDefault
'Sadly this DSO is lazy about providing a Description here, so it
'will often be empty along with an empty Errors collection.
MsgBox "Error " & pError.Number & vbNewLine _
& vbNewLine _
& pError.Description
Else
With pRecordset
If .EOF Then
List1.AddItem "*no hits*"
Else
Do Until .EOF
List1.AddItem .Fields(0).Value
.MoveNext
Loop
End If
.Close
End With
MousePointer = vbDefault
End If
Text1.SetFocus
End Sub
Private Sub Command1_Click()
MousePointer = vbHourglass
List1.Clear
'We are doing an async request here because depending on what we are asking
'for it might take a few seconds:
CN.Execute "SELECT System.ItemNameDisplay" _
& " FROM SystemIndex" _
& " WHERE DIRECTORY='file:" & Replace$(INDEXED_FOLDER_PATH, "\", "/") & "'" _
& " AND FREETEXT('" & Text1.Text & "')", _
, _
adCmdText Or adAsyncExecute
End Sub
Private Sub Form_Load()
Set CN = New ADODB.Connection
CN.Open "Provider=Search.CollatorDSO;Extended Properties='Application=Windows'"
End Sub
Private Sub Form_Unload(Cancel As Integer)
CN.Close
End Sub
More info: Querying the Index with Windows Search SQL Syntax
This is a continuation of my last post: How to read weight from scale using ethernet connection
After creating the TCP connection in vb10 - I am now trying to read the weight from the scale in iFix (vb6). The code below works if I create a breakpoint and step through: strdata takes the weight of the scale (51g at the moment). However, when i simply run the code, I get the error:
Invalid operation at current state 40020.
What i think is happening is something to do with how quickly it reads or trying to read multiple times. Any tips would be great.
TCPclient is referring to winsock, and frmclient refers to my form. The command "S" is the necessary command for the scale to grab the weight value. Thanks!
Public Sub test()
On Error GoTo errHandler
Dim strData As String
frmClient.tcpClient.LocalPort = 0
frmClient.tcpClient.Connect "192.168.0.1", 8000
'Dim i As Integer
' For i = 0 To 2000
' Debug.Print "connection status=" & frmClient.tcpClient.State
' If frmClient.tcpClient.State = 7 Then
' Exit For Next i
frmClient.tcpClient.SendData "S" & vbCrLf
frmClient.tcpClient.GetData strData
MsgBox ("weight =" & strData)
'Exit Sub
errHandler:
MsgBox Err.Description & " " & Err.Number
'Resume Next
End Sub
Use the DataArrival event of your Winsock Control.
So something like:
' ... in your "frmClient" Form ...
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
tcpClient.GetData strData, vbString
MsgBox ("weight =" & strData)
End Sub
*Obviously removing the GetData() call in your original test() method.
Got it working! The code is below. I created a picture sub to initialize the ports/IP at the beginning of the code execution and then to close the connection at the end. I made a timer to automatically read the weight upon stabilization, so the weight can be found by clicking the button, or simply waiting 2 seconds (2000ms). Best of luck and thanks for the help!
Public tcpC As New Winsock
Private Sub CFixPicture_Close()
tcpC.Close
End Sub
Private Sub CFixPicture_Initialize()
tcpC.LocalPort = 0
tcpC.Connect "192.168.0.1", 8000
End Sub
Private Sub CommandButton1_Click()
On Error GoTo errHandler
Dim strData As String
tcpC.SendData "S" & vbCrLf
tcpC.GetData strData
Text4.Caption = "Weight: " & strData
Exit Sub
errHandler:
MsgBox "error:" & Err.Description
End Sub
Private Sub readScale_OnTimeOut(ByVal lTimerId As Long)
Dim strData As String
tcpC.SendData "S" & vbCrLf
tcpC.GetData strData
Text4.Caption = "Weight: " & strData
Exit Sub
End Sub
I need to make an application that needs to retrieve data from an external terminal booking device to my application, with a telnet connection and show it on the txtOutput textbox.
I am pretty new in VB and used some time to learn the basics of the language.
First I created a Standard EXE project and added the Winsock control to the form.
I made a ping-request to the IP address I wanted to have connection too and it works.
Then I want to send a command to the external device. I want the booking-terminal to give me feedback to the txtOutput for me to read.
I made the connection and as much as I can see, I do send my messages to the terminal. But I don't get any responses from it! Nothing from it is viewed on my txtOutput.
How can that be?
Here is my code:
Dim IPAddress As String
Dim PortNum As Integer
Private Sub cmdConnect_Click()
Winsock.Close
Winsock.RemoteHost = txtIpaddress.Text
IPAddress = Winsock.RemoteHost
PortNum = CStr(txtPortnr.Text)
If (Val(PortNum) > 65535) Then
Winsock.RemotePort = (Val(PortNum) - 65535)
PortNum = Winsock.RemotePort
Else
Winsock.RemotePort = Val(PortNum)
PortNum = Winsock.RemotePort
End If
Winsock.Connect
Module1.send_to_buffer ("Attempting connection to: " & IPAddress & ":" & CStr(PortNum))
Call wsock_status
End Sub
Private Sub Winsock_Connect()
Module1.send_to_buffer ("Succeeded connection to: " & IPAddress & ":" & CStr(PortNum))
txtSend.SetFocus
End Sub
Private Sub cmdSend_Click()
Dim strSData As String
Dim message_to_send As String
If (Winsock.State = 0) Then
Module1.send_to_buffer ("You need to connect first!")
txtSend.Text = ""
Else
strSData = txtSend.Text
Winsock.SendData strSData & vbCrLf
message_to_send = txtSend.Text
If (message_to_send <> "") Then
Winsock.SendData message_to_send & vbCrLf
Module1.send_to_buffer_norm (txtSend.Text)
txtSend.Text = ""
txtSend.SetFocus
Else
Module1.send_to_buffer ("Nothing to send!")
txtSend.Text = ""
txtSend.SetFocus
End If
End If
End Sub
Private Sub terminalConnector_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock.GetData strData
If (Len(txtOutput.Text) = 0) Then
txtOutput.Text = strData & vbCrLf
Else
txtOutput.Text = txtOutput.Text & strData & vbCrLf
End If
End Sub
Private Sub cmdDisconnect_Click()
Dim Counter As Long
If (Winsock.State <> 0) Then
Winsock.Close
Call wsock_status
Module1.send_to_buffer ("Connection to " & IPAddress & ":" & CStr(PortNum) & " closed.")
End If
End Sub
Private Sub Winsock_Close()
Module1.send_to_buffer ("Disconnected from: " & IPAddress & ":" & CStr(PortNum))
Winsock.Close
End Sub
and Module1 code:
Public Function send_to_buffer(text_to_display As String)
If (Len(terminalConnector.txtOutput.Text) = 0) Then
terminalConnector.txtOutput.Text = "*** " & text_to_display
Else
terminalConnector.txtOutput.Text = terminalConnector.txtOutput.Text & vbCrLf & "*** " & text_to_display & vbCrLf & vbCrLf
End If
End Function
Public Function send_to_buffer_norm(text_to_input As String)
If (Len(terminalConnector.txtOutput.Text) = 0) Then
terminalConnector.txtOutput.Text = "> " & text_to_input & vbCrLf
Else
terminalConnector.txtOutput.Text = terminalConnector.txtOutput.Text & "> " & text_to_input & vbCrLf
End If
End Function
Thanks in advance
The DataArrival event is named wrongly :
in your code it is :
Private Sub terminalConnector_DataArrival(ByVal bytesTotal As Long)
but it should be the name of your winsock control :
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
You can always select the controlname in the combobox on the left above your code window in the IDE and then select the eventname in the combobox on the right above your code window in the IDE, which will put the outlines of the event code in your code window.
Or you can double-click the control on the design window in the IDE, which will bring you to the code window and put the outlines of an event code in your code window .. you can then select the event you want in the combobox on the right above your code window
A side comment : Winsock might is not the best name for a winsock control, it is best to give it a more unique same, which could be as simple as wskConnection or wskTerminal
When you download MZ-tools you can "Review Source Code" which will show you any procedures and variables that will not be called or used in your program ... this will often give an extra hint to misnamed variables or procedures
You Send routine is wrong :
Private Sub cmdSend_Click()
Dim strSData As String
txtSend.Text = strSData
Winsock.SendData strSData
End Sub
You are showing strSata in txtSend ... while strSData is still an empty string .. after that you send the empty string via the Winsock control
you probably meant :
Private Sub cmdSend_Click()
Dim strSData As String
strSData = txtSend.Text
Winsock.SendData strSData
End Sub
Which reads txtSend.Text into your string variable, and then sends that via the Winsock control
The server probably wants some special character at the end of your string, so dont forget to add that ... usually you have to add a cariage return :
strSData = strSData & vbCr
i have this very simple chat app made in VB6 using winsock, but as you can see it only accept only one connexion, how can i handle multiple users? Thanks!
Private Sub Winsock1_Close()
' Finaliza la conexión
Winsock1.Close
txtLog = txtLog & "*** Desconectado" & vbCrLf
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then
Winsock1.Close ' close
End If
Winsock1.Accept requestID
txtLog = "Cliente conectado. IP : " & _
Winsock1.RemoteHostIP & vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim dat As String
Winsock1.GetData dat, vbString
txtLog = txtLog & "Cliente : " & dat & vbCrLf
End Sub
The solution is to have an array of Winsock objects, and create a new one at runtime. The new object you have created accepts the connection request.
So, in your connection request sub, you would have a new socket:
Dim ConnectionCount as long
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
ConnectionCount=ConnectionCount+1
Load Winsocks(ConnectionCount)
Winsocks(ConnectionCount).Accept(requestID)
txtLog = "Cliente conectado. IP : " & _
Winsocks(ConnectionCount).RemoteHostIP & vbCrLf
End Sub
Edit: Here is a tutorial that may help you better than my code: http://www.devx.com/tips/Tip/5488
It follows the same idea.
I have added a SysInfo control to my form and, to test, am trying to produce a text message when a USB card reader is plugged or unplugged.
Private Sub SysInfo1_ConfigChanged(ByVal OldConfigNum As Long, ByVal NewConfigNum As Long)
ShowText "The system configuration has changed"
End Sub
I have tried a similar test message with the DisplayChanged event and successfully receive that message when the monitor resolution is altered, but I can't for the life of me get the above to work. I was under the impression that the ConfigChanged event should occur when a USB device is added/removed. I am including the SysInfo.ocx with the application and testing on a Vista machine. Thanks
Use the SysInfo_DeviceArrival event to detect insertion of a USB drive. SysInfo_DeviceRemoveComplete fires when it's removed.
There are a lot of events for the SysInfo control, so I wrote a test program to see what events happen. I do this often when working with a control that I haven't used before. The program that exercises the SysInfoControl is included below.
I made this by using the dropdown lists at the top of the VB6 code editing window: Select the SysInfo control in the left one, the select each of the events displayed in the right one. Add a Debug.Print statement to each, run the project, and plug in your USB device.
Also, if you select the SysInfo control in the form designer, then press F1, the MSDN library help should display which includes descriptions of all of the events and how to interpret their parameters. At least, it does on my machine. (I hate it when people say that, but it applies here because the MSDN docs have to be separately installed.)
If that doesn't work, I found the docs online here.
Option Explicit
Private Sub SysInfo_ConfigChangeCancelled()
Debug.Print Now() & ": " & "SysInfo_ConfigChangeCancelled"
End Sub
Private Sub SysInfo_ConfigChanged(ByVal OldConfigNum As Long, ByVal NewConfigNum As Long)
Debug.Print Now() & ": " & "SysInfo_ConfigChanged"
End Sub
Private Sub SysInfo_DeviceArrival(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
Debug.Print Now() & ": " & "SysInfo_DeviceArrival"
End Sub
Private Sub SysInfo_DeviceOtherEvent(ByVal DeviceType As Long, ByVal EventName As String, ByVal DataPointer As Long)
Debug.Print Now() & ": " & "SysInfo_DeviceOtherEvent"
End Sub
Private Sub SysInfo_DeviceQueryRemove(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long, Cancel As Boolean)
Debug.Print Now() & ": " & "SysInfo_DeviceQueryRemove"
End Sub
Private Sub SysInfo_DeviceQueryRemoveFailed(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
Debug.Print Now() & ": " & "SysInfo_DeviceQueryRemoveFailed"
End Sub
Private Sub SysInfo_DeviceRemoveComplete(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
Debug.Print Now() & ": " & "SysInfo_DeviceRemoveComplete"
End Sub
Private Sub SysInfo_DeviceRemovePending(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
Debug.Print Now() & ": " & "SysInfo_DeviceRemovePending"
End Sub
Private Sub SysInfo_DevModeChanged()
Debug.Print Now() & ": " & "SysInfo_DevModeChanged"
End Sub
Private Sub SysInfo_DisplayChanged()
Debug.Print Now() & ": " & "SysInfo_DisplayChanged"
End Sub
Private Sub SysInfo_PowerQuerySuspend(Cancel As Boolean)
Debug.Print Now() & ": " & "SysInfo_PowerQuerySuspend"
End Sub
Private Sub SysInfo_PowerResume()
Debug.Print Now() & ": " & "SysInfo_PowerResume"
End Sub
Private Sub SysInfo_PowerStatusChanged()
Debug.Print Now() & ": " & "SysInfo_PowerStatusChanged"
End Sub
Private Sub SysInfo_PowerSuspend()
Debug.Print Now() & ": " & "SysInfo_PowerSuspend"
End Sub
Private Sub SysInfo_QueryChangeConfig(Cancel As Boolean)
Debug.Print Now() & ": " & "SysInfo_QueryChangeConfig"
End Sub
Private Sub SysInfo_SettingChanged(ByVal Item As Integer)
Debug.Print Now() & ": " & "SysInfo_SettingChanged"
End Sub
Private Sub SysInfo_SysColorsChanged()
Debug.Print Now() & ": " & "SysInfo_SysColorsChanged"
End Sub
Private Sub SysInfo_TimeChanged()
Debug.Print Now() & ": " & "SysInfo_TimeChanged"
End Sub
Why ship an extra control? I would recommend taking a look at Karl Peterson's SysInfo example: http://vb.mvps.org/samples/SysInfo/ The CSysInfoDevice class will watch for device events.