1411
Network / Three general questions about networking in SFML
« on: January 20, 2009, 12:55:45 pm »Quote
Firstly, is it possible to have both a client program and a server program running on the same computer?
Sure. You only specify a port when you setup a listening socket. When you connect to a host, your operating system chooses a port number by itself, mostly just a free one. This means serving and connecting on the same machine is absolutely no problem.
Quote
I'm new to network coding, but I think it has to be something with the ports you use. At present, both my client and server program are using two ports, one for TCP listening and sending and one for UDP listening and sending.
I guess you've an error in your design. Clients normally don't listen on any ports for connections coming from a server. Just open the two ports (TCP and UDP) at the server. Then connect via TCP to the remote host and send an UDP packet to the server. When you're using a router to connect to the internet, it will take care of routing the UDP packets straight to your computer in case the server sends something back. This is called Network Address Translation (NAT).
Quote
HAs this something to do with the fact that I'm using a selector in the client program as well (which is not necessary, however, abit easier)?
Nope, Selectors only tell you if a socket is ready for reading and/or writing, nothing else.
Quote
However, if the client just cuts its connection with the server (e.g. it crashes) the connection will not be closed properly and I've experienced that the selector's timeout-function goes crazy and behaves as if it's set on a very short time (like 0.0...01 seconds). Is it possible to have the TCP socket nature of a constant connection to tackle this problem (which is the reason why I've been using it in the first plce)?
Even if TCP is connection-oriented (that means, when a peer disconnects, it sends a last packet stating that he's going to shutdown the connection), you'll experience problems when a machine just crashes, thus not able to shutdown the connection gracefully. You surely have read something like "UserXYZ timed out." in a game. That occurs because the client wasn't responding in a specific period of time. You can implement such a mechanism by yourself, for example a simple PING/PONG.
The reason why your Selector nearly immediately returns is because he may have detected a connection shutdown -- or better: a problem. As soon as a connection interrupts or a socket gets invalid for any reason, the Selector will return immediately. You should call IsValid() on your Socket object to check if it's valid anymore, see http://www.sfml-dev.org/documentation/1.4/classsf_1_1SocketTCP.htm#134bc86320cc3f4c1ed9075e1c713082 .