PDA

Click to See Complete Forum and Search --> : [RESOLVED] Winsock Client/Server


Blackbelt12
Jul 8th, 2009, 10:37 PM
I have a client side and server side application. I'm trying to get it so when I click a button the client side, it opens up a messagebox on the server side. I was thinking the code was something like this:

Client Side:


Private Sub cmdConnect_Click()
Winsock1.RemoteHost = "127.0.0.1"

Winsock1.RemotePort = 6667

Winsock1.Listen

End Sub

Private Sub cmdSendMsg_Click()
Dim Data

Data = "Open MessageBox"

Winsock1.SendData (Data)

End Sub




Server Side:


Private Sub Form_Load()
Winsock1.RemoteHost = "127.0.0.1"

Winsock1.RemotePort = 6667

Winsock1.Listen
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim Data

Winsock1.GetData (Data)

If InStr(Data, "Open MessageBox") Then
MsgBox("Opened MessageBox!")
End If

End Sub


But this doesn't work. Any ideas?

DigiRev
Jul 9th, 2009, 02:18 AM
Just a rough example. You'll want to run the server before running the client, since the client tries to connect when it first starts.

Client:

Option Explicit

Private Sub cmdSendMessage_Click()

If Winsock1.State = sckConnected Then
Winsock1.SendData "MessageBox"
Else
MsgBox "Not connected yet!", vbInformation
End If

End Sub

Private Sub Form_Load()

cmdSendMessage.Enabled = False

With Winsock1
.Close
.RemoteHost = "127.0.0.1"
.RemotePort = 6667
.Connect
End With

'Connect() event will fire if successful
'Error() event will fire if not
'Don't try to send data until connected

End Sub

Private Sub Form_Unload(Cancel As Integer)

Winsock1.Close

End Sub

Private Sub Winsock1_Close()

cmdSendMessage.Enabled = False
MsgBox "Connection to server lost", vbExclamation

End Sub

Private Sub Winsock1_Connect()

cmdSendMessage.Enabled = True

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)

cmdSendMessage.Enabled = False
MsgBox "Error connecting to server: " & Description, vbExclamation

End Sub

Server:

Option Explicit

Private Sub Form_Load()

With Winsock1
.Close
.LocalPort = 6667
.Listen
End With

End Sub

'Client has disconnected, reopen server for future connections
Private Sub Winsock1_Close()

Winsock1.Close
Winsock1.Listen

End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)

Winsock1.Close
Winsock1.Accept requestID

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

Dim strData As String

Winsock1.GetData strData, vbString, bytesTotal

If strData = "MessageBox" Then
MsgBox "The client told me to show a message box, tee hee hee hee <3 (love u! xoxoxo)", vbExclamation
End If

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)

Winsock1_Close

End Sub

Blackbelt12
Jul 9th, 2009, 01:20 PM
Thanks a lot for your help man!

DigiRev
Jul 9th, 2009, 09:31 PM
You're welcome.