Page 1 of 2 12 LastLast
Results 1 to 40 of 41

Thread: Sending And Recieving Data via WinSOCK

  1. #1

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    48

    Sending And Recieving Data via WinSOCK

    Hey everyone, this has been done before but prior to me having the project saved on my drive i reformatted, excluding all existing projects so i need help urgently.

    Basically i have a project with Form1 named Client
    two labels, two text boxes (username and password) , one winsock control and a command button (submit)

    I have another project with Form1 named Server
    and a text box

    I would like to transmit data between the two so that when a username and password is typed in on the client and the button Submit is pressed, it displays in the text box of Server (depending if i have it running or not) the username and password typed on the client. I have managed to get this to work more than once and i know for definete it can be done.
    Help is more than appreciated,
    - revivalry

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Sending And Recieving Data via WinSOCK

    Have you tried searching for code samples?

  3. #3

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    48

    Re: Sending And Recieving Data via WinSOCK

    Yes i have..

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Sending And Recieving Data via WinSOCK

    Here's a very simple example. Open up a project, create 2 forms, frmServer and frmClient.
    On frmServer:
    Draw two TextBoxes named txtUserName and txtPassword. Draw a Winsock control named Winsock1.
    Insert the following into frmServer
    Code:
    Private Sub Form_Load()
    '
    ' Server Form
    '
    Load frmClient
    frmClient.Visible = True
    frmClient.Top = Me.Top
    frmClient.Left = Me.Left + Me.Width
    frmClient.Caption = "Client"
    Winsock1.LocalPort = 8001
    Winsock1.Listen
    Me.Caption = "Server - Listening"
    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
    Static strBuffer As String
    Dim strReceived As String
    Dim lngPos As Long
    Dim boFinished As Boolean
    Winsock1.GetData strReceived
    strBuffer = strBuffer & strReceived
    Do
        lngPos = InStr(strBuffer, vbCr)
        If lngPos > 0 Then
            strData = Split(Mid$(strBuffer, 1, lngPos - 1), "|")
            txtUserName.Text = strData(0)
            txtPassword.Text = strData(1)
            If lngPos < Len(strBuffer) Then
                strBuffer = Mid$(strBuffer, lngPos + 1)
            Else
                strBuffer = ""
                boFinished = True
            End If
        Else
            boFinished = True
        End If
    Loop Until boFinished = True
    End Sub
    On frmClient:
    Draw two TextBoxes, named txtUserName and txtPassword and also draw a CommandButton named cmdSend. Draw a Winsock control named Winsock1.
    Insert the following into frmClient
    Code:
    '
    ' Client Form
    '
    Private Sub cmdSend_Click()
    If txtUserName.Text <> "" And txtPassword.Text <> "" Then
        Winsock1.RemoteHost = "localhost"
        Winsock1.RemotePort = 8001
        Winsock1.Connect
    Else
        MsgBox "Must specify both Username and Password"
    End If
    End Sub
    
    Private Sub Winsock1_Connect()
    Winsock1.SendData txtUserName.Text & "|" & txtPassword.Text & vbCr
    End Sub
    If it's not already, make frmServer the start-up form for the project and run.
    Type the username and password into the client form and click the CommandButton. Both should appear in the TextBoxes of the Server form.

    You can create 2 projects if you want to separate the Client and Server. Just put frmServer in one, removing everything in the Form_Load event except for
    Code:
    Winsock1.LocalPort = 8001
    Winsock1.Listen
    Me.Caption = "Server - Listening"
    and put frmClient in the other project. Run the Server and then run the Client.

  5. #5

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    48

    Re: Sending And Recieving Data via WinSOCK

    That's left me breathless lol, thank you very much ill test it ==]

  6. #6

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    48

    Re: Sending And Recieving Data via WinSOCK

    Quote Originally Posted by Doogle
    Here's a very simple example. Open up a project, create 2 forms, frmServer and frmClient.
    On frmServer:
    Draw two TextBoxes named txtUserName and txtPassword. Draw a Winsock control named Winsock1.
    Insert the following into frmServer
    Code:
    Private Sub Form_Load()
    '
    ' Server Form
    '
    Load frmClient
    frmClient.Visible = True
    frmClient.Top = Me.Top
    frmClient.Left = Me.Left + Me.Width
    frmClient.Caption = "Client"
    Winsock1.LocalPort = 8001
    Winsock1.Listen
    Me.Caption = "Server - Listening"
    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
    Static strBuffer As String
    Dim strReceived As String
    Dim lngPos As Long
    Dim boFinished As Boolean
    Winsock1.GetData strReceived
    strBuffer = strBuffer & strReceived
    Do
        lngPos = InStr(strBuffer, vbCr)
        If lngPos > 0 Then
            strData = Split(Mid$(strBuffer, 1, lngPos - 1), "|")
            txtUserName.Text = strData(0)
            txtPassword.Text = strData(1)
            If lngPos < Len(strBuffer) Then
                strBuffer = Mid$(strBuffer, lngPos + 1)
            Else
                strBuffer = ""
                boFinished = True
            End If
        Else
            boFinished = True
        End If
    Loop Until boFinished = True
    End Sub
    On frmClient:
    Draw two TextBoxes, named txtUserName and txtPassword and also draw a CommandButton named cmdSend. Draw a Winsock control named Winsock1.
    Insert the following into frmClient
    Code:
    '
    ' Client Form
    '
    Private Sub cmdSend_Click()
    If txtUserName.Text <> "" And txtPassword.Text <> "" Then
        Winsock1.RemoteHost = "localhost"
        Winsock1.RemotePort = 8001
        Winsock1.Connect
    Else
        MsgBox "Must specify both Username and Password"
    End If
    End Sub
    
    Private Sub Winsock1_Connect()
    Winsock1.SendData txtUserName.Text & "|" & txtPassword.Text & vbCr
    End Sub
    If it's not already, make frmServer the start-up form for the project and run.
    Type the username and password into the client form and click the CommandButton. Both should appear in the TextBoxes of the Server form.

    You can create 2 projects if you want to separate the Client and Server. Just put frmServer in one, removing everything in the Form_Load event except for
    Code:
    Winsock1.LocalPort = 8001
    Winsock1.Listen
    Me.Caption = "Server - Listening"
    and put frmClient in the other project. Run the Server and then run the Client.

    Ok now the ip is set to "localhost" aka "127.0.0.1" how do i set it so that when the user and pass is typed and the login is clicked from a TOTALLY DIFFERENT COMPUTER i will still recieve the data? Use a DNS or my IP?
    HELP!

  7. #7
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Sending And Recieving Data via WinSOCK

    Well as I said in my original post you split it into 2 projects, 1 for the Server and one for the Client. You can install the client on another machine and specify the IP Address for the Computer on which the Server is running. You can use a DNS name if you've got one for your Server
    eg
    Code:
    Private Sub cmdSend_Click()
    If txtUserName.Text <> "" And txtPassword.Text <> "" Then
        Winsock1.RemoteHost = "xxx.xxx.xxx.xxx"      ' Server's IP Address
        Winsock1.RemotePort = 8001
        Winsock1.Connect
    Else
        MsgBox "Must specify both Username and Password"
    End If
    End Sub
    or
    Code:
    Private Sub cmdSend_Click()
    If txtUserName.Text <> "" And txtPassword.Text <> "" Then
        Winsock1.RemoteHost = "mydnsentry"       'DNS of Server
        Winsock1.RemotePort = 8001
        Winsock1.Connect
    Else
        MsgBox "Must specify both Username and Password"
    End If
    End Sub

  8. #8

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    48

    Re: Sending And Recieving Data via WinSOCK

    Quote Originally Posted by Doogle
    Well as I said in my original post you split it into 2 projects, 1 for the Server and one for the Client. You can install the client on another machine and specify the IP Address for the Computer on which the Server is running. You can use a DNS name if you've got one for your Server
    eg
    Code:
    Private Sub cmdSend_Click()
    If txtUserName.Text <> "" And txtPassword.Text <> "" Then
        Winsock1.RemoteHost = "xxx.xxx.xxx.xxx"      ' Server's IP Address
        Winsock1.RemotePort = 8001
        Winsock1.Connect
    Else
        MsgBox "Must specify both Username and Password"
    End If
    End Sub
    or
    Code:
    Private Sub cmdSend_Click()
    If txtUserName.Text <> "" And txtPassword.Text <> "" Then
        Winsock1.RemoteHost = "mydnsentry"       'DNS of Server
        Winsock1.RemotePort = 8001
        Winsock1.Connect
    Else
        MsgBox "Must specify both Username and Password"
    End If
    End Sub
    Ok thank you, i now have it split into two projects and working.
    In the server i would rather one big text box and each time a user and pass is inserted on the client app and login is hit the logins will list down in one text box. Do you know what i mean?

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Sending And Recieving Data via WinSOCK

    OK. Remove txtUsername and txtPassword from the Server Form and draw a new TextBox (txtData), set its MultiLine Property to true and put on a vertical scroll bar. Then you need to make some changes in the server DataArrival event vis (I've marked the lines that have changed)
    Code:
    Do
        lngPos = InStr(strBuffer, vbCr)
        If lngPos > 0 Then
            strData = Split(Mid$(strBuffer, 1, lngPos - 1), "|")
            txtData.Text = txtData.Text & strData(0) & vbCrLf 'Changed
            txtData.Text = txtData.Text & strData(1) & vbCrLf 'Changed
            If lngPos < Len(strBuffer) Then
                strBuffer = Mid$(strBuffer, lngPos + 1)
            Else
                strBuffer = ""
                boFinished = True
            End If
        Else
            boFinished = True
        End If
    Loop Until boFinished = True

  10. #10

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    48

    Re: Sending And Recieving Data via WinSOCK

    Ok that seems to be great and working, i think there's two other little things that need to be done:

    when the username and password displays in the server txtData section i need them to be seperated from each other so i dont get muddled up. just a little something like -=-=-=-=-=-=-=-=- gets added along with the user and pass to prevent confusion..

    Also when i have the client open and i press login more than once i get a run time error and i believe this is because there is a connection already open and another one cannot be created to the same server, so when login is clicked the first time i need it to: send me the data > close connection. Then it can be pressed more than once to prevent suspicion. THanks for your help so far mate you are great =]

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Sending And Recieving Data via WinSOCK

    In the Server, once the data has been received then close the socket and start it listening again:
    Code:
    Do
        lngPos = InStr(strBuffer, vbCr)
        If lngPos > 0 Then
            strData = Split(Mid$(strBuffer, 1, lngPos - 1), "|")
            txtData.Text = txtData.Text & strData(0) & vbCrLf 
            txtData.Text = txtData.Text & strData(1) & vbCrLf 
            txtData/Text = txtData.Text & "-=-=-=-=-=-=-=" & vbCrLf
            If lngPos < Len(strBuffer) Then
                strBuffer = Mid$(strBuffer, lngPos + 1)
            Else
                strBuffer = ""
                boFinished = True
                Winsock1.Close
                Winsock1.Listen
            End If
        Else
            boFinished = True
        End If
    Loop Until boFinished = True
    and in the Client add:
    Code:
    Private Sub Winsock1_Close()
    Winsock1.Close
    End Sub

  12. #12
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Sending And Recieving Data via WinSOCK

    Small note..

    Should
    Code:
    txtData/Text = txtData.Text & "-=-=-=-=-=-=-=" & vbCrLf
    Be this instead:
    Code:
    txtData.Text = txtData.Text & "-=-=-=-=-=-=-=" & vbCrLf

  13. #13
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Sending And Recieving Data via WinSOCK

    Dsylexic keyboard

  14. #14

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    48

    Re: Sending And Recieving Data via WinSOCK

    I think everything's done.. let me double check

    Thanks mate ur great

  15. #15
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Sending And Recieving Data via WinSOCK

    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim strData() As String
    '****Static strBuffer As String
    Dim strReceived As String
    Dim lngPos As Long
    '****Dim boFinished As Boolean
    Winsock1.GetData strReceived
    '****strBuffer = strBuffer '& strReceived
    '****Do
    lngPos = InStr(strReceived, vbCr)
    If lngPos > 0 Then
    strData = Split(Mid$(strReceived, 1, lngPos - 1), "|")
    txtUserName.Text = strData(0)
    txtPassword.Text = strData(1)
    Text1.Text = strData(2)
    If lngPos < Len(strReceived) Then
    strBuffer = Mid$(strReceived, lngPos + 1)
    Else
    strReceived = ""
    '****boFinished = True
    End If
    Else
    '****boFinished = True
    End If
    '****Loop Until boFinished = True
    End Sub

    i added 1 more textbox to test to send multiple strings to split
    and i removed the comment with asterisk(*
    and it is still working...
    meaning the code which i removed are no functions at all?
    note: i just want to experiment/simply the code above

    can you explain, TNX.
    *****************
    VB6,PHP,VS 2005

  16. #16
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Sending And Recieving Data via WinSOCK

    The code you removed it to make sure that a complete record has been received. Whilst it may work OK without that code when you're running the client and server locally, when you try over a WAN or the Internet all the data may not arrive in one single event, it may take a number of events. Unless you cater for that you will get unexpected results and then accuse Winsock of losing data.

  17. #17
    Member
    Join Date
    Nov 2009
    Posts
    44

    Re: Sending And Recieving Data via WinSOCK

    Hi...

    i ve tried this code and its working

    but, its working for the first time, then if i entered a new values and pressed cmdSend it gives me error...

    how i can make it sending every time i press cmdSend without the need to close the whole program and start it again...

  18. #18
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Sending And Recieving Data via WinSOCK

    Post the error

  19. #19
    Member
    Join Date
    Nov 2009
    Posts
    44

    Re: Sending And Recieving Data via WinSOCK

    "Runtime error '40020':
    Invalid operation at current state"

    when i click Debug

    it highlights (Winsock1.RemoteHost = "localhost")

  20. #20
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Sending And Recieving Data via WinSOCK

    You cannot just press cmdSend everytime and anytime you want to. You must wait until the previous operation has finished. What you are doing is trying to send one sendData command after another and the previous command is not finished yet. I do not know how you are coding you project so I can't tell you what to correct.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  21. #21
    Addicted Member
    Join Date
    Oct 2009
    Location
    Clive, IA in America!!!!
    Posts
    204

    Re: Sending And Recieving Data via WinSOCK

    What if I don't know the remote computer's IP Address. Say, some friend wants to connect to a chat room. Can he transfere his IP Address so that people can talk with him?

    VBNetDude - Thinking Programmatically
    By Silver Seal Software

    Don't forget to mark your thread as "Resolved" using the Thread Tools menu on top. And don't forget to rate the answers that help you the most!

  22. #22
    Member
    Join Date
    Nov 2009
    Posts
    44

    Re: Sending And Recieving Data via WinSOCK

    Quote Originally Posted by jmsrickland View Post
    You cannot just press cmdSend everytime and anytime you want to. You must wait until the previous operation has finished. What you are doing is trying to send one sendData command after another and the previous command is not finished yet. I do not know how you are coding you project so I can't tell you what to correct.
    i am using the same code provided here...

    yeah i am aware of that operation must finish so that another one can go in proccess

    but, does that happen...

    how i could know that sendData is finished so i can press cmdSend again

  23. #23
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Sending And Recieving Data via WinSOCK

    One way would be to use either Private Sub Winsock1_SendComplete() or Private Sub Winsock1_SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long). In the _SendProgress event your data has been sent when bytesRemaining reaches zero. You could set an indicator in the _SendComplete event that informs your cmdSend sub when it is OK to send again. In your cmdSend sub check this indicator and if off then turn it on (indicator must be declared as Public) just before you issue the sendData command. Then in the _SendComplete sub turn the indicator off.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  24. #24
    Member
    Join Date
    Nov 2009
    Posts
    44

    Re: Sending And Recieving Data via WinSOCK

    Quote Originally Posted by jmsrickland View Post
    One way would be to use either Private Sub Winsock1_SendComplete() or Private Sub Winsock1_SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long). In the _SendProgress event your data has been sent when bytesRemaining reaches zero. You could set an indicator in the _SendComplete event that informs your cmdSend sub when it is OK to send again. In your cmdSend sub check this indicator and if off then turn it on (indicator must be declared as Public) just before you issue the sendData command. Then in the _SendComplete sub turn the indicator off.
    i ve tried it by i couldnt do it

    sorry i am too biggner in VB

    can you write for me the modification code please

  25. #25
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Sending And Recieving Data via WinSOCK

    Code:
      '
      '
    Dim DataSent As Boolean
      '
      ' Other declarations, etc
      '
    Private Sub FormLoad()
      '
     DataSent = True
      '
      ' 
      '
    End Sub
    
    Private Sub cmdSend(.....)
      '
      '
     If DataSent Then
       DataSent = False
       Winsock1.sendData data
     End If
      '
      '
    End Sub
      '
      ' Other Subs, etc
      '
    Private Sub Winsock1_SendComplete()
     DataSent = True
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  26. #26
    Member
    Join Date
    Nov 2009
    Posts
    44

    Re: Sending And Recieving Data via WinSOCK

    Code:
    Dim DataSent As Boolean
    '
    ' Client Form
    '
    Private Sub cmdSend_Click()
    If txtUserName.Text <> "" And txtPassword.Text <> "" Then
        Winsock1.RemoteHost = "localhost"
        Winsock1.RemotePort = 8001
        Winsock1.Connect
    Else
        MsgBox "Must specify both Username and Password"
    End If
    
    End Sub
    
    Private Sub Form_Load()
    DataSent = True
    End Sub
    
    Private Sub Winsock1_Connect()
    If DataSent Then
       DataSent = False
       Winsock1.SendData txtUserName.Text & "|" & txtPassword.Text & vbCr
     End If
    End Sub
    Private Sub Winsock1_SendComplete()
    DataSent = True
    End Sub
    this is the code...

    still not working...

  27. #27
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Sending And Recieving Data via WinSOCK

    Well, of course not. Your cmdSend sub is used only for connecting to the remote socket. You can do this only once. In your _Connect() sub you know you have made a connection and therefore you do not need to test the indicator DataSent. If you want to send more data after the initial connect you will need a Sub for that and in that sub you test the DataSent indicator. The name of your Sub cmdSend is misleading and it should be called cmdConnectToServer_Click(); not cmdSend_Click(). Make a new sub called cmdSend_Click() and put in it this code:
    Code:
    Private Sub cmdSend_Click(.....)
      '
      '
     If DataSent Then
       DataSent = False
       Winsock1.sendData data
     End If
      '
      '
    End Sub
    In your sub Winsock1_Connect() put this code:
    Code:
    Private Sub Winsock1_Connect()
     Winsock1.SendData txtUserName.Text & "|" & txtPassword.Text & vbCr
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  28. #28
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Sending And Recieving Data via WinSOCK

    If I understand your original problem correctely based on this statement how i could know that sendData is finished so i can press cmdSend again you are really trying to connect to the same host more than once which you cannot do unless the host has closed his socket. You do not want to press cmdSend again. The first time you press it, it requests a connection and in your _Connect() event the connection is completed. That's it. Don't do it again. However, like I said, if the host closes his socket then you can do it again and you will know that the host has closed his socket in your Winsock1_Close event.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  29. #29
    Member
    Join Date
    Nov 2009
    Posts
    44

    Re: Sending And Recieving Data via WinSOCK

    Quote Originally Posted by jmsrickland View Post
    Well, of course not. Your cmdSend sub is used only for connecting to the remote socket. You can do this only once. In your _Connect() sub you know you have made a connection and therefore you do not need to test the indicator DataSent. If you want to send more data after the initial connect you will need a Sub for that and in that sub you test the DataSent indicator. The name of your Sub cmdSend is misleading and it should be called cmdConnectToServer_Click(); not cmdSend_Click(). Make a new sub called cmdSend_Click() and put in it this code:
    Code:
    Private Sub cmdSend_Click(.....)
      '
      '
     If DataSent Then
       DataSent = False
       Winsock1.sendData data
     End If
      '
      '
    End Sub
    In your sub Winsock1_Connect() put this code:
    Code:
    Private Sub Winsock1_Connect()
     Winsock1.SendData txtUserName.Text & "|" & txtPassword.Text & vbCr
    End Sub
    putting Winsock1.sendData data in cmdSend_Click() cuasing error again

    for sure i am changing data to txtUserName.Text & "|" & txtPassword.Text & vbCr

    and yes, what i want the code to do is that when i press send the data be transfferd through th sock and aftter finishing the transfer it close automatically

    so, when i press send again it works without any problems and etc

  30. #30
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Sending And Recieving Data via WinSOCK

    Sockets don't close automatically; you have to close the socket. Alway put a Winsoclk1.Close in your connection sub just before you start to make a connection.

    Code:
    Private Sub cmdSend_Click()
     If txtUserName.Text <> "" And txtPassword.Text <> "" Then
       Winsock1.Close
       Winsock1.RemoteHost = "localhost"
       Winsock1.RemotePort = 8001
       Winsock1.Connect
    Else
        MsgBox "Must specify both Username and Password"
    End If
    and yes, what i want the code to do is that when i press send the data be transfferd through th sock and aftter finishing the transfer it close automatically

    so, when i press send again it works without any problems and etc


    After you send the data close your socket
    Code:
    Private Sub Winsock1_Connect()
     Winsock1.SendData txtUserName.Text & "|" & txtPassword.Text & vbCr
     Winsock1.Close
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  31. #31
    Member
    Join Date
    Nov 2009
    Posts
    44

    Re: Sending And Recieving Data via WinSOCK

    Quote Originally Posted by jmsrickland View Post
    Sockets don't close automatically; you have to close the socket. Alway put a Winsoclk1.Close in your connection sub just before you start to make a connection.

    Code:
    Private Sub cmdSend_Click()
     If txtUserName.Text <> "" And txtPassword.Text <> "" Then
       Winsock1.Close
       Winsock1.RemoteHost = "localhost"
       Winsock1.RemotePort = 8001
       Winsock1.Connect
    Else
        MsgBox "Must specify both Username and Password"
    End If
    and yes, what i want the code to do is that when i press send the data be transfferd through th sock and aftter finishing the transfer it close automatically

    so, when i press send again it works without any problems and etc


    After you send the data close your socket
    Code:
    Private Sub Winsock1_Connect()
     Winsock1.SendData txtUserName.Text & "|" & txtPassword.Text & vbCr
     Winsock1.Close
    End Sub
    sorry but are you even trying what you suggesting...

    because none of them is working...

    i did what you suggested before...

    it dont give the error but it dont send anything at all

  32. #32
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Sending And Recieving Data via WinSOCK

    Sorry, I goofed in what I typed. Put the .Close in the _SendComplete event instead of the _Connect event
    Code:
    Private Sub Winsock1_Connect()
     Winsock1.SendData txtUserName.Text & "|" & txtPassword.Text & vbCr
    End Sub
    
    Private Sub Winsock1_SendComplete()
     Winsock1.Close
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  33. #33
    Member
    Join Date
    Nov 2009
    Posts
    44

    Re: Sending And Recieving Data via WinSOCK

    Code:
    ' Client Form
    '
    Private Sub cmdSend_Click()
    If txtUserName.Text <> "" And txtPassword.Text <> "" Then
        Winsock1.Close
        Winsock1.RemoteHost = "localhost"
        Winsock1.RemotePort = 8001
        Winsock1.Connect
    Else
        MsgBox "Must specify both Username and Password"
    End If
    End Sub
    
    Private Sub Winsock1_Connect()
    Winsock1.SendData txtUserName.Text & "|" & txtPassword.Text & vbCr
    End Sub
    
    Private Sub Winsock1_SendComplete()
     Winsock1.Close
    End Sub
    first time data transfer without problem as usuall

    but, now its not giving any error when i click again on send

    but, no data transfering...

    did you try this code?!!

    is it working with you...

  34. #34
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Sending And Recieving Data via WinSOCK

    The code I am giving you is based on what I think you want to do. You have two programs. One is a client and the other is a server. The client connects to the server. Client then sends some data. When data has been sent your client's _SendComplete event is entered. There you close the client's sockets. The server get's the data but does not close his socket. If he does you will need to re-open it again before you press the cmdSend button After client closes his socket and the server does not you now can press the cmdSend button again. If this is what you are doing then the code I posted works.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  35. #35
    Member
    Join Date
    Nov 2009
    Posts
    44

    Re: Sending And Recieving Data via WinSOCK

    Quote Originally Posted by jmsrickland View Post
    The code I am giving you is based on what I think you want to do. You have two programs. One is a client and the other is a server. The client connects to the server. Client then sends some data. When data has been sent your client's _SendComplete event is entered. There you close the client's sockets. The server get's the data but does not close his socket. If he does you will need to re-open it again before you press the cmdSend button After client closes his socket and the server does not you now can press the cmdSend button again. If this is what you are doing then the code I posted works.
    well if you assuming that its working, can you give me a code that you tried it and working perfectly...

    because, i spent hours trying to resolve this problem and i couldnt as i dont have much experiance in VB

  36. #36
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Sending And Recieving Data via WinSOCK

    This is the Client Code

    Code:
    Private Sub cmdSend_Click()
     If txtUserName.Text <> "" And txtPassword.Text <> "" Then
       Winsock1.Close
       Winsock1.RemoteHost = "localhost"
       Winsock1.RemotePort = 8001
       Winsock1.Connect
     Else
       MsgBox "Must specify both Username and Password"
     End If
    End Sub
    
    Private Sub Winsock1_Connect()
     Winsock1.SendData txtUserName.Text & "|" & txtPassword.Text & vbCrLf
    End Sub
    
    Private Sub Winsock1_SendComplete()
     Winsock1.Close
    End Sub
    This is the Server code

    Code:
    Private Sub Form_Load()
     Winsock1.Close
     Winsock1.LocalPort = 8001
     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 s As String
     Winsock1.GetData s
     Text1.Text = Text1.Text & s & vbCrLf
     Form_Load
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  37. #37
    Member
    Join Date
    Nov 2009
    Posts
    44

    Re: Sending And Recieving Data via WinSOCK

    Quote Originally Posted by jmsrickland View Post
    This is the Client Code

    Code:
    Private Sub cmdSend_Click()
     If txtUserName.Text <> "" And txtPassword.Text <> "" Then
       Winsock1.Close
       Winsock1.RemoteHost = "localhost"
       Winsock1.RemotePort = 8001
       Winsock1.Connect
     Else
       MsgBox "Must specify both Username and Password"
     End If
    End Sub
    
    Private Sub Winsock1_Connect()
     Winsock1.SendData txtUserName.Text & "|" & txtPassword.Text & vbCrLf
    End Sub
    
    Private Sub Winsock1_SendComplete()
     Winsock1.Close
    End Sub
    This is the Server code

    Code:
    Private Sub Form_Load()
     Winsock1.Close
     Winsock1.LocalPort = 8001
     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 s As String
     Winsock1.GetData s
     Text1.Text = Text1.Text & s & vbCrLf
     Form_Load
    End Sub
    hmmmmmmmmmm

    you know i just feel that i will explode...

    i ve done everything correctly from before...

    you know what was wrong with my code?!!

    i didnt put Form_Load in Winsock1_DataArrival(....)

    so what was happing that everything was working correctly just the form needed to be reloaded--->what an stupid mistake...

    thanks a lot for your time and your precious help...

  38. #38
    Addicted Member
    Join Date
    Oct 2009
    Location
    Clive, IA in America!!!!
    Posts
    204

    Re: Sending And Recieving Data via WinSOCK

    What if I don't know the remote computer's IP Address. Say, some friend wants to connect to a chat room. Can he transfere his IP Address so that people can talk with him?

    VBNetDude - Thinking Programmatically
    By Silver Seal Software

    Don't forget to mark your thread as "Resolved" using the Thread Tools menu on top. And don't forget to rate the answers that help you the most!

  39. #39
    Junior Member
    Join Date
    Nov 2009
    Posts
    22

    Re: Sending And Recieving Data via WinSOCK

    I want to stream movies from remote server to the client, can anyone help me with the coding, it would be great, thanks

  40. #40
    Addicted Member
    Join Date
    Oct 2009
    Location
    Clive, IA in America!!!!
    Posts
    204

    Re: Sending And Recieving Data via WinSOCK

    Quote Originally Posted by yuvarajv View Post
    I want to stream movies from remote server to the client, can anyone help me with the coding, it would be great, thanks
    That question might be better asked in a new thread.

    VBNetDude - Thinking Programmatically
    By Silver Seal Software

    Don't forget to mark your thread as "Resolved" using the Thread Tools menu on top. And don't forget to rate the answers that help you the most!

Page 1 of 2 12 LastLast

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