Search Unity

.NET sockets demo code

Discussion in 'Multiplayer' started by EducaSoft, Oct 29, 2010.

  1. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Hi,

    Does anyone have some democode which explains how to OPEN a .NET socket from within unity to a socket server (on for example port 2222) and then send 1 string of text to that server through the socket?

    + (and thats a lot more difficult to understand I think) some technique to get a sort of event handler which can process received strings from the server?


    That would greatly simplify it for a lot of people looking on these forums but not realy finding it.
    So its not using the raknet system, but simply with .NET sockets.

    Kind regards,

    Bart
     
  2. jobemakar

    jobemakar

    Joined:
    Jun 13, 2009
    Posts:
    74
    EducaSoft,

    The connection code should be fairly standard. But the code to send a string to the server really depends on the server you are writing. I assume you are trying to write your own backend here.

    I can give you some simple basic code that shows you how to do the above using the ElectroServer API and talking to ElectroServer. Or I can paste in a little code from the source of our API showing how we connect and send. The code might look strange out of context, but I can put it in here if interested.
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    If you use google you will find a few dozen++ articles and sample projects that show you how the TCPClient class works. Thats likely how you will be working in this case unless you want to really go the "raw way" :)
     
  4. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    I thought I had one written in the WIKI, although it was originally an example converted from MSDN, it was designed to work with Unity, I will have to double check, been years.
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    unsure if you put it on the wiki but was here on the boards for sure.
    though not years was it ... or it was someone else who provided one last year in the time around "darknet main interest phase"
     
  6. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Jobemakar,

    Could you please post your code?


    I tried the following code and it connects, but I'm not sure if it SENDS anything and it seems like it receives the data which I sent (which is certainly not what the server sends)


    import System;
    import System.IO;
    import System.Net;
    import System.Net.Sockets;
    import System.Text;




    var server : TcpListener;
    var client : TcpClient;
    var stream : NetworkStream;
    var reader: StreamReader;
    var writer: StreamWriter;
    var msg:String;

    function Update () {


    }




    function OnGUI(){
    if (GUI.Button(Rect(10,140,100,20), "Start Client")) {
    client = new TcpClient("178.21.113.227", 2222);
    print(("Host returned: "));

    }

    if (GUI.Button(Rect(10,40,100,20), "Send something")) {

    stream = client.GetStream();
    reader = new StreamReader(stream);
    writer = new StreamWriter(stream);
    writer.WriteLine("NICK:mynickname");
    writer.Flush();
    msg = reader.ReadLine();
    print (msg);
    }



    }




    I don't know at all how I can poll for data which is sent by server to client. Maybe my code above it totally wrong?

    Hope you can help a little.


    Kind regards and thanks in advance,

    Bart
     
  7. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    I made some changes to my code and it connects correctly, sends data correctly but it goes wrong while receiving data.

    When I poll for data (currently in a coroutine) then it works "if there is data available for the client to read".

    But , when there is no data available, then the reader.ReadLine() just hangs and hangs forever waiting for anything readable to arrive from the server to the client.

    The biggest problem is dat all my other scripts are also not running anymore at that moment and it looks like unity is totally hanging (ok, its waiting for input on the socket but its the main effect)

    I read somewhere that sockets are blocki,g but how could I change the code below to let my unity code check regularly if there is data to read at the socket but in meanwhile not block my other scripts ?

    Regards,

    Bart


    import System;
    import System.IO;
    import System.Net;
    import System.Net.Sockets;
    import System.Text;

    var server : TcpListener;
    var client : TcpClient;
    var stream : NetworkStream;
    var reader: StreamReader;
    var writer: StreamWriter;
    var msg:String;

    function OnGUI(){
    if (GUI.Button(Rect(10,40,200,20), "Start Client")) {
    client = new TcpClient("178.21.113.227", 2222);
    }

    if (GUI.Button(Rect(10,70,200,20), "Send something to server")) {
    stream = client.GetStream();
    writer = new StreamWriter(stream);
    writer.WriteLine("NICK:mynickname");
    writer.Flush();
    }

    if (GUI.Button(Rect(10,100,200,20), "Start Coroutine")) {
    StartCoroutine("readData");
    }
    }

    function readData(){
    stream = client.GetStream();
    reader = new StreamReader(stream);
    msg = reader.ReadLine();
    print (msg);
    readData();
    }
     
  8. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
  9. Cosmin

    Cosmin

    Joined:
    May 7, 2010
    Posts:
    10
  10. pat_sommer

    pat_sommer

    Joined:
    Jun 28, 2010
    Posts:
    586
    any updates on this? i could really use a solution to the read problem :p