- Home /
TcpListener with Network Manager
I have a game that uses the Unity Network system that uses their server to set up the game connections. This works fine.
I have a bunch of specific data that I need to transfer to each client when they connect. It is formated in such a way that using the RCP system would be both wasteful and cumbersome because the only data type you can send that would be even close is the Vector3. This would work, but as I said be very wasteful and cumbersome to pack my data into a bunch of Vector3s.
So I have created a TcpListener on the server and a TcpClient on the client. This works great except that you have to know the IP address and it has to be on the same network or be a public IP address.
I am guessing that my users are going to be behind NAT in most cases. The Unity network servers get around that, but as far as I can tell only for the game session that is managed by the Network plumbing.
I can get the IP address that is used for the NAT stuff and it looks like it is on the Unity servers somewhere with a special PORT.
My question is whether there is any way that I can set up a connection via the Unity server that I can use the IP address and my own assigned port that I can attach my TcpListener to that will accept incoming TcpClient requests?
Answer by Hotshot10101 · Apr 17, 2013 at 01:41 PM
After much research I have not found a way to make this happen. The Unity networking stuff seems to not allow direct TCP connections.
There should be no problem to handle direct connections manually with TcpListener. However it's just plain C# socket stuff, so there's no NAT punchthrough. Also it doesn't work in a webplayer-build because a webplayer can never open a listening port for security reasons. On other platforms that should be no problem.
The NAT punchthrough stuff comes with the Raknet network engine which is built into unity. This also requires an external server which is reachable from all users. Unity hosts such a server mainly for development testing. You can't use this technology for your custom connections. NAT punchthrough is quite complex and it's never guaranteed to work on all clients.
You can also send a byte array via RPC, however it has to be the only parameter. I've never tried this myself but i read several times that this works. You cah use a BinaryFormatter to serialize / deserialize arbitrary data.
The NAT issue is what I meant when I said that what I wanted to do could not be done. I was hoping to be able to use the NAT punch through that Unity is doing with my own socket.
This is why I marked it as not being able to do it as the answer.
I will probably just use a byte array as the only parameter method and use the Unity methods.
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613092229im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to do a TCP Server? 4 Answers
Scriptable Objects in networks. 0 Answers
[Network] Authorative Server for MMO 0 Answers
Download pictures and store them temporarily 1 Answer
Unity Master Server Connect to own Server won't work 0 Answers