Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Receive network messages from a Java client

Discussion in 'Multiplayer' started by Dunmord, Oct 1, 2015.

  1. Dunmord

    Dunmord

    Joined:
    Aug 15, 2013
    Posts:
    40
    Hello, I'm trying to setup a server/client architecture with UNet and a Java client. What I want is UNet to listen to incomming connections, process them and send/receive messages.

    Then I have a java client that I'm using to test the connection. Very simple:

    Code (Java):
    1. try{
    2.             Socket s = new Socket("localhost", 8888);
    3.             PrintWriter p = new PrintWriter(s.getOutputStream(), true);
    4.             p.print("Hello UNet");
    5.         }
    6.         catch(Exception ex)
    7.         {
    8.             ex.printStackTrace();
    9.         }
    My UNet class looks like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class NetworkConsole : MonoBehaviour
    6. {
    7.     private int myReliableChannelId = 0;
    8.     private int myUnreliableChannelId = 0;
    9.     private int hostId = 0;
    10.     private int connectionId = 0;
    11.     public int port = 8888;
    12.  
    13.     void Start()
    14.     {
    15.         NetworkTransport.Init ();
    16.         ConnectionConfig config = new ConnectionConfig ();
    17.         config.AddChannel (QosType.Reliable);
    18.         config.AddChannel (QosType.Unreliable);
    19.         HostTopology topology = new HostTopology (config, 10);
    20.         hostId = NetworkTransport.AddHost (topology, port);
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         int recHostId;
    26.         int lConnectionId;
    27.         int channelId;
    28.         byte[] recBuffer = new byte[1024];
    29.         int bufferSize = 1024;
    30.         int dataSize;
    31.         byte error;
    32.         NetworkEventType recData = NetworkTransport.ReceiveFromHost(hostId, out lConnectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
    33.         switch (recData)
    34.         {
    35.         case NetworkEventType.Nothing:         //1
    36.             break;
    37.         case NetworkEventType.ConnectEvent:    //2
    38.             Debug.Log(recBuffer.ToString());
    39.             break;
    40.         case NetworkEventType.DataEvent:       //3
    41.             break;
    42.         case NetworkEventType.DisconnectEvent: //4
    43.             break;
    44.         }
    45.     }
    46. }
    When I run my Unity application and my Java client. I get a "Connection refused" from my java client.

    How can this be achieved? Thanks.