简体   繁体   中英

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. 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)

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. For each accepted socket create Session class. In session class connect to Destination with Socket.ConnectAsync. After connection established start reading data from both sockets with 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.

Also if no performance needed, you can use Accept/Connect/Receive methods insted xxxAsync.

Maybe we can use the existing library like DeleGate . We have implemented this in our system. You can install it as a Windows Services.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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