How do I send messages using Winsock. Also, how do I recieve them?
Printable View
How do I send messages using Winsock. Also, how do I recieve them?
Is this the coomand that you looking for?
Code:Winsock1.SendData "My Test Data"
yeah, but If I am sending more than one thing, how will the other computer know which is which? Can I name each or something?
Do you mean more than one diffrence string/message?
Then you may need to define your own Message TAG and make use of the standard communication message format like:
[STX]+[Message TAG]+[Message Body]+[ETX]
Where STX = Start Of Text ,normally is Chr(2)
ETX = End Of Text ,normally is Chr(3)
Message TAG = Your own define TAG to differentiate
each message type.
Message Body = Your real message that to be transfer.
After send this, you should wait for the ACK (Acknowledgement) or NAK from the remote terminal
Then in your
Hope this can help you. :)Code:Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strHold As String
Dim MsgTAG As String
Dim MsgBody As String
Winsock1.GetData ClientData, vbString
'Search for the STX
strHold = left(strHold, InStr(1,StrHold, Chr(2),vbBinaryCompare)-1)
'Search for the ETX
strHold = Mid(strHold, 1, InStr(1,StrHold, Chr(3),vbBinaryCompare)-1)
'Get the Message Tag and Message Body
'Assume the first Character is your Message TAG
Select case Mid(strhold,1,1)
Case TAG1
Debug.Pring "Message With TAG1 received - " & Mid(strHold,2)
Case TAG2
Debug.Pring "Message With TAG2 received - " & Mid(strHold,2)
Case Else
Debug.Pring "Unknown Message TAG received"
End Select
End Sub
[Edited by Chris on 06-18-2000 at 09:55 PM]
That helped a lot, but I still am having a really hard time with this. If it isn't too much trouble, can you show me what to do. Like make a demo. If you can't thats fine. Thanks for your help! :)
Hi SteveCRM, I just wrote a sample program & hope this can help you understand what I mention in the early post.
If you need the sample program source file, do let me know & i'll send it to you.Code:Code under frmClient.frm
Option Explicit
Private MsgType As Integer
Private Sub chkType_Click(Index As Integer)
MsgType = Index
End Sub
Private Sub cmdAction_Click()
'Setup Winsock Control
Winsock1.RemoteHost = "127.0.0.1"
Winsock1.RemotePort = 1234
Winsock1.Connect
End Sub
Private Sub BuildMessage()
Dim BodyMsg As String
Select Case MsgType
Case 0 'TAG 1 "A"
BodyMsg = "A" & txtBodyMsg
Case 1 'TAG 2 "D"
BodyMsg = "D" & txtBodyMsg
Case 2 'TAG 3 "C"
BodyMsg = "C" & txtBodyMsg
Case 3 'unknow TAG "x"
BodyMsg = "x" & txtBodyMsg
End Select
'Append the STX + ETX in the Body Message
BodyMsg = Chr(2) & BodyMsg & Chr(3)
'Send Message back to Server.
Winsock1.SendData BodyMsg
End Sub
Private Sub cmdSend_Click()
BuildMessage
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If Winsock1.State <> sckClosed Then Winsock1.Close
End Sub
Private Sub OptType_Click(Index As Integer)
MsgType = Index
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 "Error (" & Number & "), " & Description, vbExclamation + vbOKOnly
End Sub
Code under the frmServer.frm
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
ShowData "Connection Establish with ID: " & requestID
End Sub
Private Sub ShowData(ByVal MyData As String)
txtRx = txtRx & MyData & vbCrLf
txtRx.SelStart = Len(txtRx)
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim ClientData As String
Dim Tmp As String
'Retrieve the data from WinSock Control
Winsock1.GetData ClientData, vbString
'Remove the STX and ETX
DoEvents
Do While Len(ClientData) <> 0
Select Case Asc(ClientData)
Case 2 'STX
'reset the buffer
Tmp = ""
Case 3 'ETX
'clear all the ClientData
ClientData = ""
Exit Do
Case Else
Tmp = Tmp & Mid(ClientData, 1, 1)
End Select
ClientData = Mid(ClientData, 2)
Loop
'Analyze the client's send data.
'Check Message TAG
Select Case Mid(Tmp, 1, 1)
Case "A" 'TAG 1
ShowData "TAG 1 message received [A]"
Case "B" 'TAG 2
ShowData "TAG 2 message received [D]"
Case "C" 'TAG 3
ShowData "TAG 3 message received [C]"
Case "x" 'Unknown TAG
ShowData "Unknown TAG message received [x]"
Case Else 'Empy message TAG
ShowData "Unknown TAG message received [" & Mid(Tmp, 1, 1) & "]"
End Select
'Display the body message
ShowData "Body Message : " & Mid(Tmp, 2)
End Sub
[Edited by Chris on 06-18-2000 at 10:00 PM]
Error, connection forcefully refused.
Im sorry I don't understand much of this, could you send it to [email protected]? Thanks!