Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Help me received Data

Discussion in 'Multiplayer' started by hungkv, Apr 23, 2008.

  1. hungkv

    hungkv

    Joined:
    Apr 14, 2008
    Posts:
    12
    I'm a client :
    public class GuiTestCS : MonoBehaviour {
    Socket sock;
    void Start ()
    {
    IPAddress host = IPAddress.Parse("10.4.2.215");
    IPEndPoint hostip = new IPEndPoint(host, 3333);
    sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    try
    {
    sock.Connect(hostip);
    print("Connected successful");
    }
    catch (SocketException e)
    {
    print("Problem connecting to host");
    print(e.ToString());
    return;
    }
    string message = "HungKV\n";
    sock.Send(Encoding.ASCII.GetBytes(message));
    byte[] data = new byte[1024];
    int receivedDataLength = sock.Receive(data);
    string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
    }
    void Update () {
    }
    }

    I can connect to server, send message to server, but i can't receive message form server.
    (It hang when receive message)
    Someone help me
    thanks!
     
  2. Abe

    Abe

    Joined:
    Feb 14, 2008
    Posts:
    35
    I just test, and got the same situation.

    But sometime, I can receive messages from server when I change sent message.

    Anyone know about this?

    //===========
    //edit

    I see Server send char '0' after message need to send, so it makes infinite loop at socket.Receive(buffer) => hang Unity.
     
  3. Boriska

    Boriska

    Joined:
    Sep 30, 2008
    Posts:
    8
    In this case sockets in unity work in blocking mode. If i type socket.blocking=false i have an error
     
  4. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287