简体   繁体   English

从TCP / IP设备接收数据

[英]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. 我想知道是否可以通过C#连接到该设备并从该设备接收任何输入。 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. 而且,如果可以,那么任何人都可以给我一些指导以帮助我入门或建议应该去哪里看。

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. 我建议您查看“ System.Net ”命名空间。 Using either StreamReader , StreamWriter or as I recommend NetworkStream , you can easily write and read to streams between multiple devices. 使用StreamReaderStreamWriter或我推荐的NetworkStream ,您可以轻松地在多个设备之间写入和读取流。

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.");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM