locked
Client - Receiving Data from Server

    Question

  • I am using the Windows.Networking.Sockets.StreamSocket class to connect to my service...I am easily sending data, and the server is initiating a send back to me for me to receive, but the problem is I have been unable to successfully make it work, I am trying to make it so I get a messagebox of the data received to pop up as a simple test, I either get access denied, or it not trying to pop up at all...

    Can anyone help?  Because I am trying to start waiting for data after I 1st connect but in my form load, if I tell it to call the sub or function which is meant to do the work, it stops there, not executing the rest...if I put it last it maybe too late to listen for the data...it's a loop to listen for data, and i'm not even sure how I should be implementing it to begin with.

    Tuesday, April 8, 2014 6:31 PM

Answers

  • You're not really starting to read text - you're simply assigning a streamreader.

    What you need to do is call ReadLineAsync in that function too. It will start a read and return to the caller, something like:  (Sorry, this is in C#... I'm not so up-to-date on VB.NET)

    I'd do this:

    ReadLineAsync().ContinueWith( ()=>
    {

         /// you have new data here to process, a complete line!

    });

    After you process the line, don't forget to call ReadLineAsync again to get the next line (there may be more in the buffer to handle right away, too...)


    Darin R.

    Thursday, April 10, 2014 5:04 PM

All replies

  • Can you post some code? We'd all just be guessing at the problem without it.

    Based on the last paragraph, you're trying to execute a read synchronously, when you actually want to fire off an asynchronous read - but I'm guessing.

    1. Connect

    2. Start async Read

    3. Send request to server

    4 Read returns something.  If the read is complete, start another read and wait.  If all data in, go to 5.

    5. Show Messagebox.  (Go to 2 if you have more data to send/receive if needed....)


    Darin R.

    Tuesday, April 8, 2014 8:53 PM
  • The code is as follows...

    Private Client_Connection As New Windows.Networking.Sockets.StreamSocket
    
    Private Async Sub Page_Loaded(sender As Object, e As RoutedEventArgs)
        Await Client_Connection.ConnectAsync(New Windows.Networking.HostName("IP ADDRESS"), "PORT")
        'Now that I'm connected, the server will send some data to receive.
        StartReceivingText()
    End Sub
    
    Private Sub StartReceivingText()
        Dim a As New IO.StreamReader(Client_Connection.InputStream.AsStreamForRead)
    End Sub

    I don't know what I need to be doing exactly...as you can see the part to start receiving text isn't even close to how it needs to be coded to work...

    I didn't have much code besides what I showed already...I think I might need to use task.run(function() sub()) and such like this, and use the asstreamforread as I got, but I am not sure exactly how to read what I need...the server sends messages in the format of "Insert Message Here."+VbCrLf

    I really need to read line by line exluding the VbCrLf which is a separator for new lines of data in my case...any ideas how I should be doing it?

    Tuesday, April 8, 2014 10:18 PM
  • You're not really starting to read text - you're simply assigning a streamreader.

    What you need to do is call ReadLineAsync in that function too. It will start a read and return to the caller, something like:  (Sorry, this is in C#... I'm not so up-to-date on VB.NET)

    I'd do this:

    ReadLineAsync().ContinueWith( ()=>
    {

         /// you have new data here to process, a complete line!

    });

    After you process the line, don't forget to call ReadLineAsync again to get the next line (there may be more in the buffer to handle right away, too...)


    Darin R.

    Thursday, April 10, 2014 5:04 PM