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
0
Question by unity_d9reV87zevq8-Q · Oct 23, 2018 at 04:30 PM · networkingserverwebservice

Android client to C# server communication, how to?,How to have an Android app communicate with web server

Hello everyone. I'm working on a small project (just a proof of concept really) where I have an Andorid app made in Unity, which should receive inputs (binary data) from a server running on my PC and possibily send back some data too. Nothing too heavy in both cases. I want good performance and possibly ease of use for me during the implementation. I'm not really an expert when it comes to networking, so I'm trying to figure out the best way to do this. I seems to have only two ways really.

1) Communicate over HTTP, use simple GET/POST requests. I already have a simple HTTP server that I implemented in C# running (haven't done anything Unity/client side). I know of UnityWebRequest, which I think should do everything I want pretty easily. My worry is performance, I know HTTP is sort of heavy, with an overhead that I have to account for 60+ times per second.

2) Use WebSockets. These look cool for perfomance and not too complicated, C# has them already implmentend in .NET. But Unity does not. I checked various plugins for this and they're either deprecated or not working. Unity only seems to implement them for WebGL projects (which is useless to me because nothing goes through a browser). Is there anyway I can use the Unity implementation outside of WebGL projects? Or maybe can I just use the standard C# .NET implementation with Unity (and how, if so)?

So, should I prefer WebSockets if there is a way to make them work in Unity? Or is HTTP fine for a simpler project?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Oct 24, 2018 at 02:06 AM

you can use the .net library in unity simply by putting this at the very top of your script:

 using System;
 using System.Net;
 using System.Net.Sockets;

I have been using TcpClient for my andriod games and it works great to communicate basic data to a TCP server.

if you want to use http, Unity also has its own functions that work well to to grab http requests. here is the most simple:

https://docs.unity3d.com/ScriptReference/WWW.html

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 Benjamin87 · Oct 24, 2018 at 02:58 AM

Tcp socket or http, it depends on what kind of app you want to build. To be more precisely, it depends on how often your C/S talk with each other.

For instance, your app is kind of a competitive game based on lockstep, which may be C/S exchange messages 20-50 times per second. It will make you suffer from massive channel creations / deletions in each message send over HTTP. In this situation, it will be a better choice to build over socket.

On the contrary, your app, for example, is a newspaper/magazine for users to read. The communication of C/S happens every few seconds. After fetching raw data from server, there is few C/S communication afterwards. Http is good enough here.

Http(WWW) client is a built-in module in Unity. If you want to use socket, you should use C# Socket class under namespace System.Net.Sockets.

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

137 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 avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Unity server connection time out 0 Answers

ontriggerenter will not work with !islocalplayyer 0 Answers

Dedicated server lobby problem 0 Answers

Unity UDP freezes if disconnected from server 2 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