Search Unity

.net tcp networking help

Discussion in 'Multiplayer' started by TJB, Dec 23, 2008.

  1. TJB

    TJB

    Joined:
    Mar 20, 2007
    Posts:
    146
    I'm currently attempting to figure out how to send data between a tcpclient and a tcplistener... but I can't figure it out. This is the code i've written in an attempt to figure it out:

    Code (csharp):
    1. import System;
    2. import System.IO;
    3. import System.Net;
    4. import System.Net.Sockets;
    5. import System.Text;
    6.  
    7. var disptext = "";
    8. var server;
    9. var serverStarted = false;
    10. var client;
    11. var message = "hello";
    12.  
    13. function OnGUI () {
    14.     if (GUI.Button(Rect(10,10,100,20), "Start Server")) {
    15.         server = new TcpListener(IPAddress.Parse("127.0.0.1"), 13000);
    16.         server.Start();
    17.         serverStarted = true;
    18.     }
    19.     if (GUI.Button(Rect(10,40,100,20), "Start Client")) {
    20.         client = new TcpClient("127.0.0.1", 13000);
    21.     }
    22.     GUI.Box(Rect(10,70,100,20), disptext);
    23.     if (GUI.Button(Rect(10,100,100,20), "Send Message")) {
    24.         var data = System.Text.Encoding.ASCII.GetBytes(message);
    25.         stream = client.GetStream();
    26.         stream.Write(data, 0, data.Length);
    27.     }
    28. }
    29.  
    30. var stream;
    31. var bytes = new Byte[256];
    32. function Update () {
    33.     if (serverStarted) {
    34.         if (stream.Read(bytes, 0, bytes.Length) != 0) {
    35.             client = server.AcceptTcpClient();
    36.             stream = client.GetStream();
    37.         }
    38.         else {
    39.             var i = stream.Read(bytes, 0, bytes.Length);
    40.             var data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
    41.             disptext = data;
    42.         }
    43.     }
    44. }
    basically I'm trying to make it so i start a server on one machine, then the client on the other, then send some data from the client. the text sent is displayed in the gui box on the server. I think my code is close to being correct, but i really don't know.

    if anyone can fix the above code so that it works that'd really help me. I really just need something working to look over and figure out how it works.
     
  2. TJB

    TJB

    Joined:
    Mar 20, 2007
    Posts:
    146
    some help on this would be greatly appreciated.
     
  3. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Sorry but I'm not immediately able to help but I'll move this to the appropriate forum area for greater visibility in front of those that might be able to assist.


    Moving to Multiplayer Networking in 3... 2... 1...
     
  4. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Hey there,

    I hope I'm not too late and you havn't abandonned your effort of getting into Networking.

    I tried to fix your Unity-JavaScript code. I ran into strange behaviour, where calling NetworkStream.Read would throw a MissingMethodException. I posted this as a bug.
    I then translated the code to C# where it worked. Both version of the code can be found in the example project here.

    I won't get too specific about what I changed, I'll just give you a few points to remember:
    1. AcceptTcpClient is a blocking method, which means that the program halts in that line, until a client connects. I put the call to this method in a coroutine that checks the property Pending() to see if someone wants to connect. When there is someone I call the AcceptTcpClient method.

    2. I don't quite understand what you tried to do in the Update function. One thing to remember though: Once you read a stream, the data you took out is gone. So reading a stream twice without using the data you obtained from the first Read (as you are doing), is a bad idea.

    Cheers
     
    Last edited: Oct 1, 2010
  5. TJB

    TJB

    Joined:
    Mar 20, 2007
    Posts:
    146
    thanks for the help! Some of the examples in microsofts .net documentation are... impossible to understand.

    Also, I solved the problem with the javascript code. Just had to explicitly state the types of a few variables. Changed these lines:
    Code (csharp):
    1. var server;
    2. var client;
    3. var stream;
    To this:
    Code (csharp):
    1. var server : TcpListener;
    2. var client : TcpClient;
    3. var stream : NetworkStream;
    This has been a huge help, I had pretty much given up on getting this to work, thanks.
     
  6. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Hey, thanks for clearing that up. This just goes to show how much of a JavaScript newb I am. I program all my stuff in C# to avoid these kind of problems ;)
     
  7. Lugh

    Lugh

    Joined:
    Sep 28, 2010
    Posts:
    1
    The link is dead...
     
  8. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Sorry about that. Must have deleted it from my rapidshare account by mistake.

    Here is the working link.

    I updated the link above as well.