I want to create a server so that HoloLens, UWP app, could connect to it and send data to it.
So to create the server, I created a Console Application in Visual Studio and followed the example here
And from the client side, UWP application, I created the below class:
using UnityEngine;
#if !UNITY_EDITOR && UNITY_METRO
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.Networking;
using Windows.Foundation;
#endif
public class SendSocketStreamClient {
// The port number for the remote device.
private int port;
private string serverIP;
public SendSocketStreamClient(int portNum, string ip){
port = portNum;
serverIP = ip;
}
#if !UNITY_EDITOR && UNITY_METRO
private StreamSocket networkConnection;
// The response from the remote device.
private static String response = String.Empty;
public void StartClient()
{
// Setup a connection to the server.
HostName networkHost = new HostName(serverIP);
networkConnection = new StreamSocket();
IAsyncAction outstandingAction = networkConnection.ConnectAsync(networkHost, port.ToString());
AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);
outstandingAction.Completed = aach;
}
public void NetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status)
{
// Status completed is successful.
if (status == AsyncStatus.Completed)
{
DataWriter networkDataWriter;
// Since we are connected, we can send the data we set aside when establishing the connection.
using (networkDataWriter = new DataWriter(networkConnection.OutputStream))
{
networkDataWriter.WriteBytes(Encoding.ASCII.GetBytes("Sending Trial 1"));
// Again, this is an async operation, so we'll set a callback.
DataWriterStoreOperation dswo = networkDataWriter.StoreAsync();
dswo.Completed = new AsyncOperationCompletedHandler<uint>(DataSentHandler);
}
}
else
{
// TODO resend
Debug.Log("Failed to establish connection. Error Code: " + asyncInfo.ErrorCode);
networkConnection.Dispose();
}
}
public void DataSentHandler(IAsyncOperation<uint> operation, AsyncStatus status)
{
if (status == AsyncStatus.Error)
{
// didn't send, so requeue
Debug.Log("Error while sending " + operation.ErrorCode);
}
// Always disconnect here since we will reconnect when sending the next data.
networkConnection.Dispose();
}
#endif
}
I then call this class in a C# script:
#if !UNITY_EDITOR && UNITY_METRO
SendSocketStreamClient newClient = new SendSocketStreamClient(ConnectionPort, ServerIP.Trim());
newClient.StartClient();
#endif
But I always get the error below.
Failed to establish connection. Error Code: System.Runtime.InteropServices.COMException (0x8007274D): No connection could be made because the target machine actively refused it.
Any idea what I am doing wrong or how to send data from HoloLens to the server? Is there something wrong with the server?
-----Edit----
Failed to establish connection. Error Code: System.Runtime.InteropServices.COMException (0x8007274C): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
The error changed to the above when I set the serverIP to be the IP address of my machine instead of 127.0.0.1. So I think giving it the right IP address solved this error but now it does not connect to the server.
Does that mean the way I created the server is not right?
As said in the comments above and as here I found that in the server I am using
IPAddress ipAddress = IPAddress.Loopback;
instead of
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
Changing that solved my problem and I was able to connect to the server from HoloLens and receive data.