Hagakure87
Hagakure87

Reputation: 137

Receiving data from a TCP/IP device

Where I work we currently use a clock-in system that uses a hand scanner connected to our network. I want to know if there is a way via C# to connect to this device and receive any input from this device. Or, for that matter any other input device connected in a similar way. And, if so, if anyone can give me some pointers to get me started, or suggest where to look.

Upvotes: 1

Views: 7688

Answers (1)

Rasmus Søborg
Rasmus Søborg

Reputation: 3695

if anyone can give me some pointers to get me started, or where to look.

I would recommend that you look into the "System.Net" namespace. Using either StreamReader, StreamWriter or as I recommend NetworkStream, you can easily write and read to streams between multiple devices.

Take a look at the following example for how to host data and connect to the host for receiving data.

Hosting Data (Server):

static string ReadData(NetworkStream network)
{
    string Output = string.Empty;
    byte[] bReads = new byte[1024];
    int ReadAmount = 0;

    while (network.DataAvailable)
    {
        ReadAmount = network.Read(bReads, 0, bReads.Length);
        Output += string.Format("{0}", Encoding.UTF8.GetString(
            bReads, 0, ReadAmount));
    }
    return Output;
}

static void WriteData(NetworkStream stream, string cmd)
{
    stream.Write(Encoding.UTF8.GetBytes(cmd), 0,
    Encoding.UTF8.GetBytes(cmd).Length);
}

static void Main(string[] args)
{
    List<TcpClient> clients = new List<TcpClient>();
    TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Any, 1337));
    //listener.ExclusiveAddressUse = true; // One client only?
    listener.Start();
    Console.WriteLine("Server booted");

    Func<TcpClient, bool> SendMessage = (TcpClient client) => { 
        WriteData(client.GetStream(), "Responeded to client");
        return true;
    };

    while (true)
    {
        if (listener.Pending()) {
            clients.Add(listener.AcceptTcpClient());
        }

        foreach (TcpClient client in clients) {
            if (ReadData(client.GetStream()) != string.Empty) {
                Console.WriteLine("Request from client");
                SendMessage(client);
             }
        }
    }
}

Now the client would then use the following method to send the request:

static string ReadData(NetworkStream network)
{
    string Output = string.Empty;
    byte[] bReads = new byte[1024];
    int ReadAmount = 0;

    while (network.DataAvailable)
    {
        ReadAmount = network.Read(bReads, 0, bReads.Length);

        Output += string.Format("{0}", Encoding.UTF8.GetString(
                bReads, 0, ReadAmount));
    }
    return Output;
}

static void WriteData(NetworkStream stream, string cmd)
{
    stream.Write(Encoding.UTF8.GetBytes(cmd), 0,
                Encoding.UTF8.GetBytes(cmd).Length);
}

static void Main(string[] args)
{
    TcpClient client = new TcpClient();
    client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1337));
    while (!client.Connected) { } // Wait for connection

    WriteData(client.GetStream(), "Send to server");
    while (true) {
        NetworkStream strm = client.GetStream();
        if (ReadData(strm) != string.Empty) {
            Console.WriteLine("Recieved data from server.");
        }
    }
}

Upvotes: 2

Related Questions