Several sdl_net questions

Started by
5 comments, last by evillive2 13 years, 3 months ago
Hello,

I just found SDL_net a while ago, and would like to ask a few questions where I can't seem to figure the answers of my own.

Right now, my goal is to make a simple chat program, and possibly a game where you can walk around a bit.

Now, on to the questions:

- TCP? Well, it seemed it was easier to learn, and very suitable for what I wish to do. Is their any reason why UDP should be used instead?

- I wasn't completely sure about the server/client setup. I simply thought of it something like this: Client notices player input, and sends this over to the server (I.e. by sending the ASCII key number). The server changes the player status until the client tells it to stop. Would it work like this?

- Would I be able to run a server for up to 10 people moving/chatting in a world on my own PC? Simply by connecting to my own IP and port?

- If you wish to set up this game through LAN, do you simply use 127.0.0.1 as IP, or what would you use?


Thank you!

Also, Merry Christmas!
Advertisement
None of your questions seem to be specific to SDL_net but rather general networking concepts. This may be better suited for the multiplayer and network programming forum. My first recommendation is to read the FAQ in that particular forum however in the spirit of helping here goes:

Quote:TCP? Well, it seemed it was easier to learn, and very suitable for what I wish to do. Is their any reason why UDP should be used instead?
TCP is probably a good idea to start with - especially for chat and simple messaging. UDP becomes more useful when you are sending rapid updates and the speed in which they are delivered is more important than getting every single packet. That being said - you can use TCP for the chat and UDP for the movement at the same time but at this stage it is probably easier to just stick with TCP until you get some of the basics down.

Quote:I wasn't completely sure about the server/client setup. I simply thought of it something like this: Client notices player input, and sends this over to the server (I.e. by sending the ASCII key number). The server changes the player status until the client tells it to stop. Would it work like this?
Think of how you would have a conversation over a telephone - the concept is very similar. In a chat program the client sends a message and the server simply rebroadcasts that message to other clients in the same room. There are many examples on the net of simple chat servers and clients. Keep in mind that you won't always want to implicitly trust content from the client - i.e. the client says it walked somewhere it shouldn't be able to for example.

Quote:Would I be able to run a server for up to 10 people moving/chatting in a world on my own PC? Simply by connecting to my own IP and port?
Depending on what you mean by moving the likely hood of this being possible is very high. Simple chat servers are very small programs and very light on both CPU and bandwidth providing the content is simple text based messages.

Quote:If you wish to set up this game through LAN, do you simply use 127.0.0.1 as IP, or what would you use?
On a LAN you use the IP assigned to your computer. This can be found simply on Windows by opening a command window and typing "ipconfig" or on Mac/Linux by opening a terminal window and typing "ifconfig". You won't use 127.0.0.1 from other PCs as this is just an alias nearly all PCs have which "loops back" to itself. Most home routers will give you a private IP address or if you are like my parents who have an old PC hooked up directly to their cable or DSL modem you may have a public IP in which case you will have a slightly harder time having a LAN party :)

If you want to allow others to play from over the internet this is a bit more involved with setting up your home router (again I am assuming you have one) to allow port forwarding which may also make your computer more vulnerable to outside attackers and is beyond the scope of what I can post here. I will point you to a site whatismyip.com that will tell you the public internet address of your current PC just in case you find what you are looking for and are missing this piece of information.
Evillive2
He didn't answer your question completely.

You're treading into complex territory with this, I want you to know.



The first and foremost thing is that TCP should be used for "vital" messages -- one that absolutely must arrive at their destination or something terrible will happen (example: a player's HP changing)
UDP on the other hand is better for things that are unimportant and frequently updated. An example of this might be the status of an item that changes over time under some conditions (example: armor fatigue while in battle)

The second thing is that the server must absolutely not take anything that the client says at face value. The server must have a duplicate of everything that the client has. In the case of the chat server, this type of security is not important. In the case of an MMORPG; this is the most important security issue besides protecting your database and actual server computer from intrusion!

Depending on your threading/network model; you could probably hold well over a hundred clients from your personal PC with minimal lag. If you expect levels to get that high, I must defer you from using SDL_net for the server and recommend you look into boost.asio or libevent; which are cross-platform libraries for handling such excessive I/O in the most efficient manner possible on the target system (to keep it simple for you).
In the case of 10 people; you should have no issue no matter what your threading/network model is.

The above post was correct about how to get the IP to be used. 127.0.0.1 is just to loop back to your own PC.
He was also correct with his statement about opening it up to the internet. If you intend to make it available to people outside your LAN; I recommend you use a VPS instead. Most VPS hosting services will offer you something decent at less than $50/mo; so it's affordable for anybody with a job.

The first and foremost thing is that TCP should be used for "vital" messages -- one that absolutely must arrive at their destination or something terrible will happen (example: a player's HP changing)
UDP on the other hand is better for things that are unimportant and frequently updated. An example of this might be the status of an item that changes over time under some conditions (example: armor fatigue while in battle)

Nearly. I would say that a reliable protocol should be used for "vital" messages, and an unreliable protocol can be used for others. You can mix TCP and UDP in a single application, but it can be complicated and can interact poorly with NAT.


TCP? Well, it seemed it was easier to learn, and very suitable for what I wish to do. Is their any reason why UDP should be used instead?<br /> The main thing to understand about UDP is that it isn&#39;t really &quot;faster&quot;, it just behaves different when it encounters packet loss, packet reordering or packet duplication. UDP ignores all these events, and delivers every packet it can to the application as soon as it arrives. TCP will buffer packets that arrive out of order, it will drop duplicates and it will wait until all missing packets are arrived before it allows the application to read them. TCP also might be buffered at the sending side, e.g. due to the sliding window size and Nagle&#39;s algorithm. But once they are on the wire TCP segments and UDP packets get to their destinations equally quick (in the absence of network shaping).<br /> <br /> This manifests itself as an apparent speed difference, but one must remember that if you implement full reliability on top of UDP then you generally end up doing most of the same things as TCP. If you end up doing all of them, then your application will probably be at most as fast as TCP, possibly slower if it overloads the network.<br /> <br /> The real &quot;win&quot; of UDP for games is being able to specify, on a per message basis, whether it is necessary for it to be ordered or reliable. So entity creation and destruction messages might be ordered and reliable, but entity updates might be neither, or partially ordered (e.g. drop old updates, but don&#39;t buffer ones that skip a few updates). The point here is that there is a good bit of work that must go in to your application if you intend to get the benefits of UDP.<br /> <br /> For an application where reliability is paramount, such as chat, use TCP.
I personally am favoured with ENet. It uses UDP with it's own reliability algorithm, so you get the speed of UDP with the reliability of TCP. A much better performance, according to the developer.
But it also has a very simple and consistent API, which is the main reason I use it.

http://enet.bespin.org/
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
I have heard good things about Enet; but I think you're just as well using just TCP in most situations.
I won't suggest one library over another at this point since the OP requires a more basic understanding of network programming and the fundamental differences between UDP and TCP. I highly suggest that the OP do some more research before a) choosing TCP/UDP b ) choosing a networking library. I also very highly recommend starting at the Multiplayer and network FAQ so they can get the whole picture.
Evillive2

This topic is closed to new replies.

Advertisement