Results 1 to 3 of 3

Thread: Winsock newbie

  1. #1

    Thread Starter
    Hyperactive Member MeTTa@'s Avatar
    Join Date
    Aug 2005
    Posts
    312

    Winsock newbie

    I wanna dig into winsock, so i hear its the best networking code for vb6. I would be using it most likely for games. So im looking for some really basic stuff to get started with, something easy so i could just trasfer variables from 2 computers. Could someone give me a push in the door or lead me to some nice tutorials. Thanks,
    ___
    Dan

  2. #2
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Winsock newbie

    Try searching google but be sure to stay away from winsock API to start with, just look for some tutorials using the normal winsock control for vb6.

    Chris
    Chris

  3. #3
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Winsock newbie

    Here is a winsock example, there is two projects, the first project is the Server, which will listen and accept connections, the other will be the client, which will make the connection to the server.

    The server listens for a connection on port 8989
    The client makes a connection to the server on port 8989
    The server and client can then send data to each other using the textbox on form

    To add a winsock to the form...go to Project>Componants>select Microsoft Winsock Control>Drag it from the toolbar to the form, rename it to sckServer or sckClient.

    In the server project there is 4 controls:

    sckServer - This is the winsock control
    lblStatus - This label displays the connection status
    txtSend - This textbox will be used to input data to send to the client
    cmdSend - This button will be used to send data from txtSend to the client

    This is the entire code for the server:
    VB Code:
    1. Private Sub Form_Load()
    2. 'this section initializes the sckServer winsock to listen for connections
    3.  
    4. sckServer.Close 'Close the socket
    5. sckServer.LocalPort = 8989 'Set the socket port to 8989
    6. sckServer.Listen 'Listen for connection attempts on the port
    7.  
    8. lblStatus.Caption = "Listening for connections"
    9. End Sub
    10.  
    11. Private Sub sckServer_Close()
    12. 'When the remote computer ends the connection, this event is fired
    13. lblStatus.Caption = "Connection closed"
    14. sckServer.Close
    15. End Sub
    16.  
    17. Private Sub sckServer_ConnectionRequest(ByVal requestID As Long)
    18. 'When a connection attempt is made to the socket, this event is fired...
    19.  
    20. sckServer.Close 'Close the socket
    21. sckServer.Accept requestID 'Accept the connection
    22.  
    23. End Sub
    24.  
    25. Private Sub sckServer_DataArrival(ByVal bytesTotal As Long)
    26. 'When some data is received on the socket, this event is fired
    27. Dim strData As String
    28.  
    29. sckServer.GetData strData, vbString 'Grab the data received and store it in strData variable
    30. MsgBox "Data Arrived From Client: " & strData 'Display the data received in a message box
    31. End Sub
    32.  
    33. Private Sub sckServer_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)
    34. 'This event is fired when there is an error in the socket
    35. 'this event is used more in the client socket when making connections
    36. 'we will just put a message box which will display the error
    37.  
    38. MsgBox "Error: " & Number & ", " & Description, vbCritical, "Server Error"
    39. End Sub

    in the client project there are 5 controls...

    sckClient - This is the winsock for the client
    lblStatus - Same as server
    cmdConnect - When this button is pushed, the program makes a connection attempt to the server
    txtSend - Same as server
    cmdSend - Same as server

    Here is the entire code for the client project...
    VB Code:
    1. Private Sub cmdConnect_Click()
    2. 'Sub for the connect button
    3. sckClient.Close 'Close the socket
    4. sckClient.Connect "127.0.0.1", 8989 'Make a connection to 127.0.0.1 on port 8989
    5.  
    6. 'The 127.0.0.1 IP will loop back to your own computer
    7. 'You could replace this with a server IP
    8. End Sub
    9.  
    10. Private Sub cmdSend_Click()
    11. 'Send button sub
    12. sckClient.SendData txtSend.Text
    13. End Sub
    14.  
    15. Private Sub sckClient_Close()
    16. 'This event is fired when the remote side ends the connection
    17. lblStatus.Caption = "Connection closed"
    18. End Sub
    19.  
    20. Private Sub sckClient_Connect()
    21. 'This event is fired when the connection attempt has been made and established
    22. lblStatus.Caption = "Connected"
    23. sckClient.SendData "HELLO" 'Send some data to the server saying HELLO
    24. End Sub
    25.  
    26. Private Sub sckClient_DataArrival(ByVal bytesTotal As Long)
    27. 'When some data is received on the socket, this event is fired
    28. Dim strData As String
    29.  
    30. sckClient.GetData strData, vbString 'Grab the data received and store it in strData variable
    31. MsgBox "Data Arrived From Server: " & strData 'Display the data received in a message box
    32. End Sub
    33.  
    34. Private Sub sckClient_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)
    35. 'This event is fired when there is an error in the socket
    36. 'this event is used more in the client socket when making connections
    37. 'we will just put a message box which will display the error
    38.  
    39. MsgBox "Error: " & Number & ", " & Description, vbCritical, "Client Error"
    40. End Sub

    You could also add a button to end the connection, you would put this codeL
    VB Code:
    1. sckClient.Close
    Be sure to change the name as required^

    You could play around with this project and learn winsock, but I would certainly recommend reading some winsock tutorials.

    One more thing to know about winsock: in the Winsock_Close event, dont ever put a message box there to tell the user that the connection is closed, always use a label to do this, because there is some strange bug where it constantly repeats a messagebox sometimes, and this will make you unable to continue your app, you would have to close vb in task manager.

    Hope this helps and good luck!

    Chris
    Chris

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