This is an extremely simple example of reading from a network socket in C#
I created a basic C# Windows Forms Application project, put a button on the form, and added a click event handler. In the handler, it opens a connection to my Yun, on socket 255 (since that's what the currently loaded sketch is using, you'll be using your different port number.) It then waits for 5 characters to be received, and adds them to a string as they come in. Finally, it closes the socket and pops up a message box displaying what it read.
Not very useful in and of itself, but shows the various steps of opening/reading/closing a socket. Of course, you will want to do something more sophisticated.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
namespace NetTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Local data handling definitions
int count = 5;
int ch;
StringBuilder str = new StringBuilder();
// Declare the TcpClient that manages the connection, and the
// NetworkStream that manages the commuincations over it.
TcpClient tcpClient;
NetworkStream netStrm;
// Open the socket connection, and associate the network stream with the socket.
tcpClient = new TcpClient();
tcpClient.Connect("arduino.local", 255);
netStrm = new NetworkStream(tcpClient.Client);
// Read the fixed number of characters
while (count > 0)
{
// Simple method to make the blocking call non-blocking.
// Not really needed here since the while loop is blocking,
// but added just to show the concept.
if (netStrm.DataAvailable)
{
// Read a character. Normally blocks, but we already know at least
// one byte is available because of the DataAvailable call.
ch = netStrm.ReadByte();
// Add the character to the string
str.Append((char)ch);
// One less character to read
count--;
}
}
// Close the network stream, then the client. Closing the client does NOT close the stream!
netStrm.Close();
tcpClient.Close();
// Pop up a message box showing what was received.
MessageBox.Show(str.ToString(), "Received from socket");
}
}
}
While I had no need to do it in this application, data can also be sent back through the socket using the same NetworkStream object. For example:
public void SendData(byte[] data)
{
if (tcpClient.Connected)
netStrm.Write(data, 0, data.Length);
}
Note that the NetworkStream read/write functions I used are blocking calls. I used DataAvailable() as a workaround in this example (even though it wasn't needed here) which makes it kind-of-sort-of non blocking, but if you need truly asynchronous reads/writes, there is a more complicated method to accomplish that: the NetworkStream documentation has some information on that.
I didn't do that. I already had some serial port code that ran in its own thread, and used delegate functions to send it the form where the incoming data could be caught by an event handler. Since this network socket was basically a replacement for the serial code in an existing application, I just created a copy of the class that encapsulated the serial thread code, and replaced the blocking serial I/O calls with blocking network I/O calls. I did that because it was expedient and minimized the changes to the existing code (it never realized that the underlying transport mechanism had changed.) I don't necessarily recommend going to all that work unless the NetworkStream native asynchronous calls are untenable for your application.