Data receive through socket

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    Data receive through socket

    Hey,
    Happy New Year to all of you!

    I have one query,
    I am trying to send and receive the data through sockets.
    When I send it for the first time I get the data from the server.But from second time onwards I dont receive anything. Any idea?

    Shweta
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Here is an article that may help:
    VB.NET TCP Client - Server Socket Communications

    Otherwise you may want to post code. Any error message?

    Comment

    • Rathorevivek
      New Member
      • Dec 2006
      • 17

      #3
      Hi Shweta,

      I am not so familiar with the Socket programming.
      But i have found follwing link which might help you.

      http://www.codeproject .com/cs/internet/mailclient.asp

      Best Regards,
      Vivek Rathore.

      Comment

      • shweta123
        Recognized Expert Contributor
        • Nov 2006
        • 692

        #4
        Thanks for the help!
        I am stuck up at some other place.These 3 functions I am using for sending and receiving the data.

        Private Sub sockDataArrival (ByVal ar As IAsyncResult)
        Dim state As StateObject = CType(ar.AsyncS tate, StateObject)
        'Dim state As New StateObject
        Dim client As Socket = state.workSocke t
        Dim bytesRead As Integer

        Try
        bytesRead = sockPOP3.EndRec eive(ar)
        Catch
        Exit Sub
        End Try

        Try
        Dim Data() As Byte = state.buffer
        strmsg = Encoding.ASCII. GetString(Data)

        If bytesRead = 0 Then
        client.Shutdown (SocketShutdown .Both)
        client.Close()
        RaiseEvent onDisconnect()
        Exit Sub
        End If

        ReDim state.buffer(32 767)
        client.BeginRec eive(state.buff er, 0, state.BufferSiz e, 0, AddressOf sockDataArrival , state)
        RaiseEvent onDataArrival(D ata, bytesRead)
        Catch
        RaiseEvent onError(Err.Des cription)
        Exit Sub
        End Try
        End Sub

        --------------------------------------------------------------------------

        Public Sub SendData(ByVal Data() As Byte)
        Try
        Dim byteData As Byte() = Data
        sockPOP3.BeginS end(byteData, 0, byteData.Length , 0, AddressOf sockSendEnd, sockPOP3)
        Catch
        RaiseEvent onError(Err.Des cription)
        Exit Sub
        End Try
        End Sub

        ------------------------------------------------------------------------------------------
        Private Sub sockSendEnd(ByV al ar As IAsyncResult)
        Try
        Dim sockPOP3 As Socket = CType(ar.AsyncS tate, Socket)
        Dim bytesSent As Integer = sockPOP3.EndSen d(ar)
        ' Dim state As New StateObject
        ' state.workSocke t = sockPOP3
        ' sockPOP3.BeginR eceive(state.bu ffer, 0, state.BufferSiz e, 0, AddressOf sockDataArrival , state)
        RaiseEvent onSendComplete( bytesSent)
        Dim state As New StateObject
        state.workSocke t = sockPOP3
        sockPOP3.BeginR eceive(state.bu ffer, 0, state.BufferSiz e, 0, AddressOf sockDataArrival , state)
        'Call sockDataArrival (state.buffer)
        RaiseEvent onDataArrival(s tate.buffer, state.BufferSiz e)
        Catch
        RaiseEvent onError(Err.Des cription)
        Exit Sub
        End Try
        End Sub


        My Problem is that when I send data for the first time I receive it properly.But next time onwards It does not hit sockDataArrival function at all and I dont get any error also.
        Any Idea?

        Shweta

        Comment

        • Rathorevivek
          New Member
          • Dec 2006
          • 17

          #5
          I think the problem lies here:

          You are declaring Dim client as socket in your first function i.e sockDataArrival .
          But in same function rather than using client as socket you are using sockPOP3.
          else in other function you are consistent with sockPOP3 declared as socket.

          Vivek Rathore.


          Originally posted by shweta123
          Thanks for the help!
          I am stuck up at some other place.These 3 functions I am using for sending and receiving the data.

          Private Sub sockDataArrival (ByVal ar As IAsyncResult)
          Dim state As StateObject = CType(ar.AsyncS tate, StateObject)
          'Dim state As New StateObject
          Dim client As Socket = state.workSocke t
          Dim bytesRead As Integer

          Try
          bytesRead = sockPOP3.EndRec eive(ar)
          Catch
          Exit Sub
          End Try

          Try
          Dim Data() As Byte = state.buffer
          strmsg = Encoding.ASCII. GetString(Data)

          If bytesRead = 0 Then
          client.Shutdown (SocketShutdown .Both)
          client.Close()
          RaiseEvent onDisconnect()
          Exit Sub
          End If

          ReDim state.buffer(32 767)
          client.BeginRec eive(state.buff er, 0, state.BufferSiz e, 0, AddressOf sockDataArrival , state)
          RaiseEvent onDataArrival(D ata, bytesRead)
          Catch
          RaiseEvent onError(Err.Des cription)
          Exit Sub
          End Try
          End Sub

          --------------------------------------------------------------------------

          Public Sub SendData(ByVal Data() As Byte)
          Try
          Dim byteData As Byte() = Data
          sockPOP3.BeginS end(byteData, 0, byteData.Length , 0, AddressOf sockSendEnd, sockPOP3)
          Catch
          RaiseEvent onError(Err.Des cription)
          Exit Sub
          End Try
          End Sub

          ------------------------------------------------------------------------------------------
          Private Sub sockSendEnd(ByV al ar As IAsyncResult)
          Try
          Dim sockPOP3 As Socket = CType(ar.AsyncS tate, Socket)
          Dim bytesSent As Integer = sockPOP3.EndSen d(ar)
          ' Dim state As New StateObject
          ' state.workSocke t = sockPOP3
          ' sockPOP3.BeginR eceive(state.bu ffer, 0, state.BufferSiz e, 0, AddressOf sockDataArrival , state)
          RaiseEvent onSendComplete( bytesSent)
          Dim state As New StateObject
          state.workSocke t = sockPOP3
          sockPOP3.BeginR eceive(state.bu ffer, 0, state.BufferSiz e, 0, AddressOf sockDataArrival , state)
          'Call sockDataArrival (state.buffer)
          RaiseEvent onDataArrival(s tate.buffer, state.BufferSiz e)
          Catch
          RaiseEvent onError(Err.Des cription)
          Exit Sub
          End Try
          End Sub


          My Problem is that when I send data for the first time I receive it properly.But next time onwards It does not hit sockDataArrival function at all and I dont get any error also.
          Any Idea?

          Shweta

          Comment

          Working...