Results 1 to 4 of 4

Thread: [RESOLVED] Winsock Client/Server

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    207

    Resolved [RESOLVED] Winsock Client/Server

    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:

    Code:
    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:

    Code:
    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?
    Last edited by Blackbelt12; Jul 8th, 2009 at 10:42 PM.

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Winsock Client/Server

    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:

    Code:
    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:

    Code:
    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    207

    Re: Winsock Client/Server

    Thanks a lot for your help man!

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: [RESOLVED] Winsock Client/Server

    You're welcome.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width