Receiving data sent from client machine - c#

I'm working on a project that has a C# app (running 24/7 as a server) on the client's machine). This app needs to send a file as a byte stream via POST to a server I am currently hosting on my home desktop.
Can this file be received by a C# app running on my server, or does it have to be as ASP app/page?
Also, I know how to send a bytestream via POST, but how will I set my server side app to listen for this incoming data?
I have never done something like this, so I'm looking for some pointers to get me started.
Thanks

Depending on what you need to do with the file, and when it's uploaded, there is nothing wrong with using a tiny asp.net app to do this.
If the only way you can get the file to the server is via http POST, why write your own service/daemon to run a listener? Unless you have some reason not to run it in IIS, I would do it there. There is the full stack available that way for authentication and so on if you should happen to need it. Besides, if you have to upload files via http post, I would bet you will end up wanting other methods available via HTTP as well..

You're looking for this guy:
http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx
Using that, you'll create basically a mini HTTP server, listening on some port you specify. You'll then be able to post data to it, and process it accordingly (similar to if the processing code was in an ASPX code-behind).

Assuming your open to sending directly via TCP you can look at this example if you dont wanna go the HTTP way. I personaly like the NetworkStream Class, for sending data over the network painfree.

Related

Get response back from unpingable websites (C# ASP .net mvc)

I'm not a network expert, but for one of my projects, I need to ensure that the website I'm sending the request to is alive. Some websites do not respond to ping; basically, their configuration prevents response to ping requests.
I was trying to Arping instead of pinging websites, but Arping only works on the local network and will not go beyond the network segment (this).
I can download the whole or part of the webpage and confirm if the content is the same as the previous state, but I rather have one more level of confirmation before downloading Html.
Is there any other method that enables the app to get a response back from non-pingable websites outside the network?
Based on common practices you may use ping, telnet and tracert as a client to the requested server (at this point the website or the service you want to connect) and make sure the 3 command are enable to your side. You may also try to access it to your browser.
If its API you may also try to use POSTMAN and call the service.
Goodluck and happy coding :)

Send and receive a file using .NET WebClient or related objects

First of all, I'm completely open to suggestions for alternative strategies, since this is not a space I'm hugely familiar with.
I have what I think is a simple problem. I have a client app and a server app, and I want to send a bunch of files from the client to the server. I don't want to require the user to set up file shares or anything, so my thinking was:
I'll have an app on the "server" using HttpListener to listen to file transfer requests from the client.
I'll have an app on the client using WebClient.UploadFile to initiate the transfers.
Twist: The server is a Mac, and I need to use things that are supported by Mono. I'm not looking to have the server install ASP.NET / IIS / whatever. In fact, the less the user has to do, the better. (The user in this case will be in control of both the client and the server, by the way.)
So, short questions:
Is there a better/easier way than WebClient/HttpListener to send a bunch of files from one machine on a LAN to another? TCP-something? Sockets-something-or-other? (You can see my ignorance.)
Assuming I use HTTP, are there more straightforward ways to download files than using an HttpListener and manually parsing the requests to get rid of headers, etc.?

C# - Send a file over HTTP

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?
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.
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#

How to modify HTTP responses in a different .NET process

I have a standard web server that serve web pages.
I want to monitor traffic on port 80, intercept every http responses, and add/inject an additionnal header to them.
The process will then act like a proxy between the web server and the client.
Could you please give me some pointers? I'm already aware of SharpPCap, but I'm not sure where to start.
Note: I can't rely on the web server, I can't control it or change it's configuration. However I can install any other process on the same machine.
Thanks a million
I think that SharpPCap is an overkill here.
Try:
listen on a port (say 8080)
for each incoming connection, accept and open one to the server (original one, port 80)
pass everything that comes in from the client straight to the server
pass everything that comes from the server back to the client, monitoring the stream and injecting/modifying if needed
I think what you want to do can be done with IIS 7.0 URL Rewrite module instead of rolling your own code.
http://learn.iis.net/page.aspx/711/modifying-http-response-headers/

Question about C#, servers, XML

So I'm working on a project for my internship and have hit a bit of a brick wall. Unfortunately, the only people I know who are qualified to help me at the office are on vacation at the moment, and Google has been unfortunately unhelpful (or my search skills inadequate), so I thought I'd ask here.
The project is basically to make a server to mimic one that the company (which makes phone apps) already has. What I need to do is have one of their apps send a request to my server (I will have to modify the app to do this, but don't know how), and have my server reply with an XML response that the app already knows how to process. (The main purpose is so that we can see how the app responds when the real server sends it an error by simulating it on my server.)
Now, I already have a few sample HTTP requests and their associated XML responses handy, taken from simulations with the app and the real server. The app is written in C#, and currently sends HTTP web requests to the real server's online location, which responds to these HTTP web requests with XML. My server, however, will not have an online location, so the app will have to be modified to work with sockets on a local host.
My questions:
1) My boss said to create an XML file to associate certain requests with certain XML responses, but I have no idea what he means or how to do this. (He said it could also be done with a .ini file.) Does anyone know?
2) Once I have this XML file that can make these associations, how can I incorporate it into my server so that my server can check the request it received against its table of valid requests and figure out which response to send back?
3) How can one modify the app from using HTTP web requests and responses to using sockets?
If you have any questions/clarifications that you need in order to better answer this, please don't hesitate to ask me.
Thanks!
What you're describing is a web service. Unfortunately, his advice to change a setting in an .ini file make it sound like they have a proprietary system for doing this, rather than using a standard ASMX (which requires IIS) or WCF (which can either run in IIS or as a standalone service, which it sounds like is what you'd want) service.
Without more information about what they're using, I don't know that you'll be able to get much help here.
In response to question #3:
HTTP is a protocol that already runs on a specific socket (normally using port 80).
An internet socket is an endpoint that is used to transport data between processes. If you want to run your own protocol, you will need to create a new socket (with TCP or UDP) on a specific port.
This will however require you to create your own client and server in order to exchange data between them.
To get started, here is a very simple client-server example in C# using a custom socket.
Good luck!
Ask your boss if this client communicates with soap, if so then just go to MSDN and find tutorials on implementing an ASMX webservice, follow the tutorial through and you'll have a shell to start with.
First I'd like to say that it sounds like you have some unclear requirements that you should probably clarify with your boss. If you're not exactly sure what he means you should find out because nothing sucks more than having to support someone's creative interpretation of requirements.
1) It sounds like your boss just wants a way to easily change associations for testing without having to rebuild the app so he's asking you to store those associations in an xml/ini file that can easily be modified. In c# you can easily go between XML and DataSet objects so this should be trivial. I would create the data structure in a DataSet first and then use the GetXml method of the DataSet to output the xml format.
2) In .NET you can store objects in Cache and create a Cache Dependency that is a file association. Thus whenever the file is modified the Cache is purged. Whenever your program handles a request it pulls the object from Cache, if the object isn't in Cache then you have a condition block rebuild it from the xml/ini file on disk. I would have that condition block call out to a function that then loads the above mentioned xml format into a dataset that is then stored in the Cache with a Cache Dependency.
3) If you are trying to test an applications i/o, modifying it to use a different transport layer sounds like a bad idea. If the app currently works over HTTP to send requests then just route the HTTP request. I would suspect that the app probably has a configuration somewhere defining the path of the webservice it currently calls out to, once you know what that path is you can either change it, or if that's not possible setup a DNS rule on the server running the app to route it to the location of your application. On windows this is as simple as adding a line to the hosts file.

Categories

Resources