Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

help .NET socket for andoid

Discussion in 'Multiplayer' started by luubi2112, Oct 23, 2012.

  1. luubi2112

    luubi2112

    Joined:
    Jul 7, 2012
    Posts:
    6
    hi all,
    Does anyone have document about connect from client to server ?
    is it the same on window?
     
  2. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    It should be the same as the mono library Unity uses is 'copying' microsofts library. Try it and see.
     
  3. luubi2112

    luubi2112

    Joined:
    Jul 7, 2012
    Posts:
    6
    thanks @KHopcraft
     
  4. luubi2112

    luubi2112

    Joined:
    Jul 7, 2012
    Posts:
    6
    I created a client program on android, windows server.
    It works fine on windows. but on android, the TCP client cannot connect to the server.
    help me ! :(

    Code (csharp):
    1.  
    2. private TcpClient myClient = new TcpClient();
    3. public TcpClient MyClient
    4.         {
    5.             get { return myClient; }
    6.         }
    7. public void SendConnection()
    8.     {  
    9.         try
    10.         {  
    11.             if (!isConnected)
    12.             {        
    13.                 myClient.Connect(new IPEndPoint(IPAddress.Parse(ipAdress), sPort));
    14.                 isConnected = true;        
    15.                 if (myClient.Client.Connected)
    16.                 {
    17.                     recieve = new Thread(new ThreadStart(ReceiveData));
    18.                     recieve.Start();
    19.                 }
    20.             }
    21.         }
    22.         catch(Exception ex)
    23.         {
    24.             Debug.Log("Can not connect to  Server   ,Exception ::"+ex.Message);
    25.             throw new Exception(ex.Message);
    26.         }
    27.     }
    28.  
     
  5. CJR-Gaming

    CJR-Gaming

    Joined:
    Oct 12, 2012
    Posts:
    63
    I was under the impression that in order to use networking on Android you need the Android Pro version. I may be mistaken, however.
     
  6. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Quite right CJR, For Android And IOS to use .net sockets, you need pro version of Android and IOS respectively.
     
  7. ThiagoRibeiro

    ThiagoRibeiro

    Joined:
    Mar 11, 2013
    Posts:
    3
    I've got a trial Pro (all devices Pro) but I'm still getting no connections, on windows it works fine, android doesn't :( what would I be doing wrong?
    I have also policy server and so on...

    Thanks in advance!
     
  8. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    Maybe your IP / Server Address is wrong. Even if Android gets simulated on your PC, it's a separate device with it's own IP address. This means: 127.0.0.1 and localhost both target the Android device in emulator.
     
  9. ThiagoRibeiro

    ThiagoRibeiro

    Joined:
    Mar 11, 2013
    Posts:
    3
    Hello Tobiass, I'm using domain, the server is up and running, also the cross-domain policy server. It's sad, it's the only thing missing for us... thanks for the tip.
     
  10. ThiagoRibeiro

    ThiagoRibeiro

    Joined:
    Mar 11, 2013
    Posts:
    3
    (Solved) I decided to take out the policy verification on the code, and guess what, just worked, I feel a bit confused because sometimes and just does not work without it, and at this time it only works without it :( anyway, its working now... thanks for the help...
     
  11. magonicolas

    magonicolas

    Joined:
    Dec 4, 2013
    Posts:
    30
    Help me with this guys, I using an android to android TCP connection, the Server is ok, it has been tested with a native android App, but my Unity Android App is not connecting!
    Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. using System.Collections.Generic;
    7. using System.Text.RegularExpressions;
    8. using System.Runtime.Serialization.Formatters.Binary;
    9. using System;
    10. using System.IO;
    11. using Newtonsoft.Json;
    12. using System.Net;
    13. using System.Net.Sockets;
    14. using UnityEngine.Networking;
    15.  
    16.  
    17.  
    18. public class SockMan : MonoBehaviour {
    19.  
    20.     public Text myText;
    21.  
    22.     private bool socketReady;
    23.     private TcpClient socket;
    24.     private NetworkStream stream;
    25.     private StreamWriter writer;
    26.     private StreamReader reader;
    27.  
    28.     void Start() {
    29.         print("Start");
    30.         ConnectToServer();
    31.     }
    32.  
    33.     public void ConnectToServer() {
    34.         print("Setup");
    35.         myText.text = "Setup...";
    36.         if(socketReady)
    37.             return;
    38.         String host = "192.168.0.15";
    39.         Int32 port = 8080;n
    40.  
    41.         try {
    42.             print("Enter Try");
    43.             myText.text = "Enter Try...";
    44.             socket = new TcpClient(host, port);
    45.             stream = socket.GetStream();
    46.             writer = new StreamWriter(stream);
    47.             reader = new StreamReader(stream);
    48.             socketReady = true;
    49.             myText.text = "Connected!";
    50.             Send("{ \"type\": \"video\", \"value\": \"2.mp4\"}");
    51.         } catch {}
    52.     }
    53.  
    54.     private void Update() {
    55.         if(socketReady) {
    56.             if(stream.DataAvailable) {
    57.                 string data = reader.ReadLine();
    58.                 if(data != null)
    59.                     OnIncomingData(data);
    60.             }
    61.         }
    62.     }
    63.  
    64.     private void OnIncomingData(string data) {
    65.         print("Server: " + data);
    66.     }
    67.  
    68.     private void Send(string data) {
    69.         myText.text = "Will send";
    70.         if (!socketReady) {
    71.             writer.WriteLine(data);
    72.             writer.Flush();
    73.             myText.text = "Sent";
    74.         }
    75.     }
    76. }
    77.