private async Task<string> DoCommand(string command) { StringBuilder strBuilder = new StringBuilder(); using (StreamSocket clientSocket = new StreamSocket()) { await clientSocket.ConnectAsync(_serverHost, _serverPort); using (DataWriter writer = new DataWriter(clientSocket.OutputStream)) { writer.WriteString(command); await writer.StoreAsync(); writer.DetachStream(); } using (DataReader reader = new DataReader(clientSocket.InputStream)) { reader.InputStreamOptions = InputStreamOptions.Partial; await reader.LoadAsync(8192); while (reader.UnconsumedBufferLength > 0) { strBuilder.Append(reader.ReadString(reader.UnconsumedBufferLength)); await reader.LoadAsync(8192); } reader.DetachStream(); } } return (strBuilder.ToString()); }
What's important is the loop that will get the data until the end of the stream. This example is for a server that will reply some data and then close the connection; it's not suitable for an endless stream of data. I use this particular example to connect to CGMiner's api, where I send it a command string and it replies with some data, then close the connection.
Great snippet..
RépondreSupprimerGreat Article
RépondreSupprimerC# Training
Great Article
RépondreSupprimerC# Training
C# Online Training
C-Sharp Training
Dot Net Training in Chennai
.Net Online Training
ASP.NET Training
This is my first visit to your blog, your post made productive reading, thank you. dot net training in chennai
RépondreSupprimerYou post explain everything in detail and it was very interesting to read. Thank you. nata coaching centres in chennai
RépondreSupprimerInformative article, just what I was looking for.seo services chennai
RépondreSupprimerI tried many tutorials on the net, if not all of them and your article is the one that made my day. Thank you very much.
RépondreSupprimer