Communicating with program using raw socket

Hi, guys!

Is it possible for Unity to communicate with an application that is using raw socket?
Ex.: Use Unity’s NetworkTransport to send udp packets to an application in c/c++/python waiting for udp packets?

I know I can just import C# sockets library in unity, but is there a way to do it using unity’s networking features?

Thanks in advance!

1 Like

I believe the Unity LLAPI can do this. There’s very little documentation on this subsystem so far though. Is this a web socket by chance, you mentioned C++ so I’m assuming a wininet socket or jet socket?

1 Like

Hi, Freaky!
Thanks for your intereset in helping me!

The application is made in python, with raw sockets.

Unity LLAPI (accessed by the class NetworkTransport) requires us to first do a .connect() with the other side, and only then start sending and receiving data. Even though the documention says it’s an udp socket (which can be used in unrealiable mode, theoretically not needing anything else from unity) it still expects both sides to be able to respond to the .connect() command. It’s a type of agreement needed from unity, probably to guarantee both sides will communicate properly and indentify if one of them goes down.

So, I tried a lot already and I had no success. If I put the raw socket application to print everything it is receiving, as soon as I try to do a connect() to it from unity, it starts priting some block of byte repeatedly and at some point it stops and the unity application prints a “disconnected”. My guess is that the application expects a good response when trying to .connect() and can timeout if the other side doesn’t respond properly.

My motivation to make this work is: I plan to code the server myself and I would like a channel of communication close to raw sockets offered by unity, avoiding having to code different clients for different systems.

Another approach that would work me is if I could compile a unity game with only the server logic and run it in a Ubuntu Server (or any unix server). The problem is, the server cannot have a desktop GUI, it should be command line only.
Is it possible to compile unity application for unity with only server logic? (no 3d, no 2d, no game, just scripts responding to requests?)

You don’t have to compile it a special way, you take your compiled app and run it with the switch for batchmode and nographics

See this link:

1 Like

Thanks very much, Freaky!!! Thanks a lot, really!!!

If someone else sees this post or if even you have more to share, Freaky, I’m still interested on communication with applications that are using raw sockets, because this gives me ability to choose between a lot of frameworks to implement my server rather than using unity apps.

So, you absolutely should be able to communicate across a raw socket, but I think you’re actually using a raw WebSocket from the sounds of your instanciation flow.

Usually the order of operation is like this.
1.) Server opens a socket for listening, and a TCP channel to initiate connection
2.) Client initiates a connection on TCP and the server sends back a socket instruction
3.) Client initiates using the instructions, an outgoing socket to the server, which then establishes a raw persistent connection over sockets using the given protocol, encryption, and reliability instructions.

It’s sounds like you’ve got step 1 done, the server is listening, but it sounds like you’re trying to initiate the connection on the socket, rather than over TCP to get the instructions back from the server.

It’s the first flow in this documentation (not the WebGL part):

http://docs.unity3d.com/Manual/UNetUsingTransport.html
On the connect part I use the ip address and port of the raw socket from the python application.

Should it be a websocket even though my game is not a browser game?

EDIT: I will be at home in 4 hours, I will try to use it. But I have great interest in making an udp socket work.

No, it shouldn’t be a web socket necessarily, it sounded to me like you were instantiating a web socket.

I have a project where I fired up a socket client and connected it to a socket server, I’ll see if I can dig that out and give you the relevant bits.

I did some testing a few days ago and came to notice a few things about the TLAPI:

  • When you are using Send(), it will print a warning if the connection is invalid.
  • I forgot that the channels and other configurations need to be the same on the client and the server. Otherwise the connection will drop on the server’s end but still be alive on the client. As such, using this connection will produce a NetworkError other than “Ok”.
  • According to this manual, “Server can support only one Websocket Host and in the same time, it can handle generic hosts too.” From the looks of the example, that means 1 WebSocket only at a time, but multiple generic ones on the side is supported.

Hopefully this gives some insight on functionality and eliminates any problems you may be having. :slight_smile:

Hi !
Do you have any update on connecting to the python server using the LLAPI ? Is it possible? I managed only to get the same result in random bits and disconnect response and I am thinking that I should write the client in Unity in the old manner dealing with the socket logic myself.

Thanks

Guys is it the same implementation for Unix sockets ?