'Sockets in Unity
I'm working on a project that requires me to connect to an outside server from my oculus app, I want to do this via pushing a button. I have pretty limited C# knowledge, and would like someone to check my code. I can click the button just fine but initiating the connection doesn't seem to work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine.UI;
namespace Client
{
}
public class ConnectionCSClient : MonoBehaviour
{
}
public class Click : MonoBehaviour
{
}
class Program
{
public Button ConnectButton;
void Start()
{
Button Button = ConnectButton.GetComponent<Button>();
ConnectButton.onClick.AddListener(TaskOnClick); //Adds a listner on the button
}
void TaskOnClick()
{
if (Input.GetMouseButtonUp(0))
{
Debug.Log("Pressed left click.");
}
if (ConnectButton.onClick = true) try static void ExecuteClient();
{
{
// Establish the remote endpoint
// for the socket. This example
// uses port 11111 on the local
// computer.
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 11111);
// Creation TCP/IP Socket using
// Socket Class Constructor
Socket sender = new Socket(ipAddr.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
try
{
// Connect Socket to the remote
// endpoint using method Connect()
sender.Connect(localEndPoint);
// We print EndPoint information
// that we are connected
Console.WriteLine("Socket connected to -> {0} ",
sender.RemoteEndPoint.ToString());
// Creation of message that
// we will send to Server
byte[] messageSent = Encoding.ASCII.GetBytes("Test Client<EOF>");
int byteSent = sender.Send(messageSent);
// Data buffer
byte[] messageReceived = new byte[1024];
// We receive the message using
// the method Receive(). This
// method returns number of bytes
// received, that we'll use to
// convert them to string
int byteRecv = sender.Receive(messageReceived);
Console.WriteLine("Message from Server -> {0}",
Encoding.ASCII.GetString(messageReceived,
0, byteRecv));
// Close Socket using
// the method Close()
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
// Manage of Socket's Exceptions
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
Solution 1:[1]
Why do you think you need to work at the level of TCP sockets for this? That would be like opening up and removing the entire dashboard of your car just because the radio isn't working. You don't need to do that. HTTP is built on top of TCP and so HTTP is what you would use for web requests.
Unity provides the WebRequest class specifically for communicating with web servers.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Engineer |