Home > Archive > Visual Basic > October 2004 > winsock
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| I'm using winsock control in VB 6 to write a an application so that the
client can send string to the server and receive a string back from the
server indicating the status.
The client is a scheduled task. When it runs, it will get a recordset and
send each record as a string to the server. For each string the server
receives, it will send a string back to the client. Upon receiving this
confirmation, the client will continue sending records. This sending process
needs to be automated.
I can't get it work. The DataArival event of the winsock in the client
application didn't file until the who app ends. What I want on the client
side is Send, Receive, Send, Receive, ....
Can someone help please. I've been struggling for a few days now. Please
let me know if more detail is needed. Thanks.
| |
| Bob Butler 2004-10-29, 3:55 pm |
| "li" <li@discussions.microsoft.com> wrote in message news:4F76A624-531A-
4D82-9438-2D8813BE5ECB@microsoft.com
> I'm using winsock control in VB 6 to write a an application so that
> the client can send string to the server and receive a string back
> from the server indicating the status.
This is a *very* simple sample that may get you started.
Project 1: client: form with 1 winsock control and 1 command button
Private msData As String
Private Sub Command1_Click()
Dim ff As Long
' get some sample data to send
ff = FreeFile
Open "C:\winnt\system32\drivers\etc\hosts" For Binary As #1
msData = Space$(LOF(ff))
Get #ff, , msData
Close #ff
' connect to server
With Winsock1
.Protocol = sckTCPProtocol
.RemoteHost = "127.0.0.1"
.RemotePort = 99
.Connect
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
Winsock1.Close
End Sub
Private Sub Winsock1_Connect()
SendNextLine
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim sBuff As String
Winsock1.GetData sBuff
Debug.Print "server response: " & sBuff
SendNextLine
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)
MsgBox "winsock error " & Description
Unload Me
End Sub
Private Sub SendNextLine()
Dim sLine As String
Dim x As Long
x = InStr(1, msData, vbCrLf)
If x Then
sLine = Left$(msData, x + 2)
msData = Mid$(msData, x + 2)
Else
sLine = msData
End If
If Len(sLine) > 0 Then
Winsock1.SendData sLine
Else
Unload Me
End If
End Sub
--------------------------------------------------------
project 2: form with 2 winsock controls
Private Sub Form_Load()
' configure listener
With Winsock1
.Protocol = sckTCPProtocol
.LocalPort = 99
.Listen
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
Winsock2.Close
Winsock1.Close
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock2.Accept requestID
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)
MsgBox "winsock1 error " & Description
Unload Me
End Sub
Private Sub Winsock2_Close()
Debug.Print "EOF"
Winsock2.Close
End Sub
Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
Static sBuff As String
Dim sData As String
Dim x As Long
' buffer incoming data looking for vbCRLF delimiter
Winsock2.GetData sData
sBuff = sBuff & sData
x = InStr(1, sBuff, vbCrLf)
If x Then
' we have a record
sData = Left$(sBuff, x + 2)
sBuff = Mid$(sBuff, x + 2)
ProcessRecord sData
End If
End Sub
Private Sub Winsock2_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)
MsgBox "winsock2 error " & Description
Unload Me
End Sub
Private Sub ProcessRecord(ByVal TheData As String)
Debug.Print "got " & TheData
Winsock2.SendData "X" ' ack client
End Sub
---------------------------------------------------------
edit the client code to open an existing text file if the hosts file isn't
found; run the server and then run the client and click the command button.
the server will accept 1 line at a time and send back a single byte to
acknowledge that it is ready for the next record.
--
Reply to the group so all can participate
VB.Net... just say "No"
|
|
|
|
|