Using TCP and UDP at Once

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Using TCP and UDP at Once

Post by Vincent_M »

Is it a good idea to use TCP and UDP in the same application. For example, think of an online deathmatch game. UDP could be used to update positions and animation states while TCP could send crucial data such as scoring and other stuff.

If I send UDP messages every frame, and a packet gets lost along the way, would that packet be gone for good, or is there a possibility that it will show up later on after fresh data has been received and processed?

Another thing, is it a good idea to send multiple messages at once to a client instead of sending all the data in one shot? For example, send all character positions in one packet and then all the weapon existence states on the map in another. I would think sending it all at once would be a good way to go, but then again, I thought rendering a 128x128 image in one call would be much faster than rendering that same 128x128-pixel image in 64x64-pixel sections.
PhonicUK
Posts: 3
Joined: Fri Mar 07, 2008 3:10 am

Post by PhonicUK »

Mixing UDP and TCP is usually how its done. TCP for stuff that doesn't need to be fast and instant like login and scores, UDP for things like movement and position.

With UDP there is a risk that the packet will either be lost forever, or will show up again later than expected. So you need a means of dumping late-show packets.

Also with UDP you can't guarntee that packets will arrive in the same order they where transmitted (Part of the reason why its so fast) so its generally a good idea to make sure all of your data can fit inside 1 packet.
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

Ok, I'll use both protocols then. I have a simple TCP game setup between two PSPs right now. They are run through my 2WIRE router/access-point. Does that mean that my application will be able to run through the internet as well as long as the port I use is free?

Also, I am using one port in my TCP game. Is it good to use one port for the server PSP and have the other PSPs that act as clients use other different ports?
PhonicUK
Posts: 3
Joined: Fri Mar 07, 2008 3:10 am

Post by PhonicUK »

Yes as long as the port is free, your app should be able to connect to the outside world.

Stick to as few ports as you can. Makes things much eaiser in the long run.
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Only difference is that you won't use the local IP (192.168.x.x or whatever) but the NAT traversed IP.
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

So, how to do I obtain the NAT software-wise since the router will give that to me?
ttotto
Posts: 8
Joined: Mon Nov 26, 2007 2:39 am

Post by ttotto »

I think you need to use Net Resovling.
look here for more info.
http://psp.jim.sh/pspsdk-doc/pspnet__re ... 891d144884

This function:
sceNetResolverStartNtoA (int rid, const char *hostname, struct in_addr *addr, unsigned int timeout, int retry);

Can convert lets say the DNS Name is "www.google.com" it will return an IP address "74.125.19.104"

There is also an example in the SDK.
Hope That Helps!
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

I don't have a domain name though. I need my PSP to be converted to an external IP address though, wouldn't I because that is what I am using as the game server?
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

If you expect people outside your network to be able to connect to your PSP behind a NAT, you'll have to set up some form of forwarding on the router as well, or use one of the many forms of NAT traversal (STUN, etc). This is pretty basic networking stuff, there's nothing specific to the PSP here.
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

Does the XBOX360 and PS3 have to forward ports when they go online? I know that the XBOX360 checks the NAT when it attempts to connect to XBOX LIVE.

It won't just be my PSP people will be conecting to. The player of the game can choose to host (be the server) or join (be the client) the game.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Ports are forwarded by the ROUTER, not the computer/console (unless you're using a computer as a router). LinkSys puts the port forwarding settings on the Applications and Gaming tab.
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

So, then all I would have to do is use my external address instead of my local one when I go to bind my client socket information?
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

No, you'll need to understand NAT a lot better first and figure out which of many methods will suit you (static NAT, uPnP, STUN, etc)
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

Ok, I'll research them. How do I use them in programming in programming? Do I have to write something from scratch, or are there options with the socket library I can use to use NAT? I've been trying to figure out how to use NAT, but all I know right now is that it is something that the router does...
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

NAT is a networking concept. It's not something you "use" from your program. You'll need to understand that before you can figure out ways to deal with the issues it causes.
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

Ok, I knew it was a hardware thing that the router handles from a little research, but I thought there could possibly be a way of checking it with specific functions or something like that... I don't know, but I'll look into it more. NAT sounds like an algorithm, and that there are different algorithms out there such as uPnp and STUN, or something like that...
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

The way you forward a port is to log into the modem/router (usually something like 192.168.1.1 or 192.168.0.1), go to the page that controls the ports, and tell it which port to forward to which local IP address. Just use your favorite browser to do this.

For example, I type 192.168.1.1 into the address field of Firefox and I get the login for my LinkSys router. After entering the login name and password, I can then go to the Applications and Gaming tab where the first sub-tab is Port Range Forward. I tell it a port or range of ports, whether to forward TCP, UDP, or both, and the local IP address to forward those port(s) to. Click Save Settings at the bottom of the page and the ports are now forwarded where I want them to go.
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

So then I won't have to mess with NAT or anything like that? How would the people across the internet be able to access others' games they have online? Would they do it the same way they would if it was working locally, but instead using my external address?
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Vincent_M wrote:So then I won't have to mess with NAT or anything like that? How would the people across the internet be able to access others' games they have online? Would they do it the same way they would if it was working locally, but instead using my external address?
You shouldn't need to mess with the NAT at all. I certainly haven't. Port forwarding has one main purpose - to get external packets directed to a specific port to one particular computer/console. Think about it a moment: here's my setup...

Router --- DSL
|--- Computer 1
|--- Computer 2
|--- Computer 3
|--- Computer 4
|
wifi to three PSPs

If you get the IP address for any of the computers or PSPs locally, it shows the local address. If you get the IP from something like WhatIsMyIP.com, they all show the same IP address (since they share the same connection). So how is someone in Taiwan supposed to connect to computer 4 for a Death Match? They use the single IP address my connection is assigned, and direct the packets to a specific port. I must then make sure that port is forwarded to computer 4.

If I were Death Matching between computer 2 and one of the PSPs, I would use the local IP addresses and therefore not need to forward a port since all local addresses are unique. So as I said above - port forwarding is to make sure packets not from local machines can get to the correct local machine. Packets from local machines can get to another local machine without that.
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

Ok, so I just need to forward a port so that my PSP can contact all the other PSPs out there in the network. However, what if I had 3 PSPs running locally in the same game playing with 5 other PSPs located all over the world? Would I need to forward 3 ports: one for each PSP then?
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Vincent_M wrote:Ok, so I just need to forward a port so that my PSP can contact all the other PSPs out there in the network. However, what if I had 3 PSPs running locally in the same game playing with 5 other PSPs located all over the world? Would I need to forward 3 ports: one for each PSP then?
Haven't every done that myself, so I can't sure for certain. Might be that the router can handle that... might be you'll need more ports and forward them accordingly. I'd google for that iffen I were you. :)

Can't rightly say I know ANYONE who's done multiple locals vs multiple externals. It's always multiple locals, period, or one local, period.
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

Can't rightly say I know ANYONE who's done multiple locals vs multiple externals. It's always multiple locals, period, or one local, period.
Yeah, that's how I would think anyone would do it too. After all, there's only one console hooked up to an access-point usually. I'd only want to have multiple PSPs hook up locally with multiple PSPs outside of the my network just to see how performance works between 8 PSPs. Having a good deal of them close to me would be good because I can run tests quickly without having to rely on a bunch of other people while still being able to test how interaction works with the outside world. On the plus side, I do have 3 PSPs to work with right now, and I might be able to borrow 2 so that I can have a 5-player test. :p That'd be tight!
Post Reply