简体   繁体   English

将数据中继到一个或多个客户端的套接字服务器。 C#

[英]Socket server that relays the data to one or more clients. C#

Hello I am trying to create a proxy server of sorts. 您好,我正在尝试创建各种代理服务器。
The goal is to have one client (to start) connect to the socket server, then have the socket server relay byte for byte to another server. 目标是让一个客户端(启动)连接到套接字服务器,然后让套接字服务器逐字节中继到另一台服务器。 The initial concept would be just a middleman. 最初的概念只是中间人。 I have searched and found code from MSDN to create a socket server, and send data back to the client as well as a new connection. 我已经从MSDN中搜索并找到了代码以创建套接字服务器,并将数据发送回客户端以及新连接。 I have having trouble creating a server that will just relay back and forth, but still be listening for new connections. 我在创建只能来回中继的服务器时遇到麻烦,但仍在侦听新连接。

Any tips or direction would help. 任何提示或指示都会有所帮助。

This article was the closest to what I am trying to do: Is it possible to 'relay' a socket? 本文与我尝试做的最接近: 可以“中继”套接字吗?

Client --> MYSERVER --> Destination (And reverse) 客户端-> MYSERVER->目标(反向)

Client should be able to communicate to the destination without modification, just with my server in the middle. 客户端应该能够与目的地进行通信,而无需进行任何修改,就像我的服务器位于中间一样。

Thanks! 谢谢!

Update: I do have a socket server that reads all bytes on the wire. 更新:我确实有一个套接字服务器,可以读取线路上的所有字节。 I then need to copy those bytes to a new socket and relay back the bytes received. 然后,我需要将那些字节复制到新的套接字中,并中继回接收到的字节。

I dont see what's the problem ... . 我看不出有什么问题...。 Just create server class use Socket.AcceptAsync to accept sockets. 只需创建服务器类,即可使用Socket.AcceptAsync接受套接字。 For each accepted socket create Session class. 为每个接受的套接字创建Session类。 In session class connect to Destination with Socket.ConnectAsync. 在会话类中,使用Socket.ConnectAsync连接到Destination。 After connection established start reading data from both sockets with Socket.ReceiveAsync. 建立连接后,开始使用Socket.ReceiveAsync从两个套接字读取数据。 Once data received you write it to opposite socket. 收到数据后,将其写入对面的套接字。 For simplier approach you can use NetworkStream - Because Socket SendAsync/Send is not quaranteed to send whole data, while NetworkStream BeginWrite/Write is. 对于更简单的方法,您可以使用NetworkStream-因为Socket SendAsync / Send不隔离发送全部数据,而NetworkStream BeginWrite / Write则隔离。

Also if no performance needed, you can use Accept/Connect/Receive methods insted xxxAsync. 另外,如果不需要性能,则可以使用在xxxAsync中插入的Accept / Connect / Receive方法。

Maybe we can use the existing library like DeleGate . 也许我们可以使用现有的库,例如DeleGate We have implemented this in our system. 我们已经在系统中实现了这一点。 You can install it as a Windows Services. 您可以将其安装为Windows服务。

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

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