Send and Read from a socket simultaneously - c#

I'm working on a simple proxy server. The problem is browser sometimes re-uses the connection and sends multiple requests on the same connection.
I'm using TcpClient and Stream to make HTTP request to the server. How can I have read and write functions 'separate' from each other, like in separate threads for example. Thanks!

Its called persistent connection. If you don't want the browser to do that, you should not send this header "Connection: Keep-Alive" in your proxy response.
http://en.wikipedia.org/wiki/HTTP_persistent_connection

Related

Server side technology for streaming to async webrequests

I need to write a C# application that will sit and run as a service on a windows server.
This application will be responsible for sending JSON snippets to connected clients.
Clients will be connecting using an Asynchronous WebRequest and I want to Gzip encode the payloads to reduce the size of the packets being sent down the wire to each connected client. The data being sent to each client will differ so I need to manage all the connections as well.
Once a client is connected, they will remain connected for as long as possible, so the server will be sending heartbeats every x number of seconds.
I have not done programming like this since my days back in uni and wondered what the best way of achieving this was?
Can I use Sockets and standard TCP/IP with the WebRequest connection method?
I went with the HttpListener in the end.

Streaming HTTP Post Request

I was wondering if there's any solution to stream HTTP Post Requests from the server to the client on one socket connection that will stay up all the time.
Currently we have a solution to send HTTP Post Requests, but as we do it parallel (async), we are opening a connection per a request, the connection open/close takes several milliseconds which I want to save, so I was wondering if i'll be able, somehow, to keep the connection open and stream all requests through it.

Relay a request from client to Server via Proxy using sockets c#

I have custom tcp server listening on port 5888(dummy port). The proxy server listens for incoming connections. When the proxy receives a HTTP Request, it should relay the same request to a different server.
Eg:
Proxy receives: http://proxyserver.com/mypage.html
Proxy should Relay: http://MainServer.com/mypage.html
The response from the Main Server should be sent directly to the requesting client.
I have used sockets for accepting connections and parsing the raw HTTP Request before relaying the new HTTP request to the main server. This is becoming too complicated, since I have to send the Raw HTTP Request to the main server, get the response and send it to the client machine.The proxy is basically becoming a middle-man. I want the proxy only for the
one side of communication i.e to change the host address to the MainServer and then the main server should take care of sending the response to the requesting client.
How to achieve this ?
HTTP works over a TCP connection initiated from a client to a server. In presence of proxy you need at least two connections - one from client to the proxy, and one from proxy to the server. These two connections are sort of "independent" in a sense that both transmit standard HTTP requests and responses. Proxy here acts as a client to the end server.
What you want involves at least three TCP connections, and requires sending HTTP response on a different connection from the one where initiating HTTP request came on. That is outside of the HTTP proper - the client wouldn't know that it needs a second connection somewhere else, and how to match requests on one connection to responses on the other. The best you can do within HTTP is redirecting to a different server, say, with a special generated URL or something.
Just remember that HTTP response has to come on the same TCP connection as the original request.

How to Receive UDP Concurrent Request?

I'm building a UDP server that handles each incoming request in a separate
thread. The problem is, a UDP client may send out multiple requests
concurrently by using multiple threads. Each thread on the client will wait
for responses from the server. Since UDP is connectionless, a client thread
may receive a mismatching datagram. In this case, does have any
built-in mechanism or pattern that helps a client thread to get the matching response?
(for example send each request from server to specific udp port !!?)
I don't want use queue because it lost concurrently property.
If not, I guess we can build a queue that dispatches responses to
appropriate client threads. However, what if I need to run multiple clients
in different JVMs on the same client machine, and each client will make
requests to the same server?
Use a separate socket for each udp client. That way you have a different ip and port for sending and would receive response on the same i.e. the client which sent the request would only receive it's response (i presume that is what you meant by matching) This should be done automatically unless you share the same socket between threads and use it to send messages to server which seems like a bad idea.
You can set any port in your source (sender port) in client before sending message to server. The server can extract your source port and respond to the same port
e.g.
Client 1 source port:10401 -> server:listening port:2000. Server responds to port 10401.
Client 2 source port:10402 -> server: listening port: 2000. Server responds to port 10402.
If you are wanting to do concurrent connections, make multiple connections. Spawn a new UDP connection on the client, the server will just open a new connection on it's end. Anything sent on connection1 on the client comes in on comes on on connection1 on the server, anything sent on connection2 gets received on connection 2.

HttpWebRequest using Socks5 Proxies

I realize that you can use a Socks5 proxy with WebProxy. That being said, what I am trying to do is this:
Open Up TCPListener -> Create request with HttpWebRequest that sends request to TCPListener using HttpWebRequest.Proxy -> Use either the NetworkStream or Socket from TCPListener to then forward the request through socket connected to a remote Socks5 proxy -> Return the response all the way back up the chain.
I am using Socket.Receive(); to read the bytes from the socket I accept with TCPClient then write them to a socket I've connected to the Socks5 proxy server with. This works great when my requests are just plain HTTP requests. I seem to be having problems requesting SSL pages. They aren't returning responses. Is there something I am missing or maybe even an easier way to do this? At this point I might even consider a library for sending Http Requests with a socks5 proxy.
You're actually doing a man-in-the-middle-attack. HttpWebRequest fails to verify your SSL certificate check and won't accept the connection (aka it knows something fishy is going on).
You can try this to work around the SSL-checks if you really need this working (and understand the consequences)

Categories