Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Communicating with program using raw socket

Discussion in 'Multiplayer' started by rend-master, Mar 7, 2016.

  1. rend-master

    rend-master

    Joined:
    Sep 22, 2015
    Posts:
    11
    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!
     
    Makosai likes this.
  2. Freakyuno

    Freakyuno

    Joined:
    Jul 22, 2013
    Posts:
    138
    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?
     
    rend-master likes this.
  3. rend-master

    rend-master

    Joined:
    Sep 22, 2015
    Posts:
    11
    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?)
     
  4. Freakyuno

    Freakyuno

    Joined:
    Jul 22, 2013
    Posts:
    138
    rend-master likes this.
  5. rend-master

    rend-master

    Joined:
    Sep 22, 2015
    Posts:
    11
    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.
     
  6. Freakyuno

    Freakyuno

    Joined:
    Jul 22, 2013
    Posts:
    138
    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.
     
  7. rend-master

    rend-master

    Joined:
    Sep 22, 2015
    Posts:
    11
  8. rend-master

    rend-master

    Joined:
    Sep 22, 2015
    Posts:
    11
    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.
     
    Last edited: Mar 7, 2016
  9. Freakyuno

    Freakyuno

    Joined:
    Jul 22, 2013
    Posts:
    138
    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.
     
  10. Makosai

    Makosai

    Joined:
    Mar 4, 2014
    Posts:
    46
    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. :)
     
  11. Danielpunct

    Danielpunct

    Joined:
    May 9, 2013
    Posts:
    16
    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
     
  12. Kevz93

    Kevz93

    Joined:
    Feb 11, 2015
    Posts:
    2
    Guys is it the same implementation for Unix sockets ?