• home
  • forum
  • my
  • kt
  • download
  • Sending Data Between 2 Computers

    Author: 2007-09-06 09:39:43 From:

    Hey everybody, this tutorial is going to show you how to send data via 2 computers over the internet in visual basic smile.gif

    First of all start a new Standard EXE and then Ctrl + T to open the components window.

    Now find Microsoft Winsock Control and tick the box to the left of it.

    You will now see a new object in the toolbox, place 2 instances of the new object onto the form and name one of them wskServer and one of them wskClient

    Now place 3 command buttons on your form and name them cmdListen, cmdConnect and cmdSend

    Now I¡¯ll just give you a brief description of how this is going to work, when the user clicks cmdListen then wskServer will start listening for incoming connections and if the user clicks cmdConnect then wskClient will try to connect to the IP & Port specified, when the user clicks cmdSend it will send the data to the client / server its connected too.

    Note: If you don¡¯t have anyone to send your application to you can test it out on your own computer just fine smile.gif

    The first thing we want to do is enabled the app to listen for connections and add 2 variables for later on so add this code in the General area of your code.

    CODE
    Dim wskListening As String 'This is so we can tell later whether or not we are acting as the server or the client
    Dim wskConnecting As String


    And add this below it

    CODE
    Private Sub cmdListen_Click()

    wskServer.LocalPort = 8000 'Set the port that the client should connect too

    wskServer.Listen 'Start listening for incoming connections

    wskListening = "True"

    End Sub


    Now we want to put in the code that will let make the client connect to the server. So add this code to your project

    CODE
    Private Sub cmdConnect_Click()

    wskClient.RemoteHost = localhost 'replace this with the I.P of the computer you wish to connect too, this will attempt to connect to your own PC

    wskClient.RemotePort = 8000 'Set the port it should try to connect too, this MUST be the same as the port the server is listening for on the other end

    wskClient.Connect wskClient.RemoteHost, wskClient.RemotePort 'Connect to the server

    wskConnecting = "True"

    End Sub


    Now the next task is to get the server to accept the incoming connection. Very simple and short, add this to your project.

    CODE
    Private Sub wskServer_ConnectionRequest(ByVal requestID As Long)

    wskServer.Close

    wskServer.Accept (requestID) 'Accepts the incoming connection

    End Sub


    Now your app can connect to 2 different computers smile.gif but we want to send data between them, so add this code.

    CODE
    Private Sub cmdSend_Click()

    If wskConnecting = "True" Then

       wskClient.SendData ("Data from the client") 'Replace this with whatever you text you want
       
    ElseIf wskListening = "True" Then

       wskServer.SendData ("Data from the server")
       
    End If

    End Sub

    Private Sub wskServer_DataArrival(ByVal bytesTotal As Long)

    Dim data As String 'Set the string to store the data in

    wskServer.GetData data 'Recieve the data and store it in the string called data

    MsgBox data, vbOKOnly, "Data recieved" 'Display the data in a message box

    End Sub

    Private Sub wskClient_DataArrival(ByVal bytesTotal As Long)

    Dim data As String 'Set the string to store the data in

    wskClient.GetData data 'Recieve the data and store it in the string called data

    MsgBox data, vbOKOnly, "Data recieved" 'Display the data in a message box

    End Sub


    And there you have it smile.gif there may be an error in there where I have made a spelling mistake, which is why I have attached a fully working copy of this project smile.gif which also contains a few little extra features tongue.gif nothing special. If you do notice any errors please post them.

    If you have liked this tutorial then please show your appreciation register and leave your feedback.
    Attached File(s)
    Attached File  Winsock_Tutorial.zip ( 2.21K ) Number of downloads: 1715

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      .NET (8)
      Buttons (3)
      Database Related (7)
      Date and Time (1)
      Development (3)
      Error Handling (2)
      File Manipulation (5)
      Introduction to Visual Basic (9)
      Miscellaneous (2)
      Multimedia (9)
      Networking (9)
      Security (1)
      VB Script (6)

    New

    Hot