I have to detect if a server is on or off, some people told me to try using the pinging way, and if it's not working I should use TCP connection between me and the server.
Can someone please explain me whats the difference between these, why does the ping way may not work good?
Thank you.
Using TCP would allow you to verify a certain port/service is operating correctly on a host. While ping would just assure that the machine is responding.
A ping also might be blocked by various firewall settings.
It really will depend on your requirements on what your definition of "up" is for your project.
Just responding, or my service hosted on port X is responding when I open communication.
Related
I have a server that i use to run game servers on for my friends and me, and some of the servers are "attack-able" (monsters can destroy our base) so i want the server to be shut down when not in use. Then i was wondering if there was a way to detect if there was an incoming signal (trying to connect to the server) on the given port, so the server can be turned on?
Raw question:
Is there a way to detect, if someone is trying to send a message (or connect) through a specific port in c# (or another language better suited for this action)?
Yes, you have to create a server to listen on that port. The problem you will face is that the server you create to detect incoming connections will need to be shut down so the game server can be turned on. They can't listen on the same port unless they're coded to work together and that likely isn't going to be the case with your game server.
If you want to see if there is any connections in use you can try to list all current TCP connections (assuming server using TCP) and find if there is any alive connection to specific port.
Resmon does this in his "Network" tab, so there must be a way to access it programmatically.
Here is answer describing how to get active TCP connections.
How can I get all the the active TCP connections using .NET Framework (no unmanaged PE import!)?
You probably should monitor server with some intervals because player might lose and reestablish connection, so sample it every 10 seconds or so and if there is no connection for more than few samples - shut down the server.
That's it. It's a newbie question, probably. I'm almost giving up on TcpClient/Listener because of NAT and firewall issues. I wanted to know if theres an alternative. port 80 probably doesn't have to deal with any of these annoying things. I hope the answer is Yes.
What about the SignalR over WebScockets
Yes, you can implement WebSockets in a non-browser application, but it won't solve your problem.
When you try to open a server listening to websocket connections, you will run into the same problem you ran into when you created a normal socket connection: Personal firewall solutions will get in the way and not every router will forward the port you've chosen. Using the well-known port 80 won't help you here. It is quite uncommon for normal consumers to have a webserver running behind their NAT router, so by default no NAT router will forward the port unless specifically configured to do so.
A NAT router will generally prevent any hosts behind it to act as servers unless configured to allow it. When you want the users to not worry about their NAT configuration, both users must act as clients. The only way to do this is by having a central server all players connect to.
While hosting a server is still not free, it has become quite cheap due to cloud-based hosters who will rent you small, virtualized servers for very modest prices. The smallest instance from Amazon EC2 only costs you 2 cents per hour or $14.40 per month. Or you can build your own server from some spare-parts and host it on your own internet connection. When you don't have a static IP address you can use a dynamic nameserver service like no-ip or DynDNS.
Yes you can, but please don't.
I would suggest to change your architecture:
Use a server socket as a main point to handle clients. This server has to have a visible IP/port. In this case you may have clients behind nuts, and do not bother.
What is the best way of reading, changing, and resending tcp communications?
For instance i have a server application that tells a master server it's alive sending a packet over TCP on port 3209. It sends out "I'm alive, my ip is xxx.xxx.xxx.xxx and I have currently 3 clients connected to me." the master server then responds, "Hello xxx.xxx.xxx.xxx i see you there."
Whats the best way of MITM of the server sending its packet to the master server? I would like to be able to change "i'm alive," to something like, "I'm changed," or even "currently 3 clients connected" to "currently 0 clients connected"
Any ideas appreciated, thank you.
There's no need to send out that kind of message.
First of all, TCP is connection oriented, so as long as you have a connection you'll be alive.
You can check if you're still connected by using the TCPClient.Connected property.
Secondly, TCP runs over IP so in the IP header you can find the sender IP (is in the socket properties) so no need to send the IP neither.
You can check the remote IP address by using TCPClient.Socket.RemoteEndPoint property.
Finally, the only thing you might be interested in telling is the "3 clients connected part" wich can be coded as a simple integer transmision. That is, a 4 bytes hearbeat.
Getting in the middle of the connection IS tricky and has nothing to do with communication protocols. And honestly it's a hell of a job, if there's a connection already present you'll have to find a way to make the client or the server drop it and then have the server reestablish it to your MIDM. More though, if the connection is made directly to an IP address you'll have to mess with the router tables somehow for the attact to be succesful, if it's not maybe DNS poisoning will work for you... anyway not an easy task.
Those are only ideas... it depends on the class of network, if you have physical access or not, if the client and the server trust each other (as in if they are your applications) and so on... I assume all this is ethical or educational at least... :)
Have a look at SharpPcap or any other packet capturing/injecting library.
In order to MITM, you'll have to force the client to think you're the server and the server to think you're the client. For that, you'll have to send ICMP packet to both machines as described here. Then, you will capture the packets, modify them and inject them to the network (with the correct MAC address).
Pick a different tool - this is too low level a kind of thing to attempt in C#. This kind of thing is achieved (on Vista and later) using the Windows Filtering Platform
iam using c#,asp.net and iis, i want to simulate slow internet connection on my pc for testing my app.
is it possible i can control bandwidth of iis.
please dont suggest
System.Threading.Thread.Sleep(someDuration);
in c# file.
You can run Fiddler and use its connection throttling to simulate a slow connection.
Note that you'll need to browse to your machine name, not localhost. (localhost. should also work)
Fiddler will do this for you.
You could find or create a proxy that provides file-configurable or UI-modifyable speed controls. The proxy would get the request from the client, make the request to the server, receive the response, then s-l-o-w-l-y send the response to the client. (It would probaly use some sort of Thread.Sleep(x) in between sending each byte of the response to the client.)
After searching a little on the internet, I found no freeware/FOSS on win32 that does the job for arbitrary ports.
I have a server under tests that listens on port 13000 and a client under test that has a configurable sending port. Many tools, designed for web only, make a throttling tunnel from port 80 to something configurable, but my server will never listen on port 80..
Anyone knows something like unix 'tc' command?
This isn't linked to C#, but I would suggest to slow down the system network (or the vm network ..) instead, it should be much easier to play around.
In linux, we can use tc.
sudo tc qdisc add dev wlp3s0 root netem delay 500ms
To turn it off:
sudo tc qdisc del dev wlp3s0 root netem
Source: http://jvns.ca/blog/2017/04/01/slow-down-your-internet-with-tc/
https://linux.die.net/man/8/tc
I have a .NET remoting service listening on a Windows 2003 Server. As far as I know, the Windows Firewall is properly configured to let the information of the Remoting Application go out.
In my PC I have the other part of the communication, trying to execute methods that lives in the Remoting object. In theory, there are not firewalls that can interfere, but this exception is thrown:
SocketExcepcion (Error produced during the connection try, etc.)
I've captured traffic with Microsoft Network Monitor, and I've seen traffic going on between "server and client".
Ideas?. Any help would be very appreciated.
Thanks!
Ok, Windows Remoting (the predecessor to WCF) is quite limiting as there are firewalling issues...basically, it will not work with a firewall, even though you have punched a hole in the firewall, the problem is the NAT'ting of the IP addresses, hence likely, you're getting a connection timed out socket error message. It might help you and re-assure that the problem may easily be remedied by allowing the traffic to be redirected, I have written an article on CodeProject which may help you. What you can do is put the application on the end point where the firewall is, put in the IP address that is facing publicly to the internet, and put the in the IP address that is to be redirected to the remoting server.
Feel free to contact me anytime if you so wish,
Hope this helps,
Best regards,
Tom.