Results 1 to 4 of 4

Thread: Winsock tutorial

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Posts
    25

    Winsock tutorial

    I was folowing the winsock tutorial and when i finished it I coppied the code and launched another instance of VB, then I changed the "Winsock1.LocalPort" to 202 instead of 201 like it said in the tutorial. It does not work, and i dont know why.

    here is my code.


    Private Sub cmdExit_Click()
    End
    End Sub

    Private Sub cmdSend_Click()
    If Winsock1.State <> sckClosed Then
    Winsock1.Close
    Else
    If MsgBox("You are not conected do you wish to connect" _
    , vbYesNo + vbQuestion) = vbYes Then

    'Connect to machene

    Winsock1.RemoteHost = "127.0.0.1" 'txtAddress.Text
    Winsock1.RemotePort = 202
    Winsock1.Connect

    'wait for connection

    Do Until Winsock1.State = sckConnected
    DoEvents: DoEvents: DoEvents: DoEvents: DoEvents
    If Winsock1.State = sckError Then
    MsgBox "Problem Connecting"
    Exit Sub
    End If
    Loop

    'send data

    Winsock1.SendData ("txtMessage.text")

    Else
    Call MsgBox("ok -not connected")
    End If

    End If

    End Sub

    Private Sub Form_Load()
    On Error GoTo PortErr
    Winsock1.LocalPort = 201
    Winsock1.Listen
    Me.Caption = "NCS - Network Chat Software " & "(" & Winsock1.LocalIP & ")"
    Exit Sub
    PortErr:
    MsgBox "Another Program is using port 201"
    End Sub

    Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)

    If Winsock1.State <> sckClosed Then Winsock1.Close
    Winsock1.Accept requestID
    End If

    End Sub

    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

    'Display incoming data

    Dim strIncoming As String
    Winsock1.GetData strIncoming
    txtIncoming = strIncoming

    End Sub

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    The port number for both the client and the server should be the same. So change the two values Winsock1.RemotePort and Winsock1.LocalPort to same value.
    Last edited by amitabh; Mar 22nd, 2002 at 01:02 PM.

  3. #3
    Lively Member
    Join Date
    Feb 2002
    Location
    Nepal
    Posts
    103

    Thumbs up Well it works this way....

    Well the port numbers should be different if you are using the application in a standalone computer. what you should do is after writing the codes(with localport =201 & remoteport=202), you should compile & make exe file of that code. Next make localport=202 & remoteport=201 & recompile & make exe file with a different name ( eg make winsck1.exe for the first & winsck2.exe for second).

    Now You open both windows by double clicking on those exe files & test your application. Remember, you should compile & make exe & then test the application.

    See if it works or mail me at [email protected] if you have any problem.

    If everything goes fine, you will know why I said make localport=201 for first exe file & remoteport=201 for the second.

  4. #4

    Winsock1.Connect

    When connecting to a remote host with a winsock control, you must send the RemoteHostIP and RemoteHostPort values as parameters to the .Connect function. An example would be this:

    Dim IP As String, Port As Integer
    Winsock1.Connect IP, Port

    The .Remote* variables are Read Only. This is how to change them.
    http://petboysherman.8m.com
    Trust, Love, and Magic.

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