Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by LucidCode · Jul 11, 2017 at 11:10 AM · androidandroid buildsocketsockets

I have a problem in Android socket comm with PC

Hello, Unity guys (?) I am a student who wants to study UNITY for the experiment
Anyway, I'm developing UNITY module for HID (Human Interface Device) by using socket Comm
In detail, build server in UNITY system and make a client which gets data from the external device and transfer preprocessed data to UNITY server.
And all was well before my program run on Android and the problem comes up after that
In the PC environment, my program works very well by using localhost but when I try to connect to PC in Android app, they can not find each other both server and client. Be frankly, I can't explain this problem better than this, sorry I attach my source code
I'm wondering if I need to add source code for use in the Android environment or for connecting to the external socket.
Thank you for reading me

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Text;
 using System.Net;
 using System.Net.Sockets;
 using System.Threading;
 
 
 public class Player : MonoBehaviour {
 
     static Socket server;
     Socket accpeted;
     static byte[] buffer { get; set; }
     bool isStarted = false;
     string strdata;
     // Use this for initialization
     void Start () {
         Application.runInBackground = true;
         server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         server.Bind(new IPEndPoint(IPAddress.Any, 23000));
         Thread data_th = new Thread(receiveData);
         data_th.Start();
     }
     
     // Update is called once per frame
     void Update () {
         if (isStarted)
         {
             goSpeed(1, strdata);
         }
     }
     void receiveData()
     {
         while (true)
         {
             try
             {
                 if (isStarted)
                 {
                     int bytesRead = accpeted.Receive(buffer);
                     byte[] formatted = new byte[bytesRead];
 
                     for (int i = 0; i < bytesRead; i++)
                     {
                         formatted[i] = buffer[i];
                     }
                     strdata = Encoding.UTF8.GetString(formatted);
                     Debug.Log("Get " + strdata + "\r\n");
                     
                 }
                 else
                 {
                     server.Listen(100);
                     accpeted = server.Accept();
                     buffer = new byte[accpeted.SendBufferSize];
                     isStarted = true;
                 }
             }
             catch { }
             Thread.Sleep(250);
         }
     }
 
 
     void goSpeed(float velocity ,string direction) // FB : X , LR : Z
     {
         float speed = velocity * Time.deltaTime;
         if (direction == "GF") transform.Translate(Vector3.forward * speed);
         else if (direction == "GB") transform.Translate(Vector3.back * speed);
         else if (direction == "GL") transform.Translate(Vector3.left * speed);
         else if (direction == "GR") transform.Translate(Vector3.right * speed);
         else if (direction == "TR") transform.Rotate(Vector3.right);
         else if (direction == "TL") transform.Rotate(Vector3.left);
     }
 }

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

0 Replies

· Add your reply
  • Sort: 

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

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to define which Android API level should I target for Minimum and Target? 1 Answer

Android build not working properly 0 Answers

How to port game on android wear ? 2 Answers

Unity2D quads not appearing in front of other quads in Android build 1 Answer

IL2CPP support for Linq 1 Answer


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