2

In a server / client environment, I need to send a plain text file over the HTTP protocol.

Typical scenario:
A client joins the server, and the server sends a string telling the client a url to download a text file. The URL would be:

"IP:PORT/folder/folder/file.txt" (where 'IP' and 'PORT' are actual IP's and Ports IE: 127.0.0.1:1234)

I need the server to allow a connection to the files location, and the files location alone. The client is closed source, and so I have no control over its code. It should act like a HTTP server, but only for that file, or at least the subfolder. You should be able to type in the URL into a browsers address and read the contents of the file.

What's the best way of doing this?

1
  • @Oded - I'm not entirely sure on how to read the code I have, but it uses NetworkStream and StreamReader/StreamWriter. I could do with rewriting the entire thing so I know what's going on. Commented Dec 2, 2011 at 13:30

2 Answers 2

3

The easiest way would be to use the HttpListener class.

The documentation contains an example which shows you how to set up the listener and receive a request, but it does not directly cover how to check what file was requested and how to feed that file back to the client. However, both are easy to do and would not take a lot of code.

Sign up to request clarification or add additional context in comments.

4 Comments

Listeners aren't ideal because I don't know what the client sends to listen for. The client is closed source, so I'm not sure how it's formatted.
@ZionFox: Wait, you said "act like HTTP server" right? That's exactly what HttpListener does. If you are not sure what the client sends then how will you ever be able to parse it?
I don't need to parse what the client sends, other than its packet, which I've already sorted out. The URL string is converted to a byte array, which is then sent to the client, which then (I presume) checks for 'keywords' and acts accordingly (downloading the contents of the file). The entire byte array reads as "cfg=IP:PORT/folder/folder/file.txt", so it probably looks for 'cfg=' and downloads the file from the latter substring. When the client connects, it sends a string which I parse and store in a variable, saying that I can send special strings like the above.
@ZionFox: I 'm sorry, but what you are trying to describe is totally unclear. HttpListener will allow the server to listen to an HTTP request from the client (one like GET /folder/folder/file.txt) and respond with the contents of the file. If this is not what you want, please update the question with more details.
1

You can try to use an existing webserver like Apache to serve those files.

Also if this is some kind of learning exercise. You can implement your own simple HTTP server in C#

3 Comments

HTTP web servers are a difficult option, due to the audience that this software would become available to. I predict most users won't know what HTTP is, let alone setting up a server to host files. (I personally run my own web server, with 4 domains, so doing it for me is not a problem, but other people probably won't have this luxury.)
I have included the other option as well
Indeed, I am looking into it, it seems like it may very well work. Thanks ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.