Search Unity

Socket not working in browser

Discussion in 'Multiplayer' started by angeloffog, Aug 12, 2009.

  1. angeloffog

    angeloffog

    Joined:
    Aug 12, 2009
    Posts:
    3
    Hello,
    ive created a basic tcp connection to a own writen server.
    Everything works when i just pres the play button in the unity editer.

    When i export it however, it wont work. it does load in the dll, it just wont connect :(

    Any clues?
    This is the code i call, i first call the connect than the print, just to see that it works :p

    Note i have the last unity version

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Text;
    5. using System.Net.Sockets;
    6.  
    7. namespace aof
    8. {
    9.    
    10.     public class Class1
    11.     {
    12.         const int READ_BUFFER_SIZE = 255;
    13.         private TcpClient client;
    14.         private byte[] readBuffer = new byte[READ_BUFFER_SIZE];
    15.  
    16.         private String stats;
    17.  
    18.         public String st()
    19.         {
    20.             return "v1.1.2: " + stats;
    21.         }
    22.  
    23.         public void fnConnectResult(string sNetIP, int iPORT_NUM)
    24.         {
    25.             stats = "call";
    26.             try
    27.             {
    28.                 stats = "trying to connect";
    29.                 client = new TcpClient(sNetIP, iPORT_NUM);
    30.                 stats = "Connected";
    31.                 client.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(DoRead), null);
    32.  
    33.                stats=  "Connectionsss";
    34.             }
    35.             catch
    36.             {
    37.                 stats = "unactive";
    38.             }
    39.         }
    40.  
    41.         private void DoRead(IAsyncResult ar)
    42.         {
    43.             int BytesRead;
    44.             try
    45.             {
    46.                 // Finish asynchronous read into readBuffer and return number of bytes read.
    47.                 BytesRead = client.GetStream().EndRead(ar);
    48.                 if (BytesRead < 1)
    49.                 {
    50.                     // if no bytess were read server has close.  
    51.                   //  res = "Disconnected";
    52.                     return;
    53.                 }
    54.                 // Convert the byte array the message was saved into, minus two for the
    55.                 // Chr(13) and Chr(10)
    56.               //  strMessage = Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 2);
    57.               ///  ProcessCommands(strMessage);
    58.                 // Start a new asynchronous read into readBuffer.
    59.                 client.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(DoRead), null);
    60.  
    61.             }
    62.             catch
    63.             {
    64.                 //res = "Disconnected";
    65.             }
    66.         }
    67.  
    68.     }
    69. }
    70.  
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    there are two things:

    1. Where do you call fnConnectResult?
    2. You can't use namespaces in unity, thats not allowed
     
  3. angeloffog

    angeloffog

    Joined:
    Aug 12, 2009
    Posts:
    3
    this is the dll, and the dll is working the st function works perfectlky, it just wont connect, so this file gets compiled into a dll xD
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    I see, in that case its possible that won't work at all in the webplayer.
    The webplayer security requirements are very strict and different things are not supported there at all.

    Also I fail to see the use of writting something in a dll that works out of the box if you just put the C# file into unity. Its not like you could use network related stuff in DLLs that you can't use in Unity, you are bound to the same Namespaces to use.


    Generally you would check out the log to see if it mentions something.
     
  5. angeloffog

    angeloffog

    Joined:
    Aug 12, 2009
    Posts:
    3
    nop it doesn't and the smarfox uses nearly the same method, and it does work :(