Search Unity

Sending data between devices - Sockets? Other?

Discussion in 'Multiplayer' started by Philip-ACN, Jun 18, 2019.

  1. Philip-ACN

    Philip-ACN

    Joined:
    Oct 25, 2018
    Posts:
    21
    Hello, i'm trying to send webcam data between two devices using sockets. I have one script which will setup a socket and start sending a byte array of data, and another script which sets up another socket to listen for incoming packets and reconstruct the image data. (The webcam image data works fine BTW.)

    The problem I'm having is that when I try to connect to a socket at 127.0.0.1 (or another IP on the same network, either to send or listen) I get an error that "No connection could be made because the target machine actively refused it."

    This is within the unity editor on both a PC and a Mac. (I haven't tried whilst deployed to device yet.)

    Does anyone have any idea why I'm getting this issue?
    Should I be using sockets at all for this? (Is there a better way?)
    I'm using the Oculus SDK and it's already using the OVRNetworkTcpServer to listen to a port, would this affect my sockets?


    Here's my two network scripts. (Redacted, of course. ;) )

    Send script...
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Drawing;
    5. using System.Net;
    6. using System.Net.Sockets;
    7. using System.Threading;
    8. using UnityEngine;
    9.  
    10. public class SendWebcamData : MonoBehaviour
    11. {
    12.     public string ipAddress = "127.0.0.1";
    13.     public int portNumber;
    14.  
    15.     private Socket sendSocket = null;
    16.     private IPAddress IP;
    17.     private IPEndPoint IPE;
    18.  
    19.     private float currentTime = 2f;
    20.     private float tempTime = 0f;
    21.  
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    27.  
    28.         IP = IPAddress.Parse(ipAddress);
    29.         IPE = new IPEndPoint(IP, portNumber );
    30.  
    31.         AttemptSocketConnection();
    32.  
    33.         currentTime = Time.timeSinceLevelLoad;
    34.     }
    35.  
    36.  
    37.     private void AttemptSocketConnection()
    38.     {
    39.         try
    40.         {
    41.             sendSocket.Connect( IPE );
    42.             Debug.Log( "***** Transmission Connection established" );
    43.         }
    44.         catch( SocketException e )
    45.         {
    46.             Debug.LogError( "***** Transmission Connection failed\n" + e.ToString() );
    47.             return;
    48.         }
    49.     }
    50.  
    51.  
    52.     // Update is called once per frame
    53.     void Update()
    54.     {
    55.         // If the screen is hidden, don't update the material.
    56.         if (gameObject.activeSelf == false) return;
    57.  
    58.         tempTime = Time.timeSinceLevelLoad;
    59.  
    60.         if (tempTime >= (currentTime + 1.0f))
    61.         {
    62.             currentTime = tempTime;
    63.  
    64.             if (sendSocket.Connected)
    65.             {
    66.                 Debug.Log( "***** Sending Camera Data" );
    67.  
    68.                 //Socket clientSocket = socket.Accept();
    69.  
    70.                 sendTexture.SetPixels([WebCamtexture]);
    71.                 byte[] buffer = sendTexture.EncodeToPNG();
    72.  
    73.                 try
    74.                 {
    75.                     sendSocket.Send( buffer, buffer.Length, SocketFlags.None );
    76.                 }
    77.                 catch( SocketException e )
    78.                 {
    79.                     Debug.LogError( "***** Problem sending data\n" + e.ToString() );
    80.                     sendSocket.Close();
    81.                     return;
    82.                 }
    83.  
    84.             }
    85.             else
    86.             {
    87.                 AttemptSocketConnection();
    88.             }
    89.         }
    90.     }
    91.  
    92.  
    93.     private void OnDestroy()
    94.     {
    95.         if( sendSocket.Connected )
    96.         {
    97.             sendSocket.Close();
    98.         }
    99.     }
    100. }


    Receive script...
    Code (CSharp):
    1. using System.Net;
    2. using System.Net.Sockets;
    3. using UnityEngine;
    4.  
    5. public class ReceiveWebcamData : MonoBehaviour
    6. {
    7.     [Header("Attach this to the object you want to display webcam footage on.")]
    8.     public string ipAddress = "127.0.0.1";
    9.     public int portNumber;
    10.  
    11.     private float currentTime = 0f;
    12.  
    13.     private float tempTime = 0f;
    14.  
    15.     private Socket listenSocket = null;
    16.  
    17.  
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    23.  
    24.         IPAddress IP = IPAddress.Parse(ipAddress);
    25.         IPEndPoint IPE = new IPEndPoint( IP, portNumber );
    26.  
    27.         try
    28.         {
    29.             listenSocket.Connect( IPE );
    30.             Debug.Log( "***** Receive Connection established" );
    31.         }
    32.         catch( SocketException e )
    33.         {
    34.             Debug.LogError( "***** Receive Connection failed\n" + e.ToString() );
    35.             return;
    36.         }
    37.  
    38.         currentTime = Time.timeSinceLevelLoad;
    39.     }
    40.  
    41.  
    42.     // Update is called once per frame
    43.     void Update()
    44.     {
    45.         // If the screen is hidden, don't update the material.
    46.         if (gameObject.activeSelf == false) return;
    47.  
    48.         tempTime = Time.timeSinceLevelLoad;
    49.  
    50.         if (tempTime >= (currentTime + 0.1f ) )
    51.         {
    52.             currentTime = tempTime;
    53.  
    54.             if( listenSocket.Connected )
    55.             {
    56.                 Debug.Log("Getting Camera Data");
    57.  
    58.                 byte[] buffer = new byte[(int)imageSize.x * (int)imageSize.y ];
    59.  
    60.                 listenSocket.Receive(buffer, buffer.Length, SocketFlags.None);
    61.  
    62.                 receiveTexture.LoadImage(buffer);
    63.  
    64.                 displayObjectMaterial.mainTexture = receiveTexture;
    65.             }
    66.         }
    67.  
    68.     }
    69.  
    70.     private void OnDestroy()
    71.     {
    72.         if( listenSocket.Connected )
    73.         {
    74.             listenSocket.Shutdown( SocketShutdown.Both );
    75.             listenSocket.Close();
    76.         }
    77.     }
    78. }
    Thanks for listening on port 69. ;)
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I haven't done much TCP socket stuff and don't know how Oculus could affect this, but I don't think your receive script should be calling Connect. I thought you need to call Bind followed by Listen.

    Also, normally you'd run this kind of net code in a separate thread instead of Unity's main thread.