Jump to content
  • Advertisement
Sign in to follow this  

Unity Data from C# does not seem to arrive.

This topic is 3079 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am completely new to C# programming but I have decent skills in C++/Java etc.
Recently I been trying to get my MMO project working with Unity3d.

On my server I print debug output in raw binary as soon as data arrives, when sending stuff from my C++ client I see the debug info displayed correctly, however data sent from Unity3d nothing does not seem to arrive.

I notice that the socket connection from Unity3d works, the server prints a message when a new connection is established. Since the connection is successful the problem is not the firewall (I tried to disable that as well to be sure).

I keep track of number sent bytes and error messages, everything seems OK.
For example after connecting and send pressing the "Account Login" button the integer totalSent is 17, which is correct:
2 byte header
1 byte msg id
5 byte string length + string (4 + "Name") a
9 byte string length + string (8 + "Password")

Here is my Unity3d that display soms buttons and handles the connection. I have been following the C# non blocking socket tutorial at http://blog.bee-eee.com/2008/09/15/c-non-blocking-sockets/.

using UnityEngine;
using System.Collections;

using System;
using System.Net;
using System.Net.Sockets;

public class NextGenConnection : MonoBehaviour {
public int totalUpdates;
public int totalReceived;
public int totalSent;
public bool connected;
Socket client;
private const int BUFFER_SIZE = 1024;
private const int HEADER_SIZE = 2;
private byte[] data = new byte[BUFFER_SIZE];
private byte[] header = new byte[HEADER_SIZE];

// Put these in an seperate header file
private const int ACCOUNT_LOGIN = 2;
private const int CHAR_LOGIN = 5;
private const int ACCOUNT_LOGIN_SUCCESS = 3;
private const int ACCOUNT_LOGIN_FAIL = 4;
private const int CHAR_LOGIN_SUCCESS = 6;
private const int CHAR_LOGIN_FAIL = 7;

public int recv;
public SocketError err;

// Putting GUI stuff here is not pretty, fix later.
Rect windowRect = new Rect(20, 20, 120, 120);

void Start () {
connected = false;

void Connect() {
client = new Socket( AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );
try {
Debug.Log("Connecting to server.next-gen.cc");
client.Connect("server.next-gen.cc", 2000);
} catch (SocketException) {
Debug.Log("Connection Failure");
Debug.Log("Connections success");
// This must be set after Connect().
client.Blocking = false;
connected = true;
totalReceived = 0;

// Update is called once per frame
void Update () {
int test = Read();
if(test < 0)
Debug.Log("Got server data");
totalUpdates += 1;

int Read() {
return 0;

int bytesRead=0;
SocketError err;
bytesRead = client.Receive(header, 0, HEADER_SIZE,
SocketFlags.None, out err);
totalReceived += bytesRead;

if(err != SocketError.WouldBlock)
return 0;
short dataSize = (short)(header[0] | (header[1] >> 8));
totalReceived += client.Receive(data, 0, (int) dataSize,
SocketFlags.None, out err);

return bytesRead;

void Send(int msgId, String arg1, string arg2) {
SocketError err = SocketError.WouldBlock;
byte[] Id = new byte[1];
byte[] header = new byte[2];

header = System.BitConverter.GetBytes((short) 17);
Id = System.BitConverter.GetBytes((char) msgId);

while (err == SocketError.WouldBlock) {
totalSent += client.Send(header, 0, 2, SocketFlags.None, out err);
Debug.Log("Sent header!");

totalSent += client.Send(Id, 0, 1, SocketFlags.None, out err);
Debug.Log("Sent message Id!");

byte[] test1 = makeString(arg1);
totalSent += client.Send(test1, 0, arg1.Length + 1, SocketFlags.None, out err);
Debug.Log("Sent message arg1" + arg1);

byte[] test2 = makeString(arg2);
totalSent += client.Send(test2, 0, arg2.Length + 1, SocketFlags.None, out err);
Debug.Log("Sent message arg2" + arg2);

if (err != SocketError.Success ) {
Debug.Log("Send error!");

if (err == SocketError.Success) {
Debug.Log("Sent was successful");

byte[] makeString(String str) {
int strLen = str.Length;
Debug.Log("str.Length = " + strLen.ToString());
byte[] strHeader = System.BitConverter.GetBytes((char) strLen);
byte[] payload = System.Text.Encoding.ASCII.GetBytes(str);
byte [] concat = new byte [strHeader.Length + payload.Length];
System.Buffer.BlockCopy(strHeader, 0, concat, 0, strHeader.Length);
System.Buffer.BlockCopy(payload, 0, concat, strHeader.Length, payload.Length);
return concat;

void OnGUI() {
windowRect = GUI.Window(0, windowRect, DoMyWindow, "My Window");

void DoMyWindow(int windowID)
if(GUI.Button(new Rect(10,20,100,20), "Connection"))
if(GUI.Button(new Rect(10,42,100,20), "Account Login"))
Send(ACCOUNT_LOGIN, "Name", "Password");
if(GUI.Button(new Rect(10,64,100,20), "Char Login"))
Send(CHAR_LOGIN, "", "");

[Edited by - flodihn on July 11, 2010 4:37:38 PM]

Share this post

Link to post
Share on other sites
While brushing my teeths I figured out I do multiple socket.Send(), that is probably the fault.

Share this post

Link to post
Share on other sites
I noticed my arrays have the same size, for example my byte header should be size 1 not 2.

One of my problems is BlockCopy one byte, C# does not allow just a byte to be sent, it wants a byte[], any ideas?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!