[VB.NET] Problem on socket

Discussion in 'Mixed Languages' started by jcgo16, Jun 11, 2013.

  1. jcgo16

    jcgo16 MDL Junior Member

    Sep 16, 2010
    74
    2
    0
    #1 jcgo16, Jun 11, 2013
    Last edited by a moderator: Apr 20, 2017
    i found on google in some website


    when i try to send this on the client

    Code:
       Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)








    the client will show

    Code:
    woot
    wootwootwootwootwootwoot
    

    but i like to see

    Code:
    woot
    woot
    woot
    woot
    woot
    woot



    whats the problem in the code?

    any ideas?




    SERVER CODE
    Code:
    Imports System.Net.Sockets
    Imports System.Net
    
    Public Class Form1
        Dim serverSocket As Socket
        Dim clientSocket As Socket
        Dim byteData(1023) As Byte
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            serverSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            Dim IpEndPoint As Net.IPEndPoint = New Net.IPEndPoint(IPAddress.Any, 8800)
            serverSocket.Bind(IpEndPoint)
            serverSocket.Listen(50)
            serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing)
        End Sub
        Public Sub Send(ByVal msg As String, ByVal client As Socket)
            Dim sendBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(msg)
            client.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, New System.AsyncCallback(AddressOf OnSend), client)
            Array.Clear(byteData, byteData.GetLowerBound(0), byteData.Length)
        End Sub
        Private Sub OnSend(ByVal ar As IAsyncResult)
            Dim Client As Socket = ar.AsyncState
            Client.EndSend(ar)
        End Sub
        Delegate Sub _OnRecieve(ByVal ar As IAsyncResult)
        Private Sub OnRecieve(ByVal ar As IAsyncResult)
            If InvokeRequired Then
                Invoke(New _OnRecieve(AddressOf OnRecieve), ar)
                Exit Sub
            End If
            Dim client As Socket = ar.AsyncState
            Try
                client.EndReceive(ar)
                Dim bytesRec As Byte() = byteData
                Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(bytesRec)
                clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), clientSocket)
    
            Catch ex As Exception
            End Try
        End Sub
        Private Sub OnAccept(ByVal ar As IAsyncResult)
            clientSocket = serverSocket.EndAccept(ar)
            serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing)
            clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), clientSocket)
            AddClient(clientSocket)
        End Sub
        Delegate Sub _AddClient(ByVal client As Socket)
        Private Sub AddClient(ByVal client As Socket)
            If InvokeRequired Then
                Invoke(New _AddClient(AddressOf AddClient), client)
                Exit Sub
            End If
        End Sub
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
            Send("woot", clientSocket)
        End Sub
    End Class
    

    CLIENT CODE


    Code:
    Imports System.Net.Sockets
    Imports System.Net
    
    
    
    Public Class Form1
     Dim clientSocket As Socket
        Dim byteData(1023) As Byte
     Private Sub OnConnect(ByVal ar As IAsyncResult)
            Try
                clientSocket.EndConnect(ar)
                restarttimer()
                Send("username:" & Environment.UserName, clientSocket)
                clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), clientSocket)
            Catch ex As Exception
                reconnectdata()
            End Try
        End Sub
        Public Sub Send(ByVal msg As String, ByVal client As Socket)
            Dim sendBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(msg)
            client.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnSend), client)
        End Sub
        Private Sub OnSend(ByVal ar As IAsyncResult)
            Dim client As Socket = ar.AsyncState
            client.EndSend(ar)
        End Sub
        Delegate Sub _OnRecieve(ByVal ar As IAsyncResult)
        Private Sub OnRecieve(ByVal ar As IAsyncResult)
            If InvokeRequired Then
                Invoke(New _OnRecieve(AddressOf OnRecieve), ar)
                Exit Sub
            Else
                Dim client As Socket = ar.AsyncState
                Try
                    client.EndReceive(ar)
                    Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(byteData)
                    Read(message)
                    clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), client)
                Catch ex As Exception
                    clientSocket.Disconnect(False)
                    reconnectdata()
                End Try
            End If
        End Sub
    
      Private Sub Read(ByVal msg As String)
                    ListBox1.Items.Add(msg)
                    ListBox1.SelectedIndex = ListBox1.Items.Count - 1
                   Array.Clear(byteData, byteData.GetLowerBound(0), byteData.Length)
    
        End Sub
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     System.Threading.Thread.Sleep(2000)
            clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            Dim ipAddress As IPAddress = ipAddress.Parse(ini.ReadString("ip"))
            Dim ipEndPoint As IPEndPoint = New IPEndPoint(ipAddress, ini.ReadString("port"))
            clientSocket.BeginConnect(ipEndPoint, New AsyncCallback(AddressOf OnConnect), Nothing)
      End Sub
      
      End Class
     
  2. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,100
    120
    #2 Josh Cell, Jun 11, 2013
    Last edited by a moderator: Apr 20, 2017
    Can you try with it?

    Code:
            Send("woot" & vbNewLine, clientSocket)
            Send("woot" & vbNewLine, clientSocket)
            Send("woot" & vbNewLine, clientSocket)
            Send("woot" & vbNewLine, clientSocket)
            Send("woot" & vbNewLine, clientSocket)
            Send("woot" & vbNewLine, clientSocket)
            Send("woot" & vbNewLine, clientSocket)
            Send("woot" & vbNewLine, clientSocket)
            Send("woot" & vbNewLine, clientSocket)
            Send("woot" & vbNewLine, clientSocket)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. jcgo16

    jcgo16 MDL Junior Member

    Sep 16, 2010
    74
    2
    0
    #3 jcgo16, Jun 11, 2013
    Last edited by a moderator: Apr 20, 2017
    (OP)
    still the same

    when i try only these

    Code:
     Send("woot" & vbNewLine, clientSocket)
            Send("woot" & vbNewLine, clientSocket)
    the output is
    Code:
    woot
    woot
    when i add more
    the output will be this

    Code:
    woot
    wootwoot
     
  4. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,100
    120
    #4 Josh Cell, Jun 11, 2013
    Last edited by a moderator: Apr 20, 2017
    Have you tried to send a couple of data in one packet instead of doing it separately?

    Something like that:

    Code:
    Send("woot" & vbNewLine &
    "woot" & vbNewLine &
    "woot" & vbNewLine &
    "woot" & vbNewLine &
    "woot" & vbNewLine &
    "woot" & vbNewLine &
    "woot" & vbNewLine &
    "woot" & vbNewLine, clientSocket)
    I have seen no issue around your code.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. jcgo16

    jcgo16 MDL Junior Member

    Sep 16, 2010
    74
    2
    0
    becuase im trying to send words or commands in new line but still the same

    Send("computerresetcommand",clientsocket)

    Send("shuttingdown",clientsocket)

    Send("messageshowme",clientsocket)
     
  6. jcgo16

    jcgo16 MDL Junior Member

    Sep 16, 2010
    74
    2
    0
    Untitled.jpg

    so what i did is to count what i send from server to client, the server looks good and ok on the sending, but the client only receive 2, i don't know whats the problem but still working and giving clues
     
  7. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,100
    120
    #7 Josh Cell, Jun 11, 2013
    Last edited by a moderator: Apr 20, 2017
    Hmm, ok.

    Try to replace this code by this another one:

    OLD:
    Code:
    Array.Clear(byteData, byteData.GetLowerBound(0), byteData.Length)
    NEW:
    Code:
    byteData = New Byte(1023) { }
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. jcgo16

    jcgo16 MDL Junior Member

    Sep 16, 2010
    74
    2
    0
    #8 jcgo16, Jun 11, 2013
    Last edited by a moderator: Apr 20, 2017
    (OP)
    yup still the same, damn i think theres something wrong with the client i think, i dont know what line, when i send 3 or more the second and up will stacked each other
    woot << 1st send
    wootwootwoot <<< 2nd 3rd and 4th send, i think theres something wrong with this line


    Code:
        Delegate Sub _OnRecieve(ByVal ar As IAsyncResult)
        Private Sub OnRecieve(ByVal ar As IAsyncResult)
            If InvokeRequired Then
                Invoke(New _OnRecieve(AddressOf OnRecieve), ar)
                Exit Sub
            Else
                Try
                    Dim client As Socket = ar.AsyncState
                    client.EndReceive(ar)
                    Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(byteData)
                    Read(message)
                    clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), client)
                Catch ex As Exception
                    clientSocket.Disconnect(False)
                    reconnectdata()
                End Try
            End If
        End Sub
     
  9. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,100
    120
    #9 Josh Cell, Jun 12, 2013
    Last edited by a moderator: Apr 20, 2017
    Try it:

    Code:
      Private Sub Read(ByVal msg As String)
                    ListBox1.Items.Insert(ListBox1.Items.Count, msg)
                    ListBox1.SelectedIndex = ListBox1.Items.Count - 1
                   Array.Clear(byteData, byteData.GetLowerBound(0), byteData.Length)
    
      End Sub
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. BobSheep

    BobSheep MDL Guru

    Apr 19, 2010
    2,326
    1,358
    90
    The problem is probably due to the Nagle Algorithm

    http://support.microsoft.com/kb/214397

     
  11. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    3,501
    3,681
    120
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. jcgo16

    jcgo16 MDL Junior Member

    Sep 16, 2010
    74
    2
    0
    thank you for the help, still getting the same thing, sad to say that there is a limitation :(
     
  13. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    3,501
    3,681
    120
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  14. jcgo16

    jcgo16 MDL Junior Member

    Sep 16, 2010
    74
    2
    0
    yes, not working, kinda worried if some commands might be turn off on my server
     
  15. luciferall

    luciferall MDL Novice

    Oct 6, 2009
    31
    32
    0
    #15 luciferall, Feb 1, 2014
    Last edited by a moderator: Apr 20, 2017

    modify your output box so when it recives the data does a break and adds a new line.

    Private Sub Read(ByVal msg As String)
    If InvokeRequired Then
    Invoke(New _Read(AddressOf Read), msg)
    Exit Sub
    End If
    RichTextBox1.AppendText(msg & vbCrLf)
    RichTextBox1.ScrollToCaret()
    End Sub

    Private Sub SendButton_Click(sender As Object, e As EventArgs) Handles SendButton.Click
    Send(InputTextBox.Text & vbNewLine, clientSocket)
    End Sub

    this will do the trick, it worked for me. don't forget to changes accordingly in the server and client app.
     
  16. jellybelly

    jellybelly MDL Member

    Oct 30, 2009
    149
    28
    10
    #16 jellybelly, Feb 1, 2014
    Last edited by a moderator: Apr 20, 2017
    In the past whenever I was doing client/server stuff I generally always delimited commands just to avoid any situations like this (among others)... maybe there was a better way of doing things but it always worked for me.

    Send("woot|", clientSocket)
    Send("woot|", clientSocket)
    Send("woot|", clientSocket)

    And on the receiving end split by | and go from there.. whether you have one or many commands mushed together on a line it's easy peasy to process and not get any errors.

    Code:
           Dim commands() As String = Split(msg, "|")
            For Each s As String In commands
                If Not String.IsNullOrEmpty(s) Then
                    ' perform action
                End If
            Next
    

    Easy but effective solution.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...