【问题标题】:Receiving data from a TCP/IP device从 TCP/IP 设备接收数据
【发布时间】:2012-08-09 11:16:33
【问题描述】:

在我工作的地方,我们目前使用打卡系统,该系统使用连接到我们网络的手持扫描仪。我想知道是否有办法通过 C# 连接到该设备并接收来自该设备的任何输入。或者,就此而言,以类似方式连接的任何其他输入设备。而且,如果是这样,是否有人可以给我一些指导以帮助我入门,或者建议在哪里寻找。

【问题讨论】:

  • 查看设备的 API 文档将是最好的地方......我们不能告诉你。在最基本的层面上,SocketNetworkStreamTcpClient 可能有用 - 但如果不了解 API 就很难说。
  • 能否连接到设备取决于设备本身。它是否附带任何文档?
  • 同意,除非您想花很长时间猜测,否则您可能需要协议文件或等效文件。
  • 你试过什么?设备是否发送到给定的 IP 或者它是否更复杂并进行广播以查找侦听器等?你试过什么?

标签: c# tcp ip device


【解决方案1】:

如果有人可以给我一些指导让我开始,或者去哪里看。

我建议您查看“System.Net”命名空间。使用StreamReaderStreamWriter 或我推荐的NetworkStream,您可以轻松地在多个设备之间写入和读取流。

查看以下示例,了解如何托管数据并连接到主机以接收数据。

托管数据(服务器):

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

现在客户端将使用以下方法发送请求:

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

【讨论】:

    猜你喜欢
    • 2017-09-18
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 2012-03-05
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多