Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by philthegiant · Jan 13, 2015 at 02:47 AM · androidnetworkingtcpsending

TCP data sending with Android

Hi, I am fairly new to Unity, but I have created an app which upon the click of a button sends a simple TCP message to a very simple TCP server console application. The code works perfectly when i run it on my PC, but when i build it for Android and test it through my phone the app is not able to connect. Am i doing it wrong? Is simple TCP interaction done differently with android? Here's my Unity code:

     public Texture btnTexture;
     public GUIStyle style;
     TcpClient mySocket = new TcpClient();
     public NetworkStream theStream;
     public String Host = "192.168.2.25";
     public string banner = "";
     public Int32 Port = 8888;
     public bool sent = false;
     StreamWriter theWriter;
     StreamReader theReader;
 
     void OnGUI()
     {
         if (GUI.Button (new Rect ((Screen.width / 2) - Screen.width/4, (Screen.height / 2) - Screen.height/4, 
                                   Screen.width/2, Screen.width/2), btnTexture)) 
         {
             try
             {
                 if(!sent)
                 {
                     mySocket = new TcpClient(Host, Port);
                     theStream = mySocket.GetStream();
                     theWriter = new StreamWriter(theStream);
                     theReader = new StreamReader(theStream);
                     sent = true;
                     banner = "connected";
                 }
                 theWriter.Write("TCP Message" + " $");
                 theWriter.Flush();
                 //Application.LoadLevel ("Page1");
             }
             catch(Exception e)
             {
                 banner = "not connected";
             }
         }
 
         GUI.Label (new Rect ((Screen.width / 2) - Screen.width/4, (Screen.height / 2) - Screen.height/4, 20, 20), banner, style);
 
     }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by supernat · Jan 13, 2015 at 02:51 AM

http://unity3d.com/unity/licenses

If you don't have Android Pro, you won't get .NET sockets.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image philthegiant · Jan 13, 2015 at 04:03 AM 0
Share

I bought the "Good ol'Sockets" from the asset store https://www.assetstore.unity3d.com/en/#!/content/13166 which allows me to use System.Net.Sockets in free mode, but it still doesn't work, have you used or heard of this ? @supernat

avatar image supernat · Jan 13, 2015 at 04:53 AM 0
Share

I wasn't aware of that one, but I figured there were assets on the store. You need to follow the guide they provide though, here: http://cdn.lostpolygon.com/unity-assets/good-ol-sockets/files/Good%20Ol%27%20Sockets%20-%20User%20$$anonymous$$anual.pdf Did you use the LostPolygon namespace, and it's just not in the code above?

avatar image philthegiant · Jan 13, 2015 at 04:56 AM 0
Share

I just figured it out! Was careless on my part, just had to go into "Player Settings" and change "Internet Access" to Required and "Api compatibility level" to .NET 2.0 and it works from my phone now @supernat

avatar image
0

Answer by Mabulhuda · Sep 18, 2015 at 08:17 PM

did you give the permission to use INTERNET in the manifest ?

 <uses-permission android:name="android.permission.INTERNET" />
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by magonicolas · Oct 22, 2017 at 09:45 PM

Hi, Im trying to connect to a socket from my client, but is not working, it never connect, someone know what might be happening?

Here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

 using System.Collections.Generic;
 using System.Text.RegularExpressions;
 using System.Runtime.Serialization.Formatters.Binary;
 using System;
 using System.IO;
 using Newtonsoft.Json;
 using System.Net;
 using System.Net.Sockets;
 using UnityEngine.Networking;
 
 
 
 public class SockMan : MonoBehaviour {
 
     public Text myText;
 
     private bool socketReady;
     private TcpClient socket;
     private NetworkStream stream;
     private StreamWriter writer;
     private StreamReader reader;
 
     void Start() {
         print("Start");
         ConnectToServer();
     }
 
     public void ConnectToServer() {
         print("Setup");
         myText.text = "Setup...";
         if(socketReady)
             return;
         String host = "192.168.0.15";
         Int32 port = 8080;n
 
         try {
             print("Enter Try");
             myText.text = "Enter Try...";
             socket = new TcpClient(host, port);
             stream = socket.GetStream();
             writer = new StreamWriter(stream);
             reader = new StreamReader(stream);
             socketReady = true;
             myText.text = "Connected!";
             Send("{ \"type\": \"video\", \"value\": \"2.mp4\"}");
         } catch {}
     }
 
     private void Update() {
         if(socketReady) {
             if(stream.DataAvailable) {
                 string data = reader.ReadLine();
                 if(data != null) 
                     OnIncomingData(data);
             }
         }
     }
 
     private void OnIncomingData(string data) {
         print("Server: " + data);
     }
 
     private void Send(string data) {
         myText.text = "Will send";
         if (!socketReady) {
             writer.WriteLine(data);
             writer.Flush();
             myText.text = "Sent";
         }
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Android Access network connections information (netstat) 0 Answers

Some questions about unity app's tcp connection state in Android sleep mode 0 Answers

Android build is not receiving UDP broadcasts 4 Answers

How to access a PHP script on XAMPP with the use of Android device? 0 Answers

Networking RPC Sending Material Bandwidth 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges