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.

question about Network.InitializeServer

Discussion in 'Multiplayer' started by rmbunity, Dec 4, 2007.

  1. rmbunity

    rmbunity

    Joined:
    Dec 1, 2007
    Posts:
    32
    My script is below. It is attached to the camera which is the only object.
    When play starts, the console shows the output from the 2 prints. But, no
    sever seems to be listening on port 2500. I have briefly explored the NAT
    stuff but don't think I need it here. I would be happy to see an open socket
    on port 2500 via localhost/127.0.0.1.
    Thanks for any insights.
    --ralph

    function Start() {
    print("HELLO\n");
    // Network.useNat = !Network.HavePublicAddress();
    var rc = Network.InitializeServer(5,2500); // start my own server
    print("RC=" + rc);
    }
     
  2. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    274
    If you run that script, a server should start listening on port 2500. Just as long as you get RC=NoError or something similar printed to the debug log.

    To "see" the port has opened, open a terminal window and run
    Code (csharp):
    1. netstat -n | grep 2500
    You should see a line printed indicating that someone is listening on that port. It does not bind to localhost specifically but to any interface (*). If you don't see the port has opened you presumably have an error printed after "RC="?
     
  3. rmbunity

    rmbunity

    Joined:
    Dec 1, 2007
    Posts:
    32
    Thanks for the response. This is very helpful. The RC is indeed NoError.
    And, a UDP port 2500 port is there. I had been looking for a TCP port.

    If you don't mind continuing the thread for a moment, in the same script, I next
    added this line after printing the RC from the InitializeServer:
    Network.Connect("192.168.2.100",2500); // now start client
    I experimented with various IPs including localhost, 127.0.0.1, etc but was not
    successful. Now I know that technically you don't really "connect" to a UDP port,
    but I am aware that you can execute a connect to one to tie down the
    association. Anyway, I am not sure what Network.Connect is actually doing
    internally. I just know that the docs tell me:

    To create the server, you simply call Network.InitializeServer() from a script. When you want to connect to an existing server as a client, you call Network.Connect() instead.

    So, I am unsure how to accomplish the connect.

    Thanks very much.
    --ralph
     
  4. rmbunity

    rmbunity

    Joined:
    Dec 1, 2007
    Posts:
    32
    Never mind. :)
    I answered my own question. I found that I could start 2 separate instances
    of Unity, one running the InitializeServer and the other running the Connect.
    Both work fine now.

    Thanks.
    --ralph