Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
5
Question by Manu · Apr 18, 2010 at 08:29 PM · sockethardwaretcpthird-party

Unity project and 3rd Party apps

Hi

I recently discovered Unity and it seems pretty cool. I was wondering if there was any chance to add functionalities to a unity project through a 3rd party application. For instance, should I have an hardware that produces data, or should I want to deal with an advanced database system within my game, is there an option to develop an asset (or I don t know what) in order to have my unity application interacting with the program or server I would have created myself (if yes, are there any computer language limitations? can I use Java for my own application for instance?)

Thanks

Manu

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
9

Answer by Cyclops · Apr 19, 2010 at 01:16 PM

.NET Sockets are mostly just TCP/IP Sockets, nothing unusual. .NET also has a set of classes such as TCPClient and TCPListener (for building a server), that contain a Socket, and supply additional functionality - and are easier to use than straight Sockets.

I have started playing with networking, and written a TCP script, which I attached to a GUI script. The GUI script has a Text Box and a Send Button, which sends the contents of the Text box via the WriteSocket() function I wrote. In the GUI script, there is also a function that reads (once a second) the ReadSocket() function for incoming data. If it gets data, it then displays it in a second Text box.

The server part of my test software isn't .NET, I wrote a Python/Twisted-based server. Doesn't do much, basically echoes lines back. But, this is a working example of creating a TCP client in Unity

A list of the C# TCP code I wrote, in a file called 's_TCP.cs', is:

 using UnityEngine;
 using System.Collections;
 using System;
 using System.IO;
 using System.Net.Sockets;

 public class s_TCP : MonoBehaviour {
 internal Boolean socketReady = false;

 TcpClient mySocket;
 NetworkStream theStream;
 StreamWriter theWriter;
 StreamReader theReader;
 String Host = "localhost";
 Int32 Port = 5111;

 void Start () {
 }
 void Update () {
 }
 // **********************************************
 public void setupSocket() {
     try {
         mySocket = new TcpClient(Host, Port);
         theStream = mySocket.GetStream();
         theWriter = new StreamWriter(theStream);
         theReader = new StreamReader(theStream);
         socketReady = true;
     }
     catch (Exception e) {
         Debug.Log("Socket error: " + e);
     }
 }
 public void writeSocket(string theLine) {
     if (!socketReady)
         return;
     String foo = theLine + "\r\n";
     theWriter.Write(foo);
     theWriter.Flush();
 }
 public String readSocket() {
     if (!socketReady)
         return "";
     if (theStream.DataAvailable)
         return theReader.ReadLine();
     return "";
 }
 public void closeSocket() {
     if (!socketReady)
         return;
     theWriter.Close();
     theReader.Close();
     mySocket.Close();
     socketReady = false;
 }
 } // end class s_TCP

Hope that helps.

Comment
Add comment · Show 6 · 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 DaveA · Apr 01, 2011 at 07:14 AM 0
Share

This is cool. Other posts I've seen say that this type of stuff won't really work unless you do a bunch of thread stuff. Does you code 'just work' or should it really involve threading?

avatar image Cyclops · Apr 01, 2011 at 01:14 PM 0
Share

@DaveA, yes, that code works as-is, no threading required or anything else (just attach it to an Object). Sockets are a built-in part of .Net/$$anonymous$$ono, which is included with Unity.

avatar image FingerFighter · Jul 03, 2012 at 03:06 PM 1
Share

It's great. Thanks. But I'm having the compilation error on the line of using http://System.IO; in my C# source. It seems to me "//" was interpreted by the unity C# compiler as the beginning of comment. Can you please shed some light on that?

avatar image sona.viswam · Jul 18, 2013 at 11:05 AM 0
Share

Its not working for me. Connected but not receiving data. Please help

avatar image Saward · Nov 16, 2013 at 02:44 PM 0
Share

The following code does not work correctly:

 public String readSocket() {
   if (!socketReady)
     return "";
   if (theStream.DataAvailable)
     return theReader.ReadLine();
   return "";
 }

This caused me a headache for quite few hours. I think that checking DataAvailable on the stream is not a reliable way to check if there is data to be read on the streamreader. So you do not want to check for DataAvailable. However, if you just remove that, then the code will block on ReadLine when there is no more to read. So ins$$anonymous$$d, you need to set a timeout for reading from the stream, so that you won't wait longer than (say) a millisecond:

 theStream.ReadTimeout = 1;

And then, you can use something like:

 public String readSocket() {
     if (!socketReady)
         return "";
     try {
         return theReader.ReadLine();
     } catch (Exception e) {
         return "";
     }
 }

This code isn't perfect, I still need to improve it (e.g., check what kind of exception was raised, and deal with it appropriately). And maybe there's a better way overall to do this (I experimented with using Peek(), but the -1 it returns I suspect is for when the socket closes, and not just when there is no more data to read for now). However, this should solve problems with the posted code, like those I was having. If you're finding data is missing from the server, then it's probably sitting in your reader stream, and won't be read until new data is sent from the server and stored in the stream such that theStream.DataAvailable returns true.

Show more comments
avatar image
5

Answer by duck · Apr 18, 2010 at 09:15 PM

You can use standard .net sockets to communicate with any other socket-based application. You also have access to most of the .net 2.0 API, and with unity pro, you can write c++ DLLs for your project.

So, there are many ways for your projects to interact with 3rd party applications. You can't use java directly with the Unity engine, although there's nothing stopping you from communicating with Java applications.

One common example of this is Unity clients communicating with Smartfox Server (a socket-based multiuser server application written in Java)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Global update function for network 1 Answer

Tcp server 0 Answers

Android Tcp Socket Problem 1 Answer

Right way to receive Images over tcp? 2 Answers

Socket received data being displayed in game with large delay, how can I synchronize it better?,Unity not synchronously receiving/displaying data from socket 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