Is this a c# error or unity bug - networking
so i built these two as sperate c# projects and it worked fine, the problem is as soon as i tried to use the code in unity i get this error: "No connection could be made cause the target machine actively refused it"
https://www.youtube.com/watch?v=PCwS7F2uK3Q
https://www.youtube.com/watch?v=bbT4IzCoQjs
here is the main class (Connect_Server.cs) (it contains quite a lot of different code to the one in the video due to it not being a form)
using System.Net.Sockets;
using System.Net;
using UnityEngine;
using System.Threading;
using System.Text;
using System;
using System.IO;
public class Connect_Server : MonoBehaviour {
Thread login;
Client client;
string ip = "127.0.0.1";
int port = 9825;
void Start () {
client = new Client();
client.OnConnect += new Client.OnConnectEventHandler(Client_OnConnect);
client.OnSend += new Client.OnSendEventHandler(Client_OnSend);
client.OnDisconnect += new Client.OnDisconnectEventHandler(Client_OnDisconnect);
Debug.Log("well we got this far");
if (!client.Connected)
client.Connect(ip, port);
}
private void Client_OnDisconnect(Client sender)
{
Debug.Log("DC'd");
}
private void Client_OnSend(Client sender, int sent)
{
Debug.Log(string.Format("Data Sent: {0}", sent));
}
private void Client_OnConnect(Client sender, bool connected)
{
Debug.Log("woooo");
if (connected)
Debug.Log("we're in");
}
void SendText(string text)
{
BinaryWriter bw = new BinaryWriter(new MemoryStream());
bw.Write((int)Commands.String);
bw.Write(text);
bw.Close();
byte[] data = ((MemoryStream)bw.BaseStream).ToArray();
bw.BaseStream.Dispose();
client.Send(data, 0, data.Length);
data = null;
}
void SendImage(string path)
{
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
byte[] b = File.ReadAllBytes(path);
bw.Write((int)Commands.Image);
bw.Write(b.Length);
bw.Write(b);
bw.Close();
b = ms.ToArray();
ms.Dispose();
client.Send(b, 0, b.Length);
}
void OnApplicationQuit()
{
}
void Update () {
if (client.Connected)
{
if (Input.GetKeyDown(KeyCode.Return))
{
SendText("Hi all");
}
}
}
Socket socket()
{
return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
}
then the other class (client.cs)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System;
enum Commands
{
String = 0,
Image
}
public class Client {
public delegate void OnConnectEventHandler(Client sender, bool connected);
public event OnConnectEventHandler OnConnect;
public delegate void OnSendEventHandler(Client sender, int sent);
public event OnSendEventHandler OnSend;
public delegate void OnDisconnectEventHandler(Client sender);
public event OnDisconnectEventHandler OnDisconnect;
Socket skt;
public bool Connected
{
get
{
if (skt != null)
return skt.Connected;
return false;
}
}
public Client()
{
skt = socket();
}
public void Connect(string ip, int port)
{
if (skt == null)
skt = socket();
skt.BeginConnect(ip, port, connectCallback, null);
}
void connectCallback(IAsyncResult ar)
{
try
{
Debug.Log("noot noot");
skt.EndConnect(ar);
if (skt.Connected)
OnConnect(this, Connected);
}
catch (Exception ex)
{
Debug.Log(ex.Message);
}
}
public void Send(byte[] data, int index, int length)
{
skt.BeginSend(BitConverter.GetBytes(length), 0, 4, SocketFlags.None, sendCallback, null);
skt.BeginSend(data, index, length, SocketFlags.None, sendCallback, null);
}
void sendCallback(IAsyncResult ar)
{
try
{
int sent = skt.EndSend(ar);
if (OnSend != null)
OnSend(this, sent);
}
catch (Exception ex)
{
Debug.Log(ex.Message);
}
}
public void Disconnect()
{
try
{
if (skt.Connected)
{
skt.Close();
skt = null;
if (OnDisconnect != null)
OnDisconnect(this);
}
}
catch (Exception ex)
{
Debug.Log(ex.Message);
}
}
Socket socket()
{
return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
}
Your answer
Follow this Question
Related Questions
UNet: Giving authority to all clients for scene objects 0 Answers
Having multiple UNET errors that I believe are caused by unity itself. Am I doing something wrong? 1 Answer
Download Multiple Images from Server and show in unity Images 0 Answers
Network Transport Layer API does not work with iOS to PC???? 0 Answers
How to optimize this scripts ? 2 Answers