有没有现成的应用程序可以重播 tcp 数据包?

发布于 2024-12-02 17:25:22 字数 466 浏览 0 评论 0原文

我正在使用一台第三方设备,该设备打开一个 TCP 端口,一次只允许一个连接。如果我的应用程序连接到该端口,则所有其他连接都会被拒绝。

我想找到一个基本上连接到此端口的应用程序,然后允许其他人在不同的端口上连接到它。 从设备端口发送的任何数据都会重新广播到任何连接的客户端。

我知道如何编写这样的应用程序,但似乎其他人已经想到并编写了它并编写了它。共享,我可以避免花时间写它。

基本上代码是:

1)启动一个tcp套接字服务器,绑定到TO_PORT(客户端连接到此) 2)作为客户端连接到DEVICE_IP:DEVICE_PORT 3) 当数据从DEVICE_IP:DEVICE_PORT读入缓冲区时,缓冲区内容被重新发送到每个连接的客户端。 4) 使其成为一个稳定、可运行的程序的其他一切。

这是针对 Windows 的,我希望它不需要安装 java。

我的谷歌技能让我失败了。

有人知道这样的应用程序吗?

I am working with a 3rd party device which opens a tcp port that only allows one connection at a time. If my app connects to the port, all other connections are denied.

I'd like to find an app that basically connects to this port, then allows others to connect to it on a different port.
Any data sent out of the device's port is then rebroadcast to any connected client.

I know how to write such an app, but it seems like it would be something someone else has already thought off and written it & shared, and I could avoid taking the time to write it.

basicaly code would be:

1) start a tcp socket server, binding to TO_PORT (clients connect to this)
2) connect as a client to DEVICE_IP:DEVICE_PORT
3) when data is read into a buffer from DEVICE_IP:DEVICE_PORT, the buffer content is resent to each connected client.
4) everything else that makes it a stable, working program.

This is for windows, and I'd prefer it not require a java install.

My google skills have failed me.

Anyone know of such an app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夢归不見 2024-12-09 17:25:22

我想我会回答我自己的问题。

我自己实施了该解决方案。

我的解决方案的要点:

一个名为 IPClient 的类,它包装了一个 TcpClient 实例,使用调用 TcpClient.BeginConnect、BeginRead 等的异步模型。它有一个计时器,用于在失去连接时重新连接。
这是连接到设备的类。

它的公共接口看起来像这样:

public class IPClient{

    public event EventHandler<MyConnectedArgs> Connected;   
    public event EventHandler<MyDisconnectedArgs>Disconnected;
    public event EventHandler<MyDataReceivedArgs> DataReceived;

    public bool Connect(string address, int port){...}
    public bool Disconnect() {...}
  }    

要打开允许其他客户端连接的端口,我使用了这个库: http://codeproject.com/KB/IP/BasicTcpServer.aspx 并对其进行了一些修改。

它的工作是打开一个端口,接受连接,并执行以下操作:

  1. 在 Connected 处理程序中,在 Disconnected 处理程序中启动侦听端口
  2. ,在 DataReceived 处理程序中停止侦听端口
  3. ,将数据广播到任何连接的客户端。

我将省略其余无聊的细节,但要说这并不“太难”,最终我不得不自己动手。

命令行用法:myapp.exeremote_addrremote_portlisten_portpsuedocode

/我的程序主要思想:

static int Main(string[] args){

  //SetConsoleCtrlHandler(my callback re: ctrl+C,etc)

  //get command line params

  var ipClient = new IPClient();
  var myprovider = MyTcpServiceProvider();
  var server = new TcpServer(myProvider, listenPort);

  ipClient.Connected += (sender, e) => server.Start();
  ipClient.Disconnected += (sender,e) => server.Stop();
  ipClient.DataReceived += (sender,e)=> provider.BroadcastToClients(e.Data);

  ipClient.Connect(remoteAddress,  remotePort);

  //wait for Ctrl+C or program exit
  //shutdown code,etc
  return 0;
}

Guess I'll answer my own question.

I implemented the solution my self.

Key points to my solution:

A class named IPClient which wraps up a TcpClient instance, uses async model of calling TcpClient.BeginConnect, BeginRead, etc. It has a Timer used for reconnecting if it loses connection.
This is the class that connects to the device.

It's public interface would look something like this:

public class IPClient{

    public event EventHandler<MyConnectedArgs> Connected;   
    public event EventHandler<MyDisconnectedArgs>Disconnected;
    public event EventHandler<MyDataReceivedArgs> DataReceived;

    public bool Connect(string address, int port){...}
    public bool Disconnect() {...}
  }    

To open the port that would allow other clients to connect, I used this library: http://codeproject.com/KB/IP/BasicTcpServer.aspx and modified it a bit.

It's job was to open a port, accept connections, and do the following:

  1. in the Connected handler, start the listening port
  2. in the Disconnected handler, stop the listening port
  3. in the DataReceived handler, broadcast the data to any connected clients.

I'll leave out the rest of the boring details, but say it wasn't "too hard", and eventually I just had to roll my own.

command line usage: myapp.exe remote_addr remote_port listen_port

psuedocode/main idea of my program main:

static int Main(string[] args){

  //SetConsoleCtrlHandler(my callback re: ctrl+C,etc)

  //get command line params

  var ipClient = new IPClient();
  var myprovider = MyTcpServiceProvider();
  var server = new TcpServer(myProvider, listenPort);

  ipClient.Connected += (sender, e) => server.Start();
  ipClient.Disconnected += (sender,e) => server.Stop();
  ipClient.DataReceived += (sender,e)=> provider.BroadcastToClients(e.Data);

  ipClient.Connect(remoteAddress,  remotePort);

  //wait for Ctrl+C or program exit
  //shutdown code,etc
  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文