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 /
  • Help Room /
avatar image
0
Question by Ti_Mo · Sep 01, 2018 at 08:12 AM · networkingstreamtcpwrite data

TCP Stream is empty

Hey, i´m trying to setup a TCP communication between a server and a client. I get my client connected to the server, so far so good. Now i´m trying to send a message via stream.write to the server. The server doesnt recognize the message until im disconnecting the client. Then the "readingDone" method gets fired. But even then the message is empty. I tried to follow this example but i dont understand what they really did other than me. Except of mine is way less complex because i only need a message from the client to the server and not a chat option.

Below my server code:

 public class Server : MonoBehaviour
 {
     TcpClient client = new TcpClient();
     TcpListener listener;
     int port = 8010;
 
     byte[] bytes = new byte[500];
     String data = null;
 
     NetworkStream stream;
 
     public void Start(){
     
         listener = new TcpListener (IPAddress.Any, port);
         listener.Start ();
         listener.BeginAcceptTcpClient (OnServerConnect, null);
         //StartCoroutine (connecting ());
         
         }
 
 
     void OnServerConnect(IAsyncResult ar){
 
 
         client = listener.EndAcceptTcpClient (ar);
         Debug.Log ("Connected!!!!");
         listener.BeginAcceptSocket (OnServerConnect, null);
 
         stream = client.GetStream ();
         stream.BeginRead (bytes, 0, bytes.Length, readingDone, null);
     }
         
 
 
     void readingDone(IAsyncResult ar){
         int length = stream.EndRead (ar);
         data = System.Text.Encoding.UTF8.GetString (bytes, 0, length);
         stream.BeginRead (bytes, 0, bytes.Length, readingDone, null);
     
     }
 
     void Update(){
         if(data != null)
         Debug.Log (data);
     
     
     }
 
     protected void OnApplicationQuit(){
         listener.Stop ();
         client.Close ();
     
     
     }
         
 }

client:

 public class Client : MonoBehaviour
 {
 
     TcpClient client = new TcpClient();
     IPAddress serverIp = IPAddress.Parse("127.0.0.1");
     int port = 8010;
 
     //messagestuff
     String msg = "TestMessage";
     //byte[] data;
 
     NetworkStream stream;
 
     public void Start(){
         
         client.BeginConnect (serverIp, port, (ar) => client.EndConnect (ar), null);
 
         stream = client.GetStream ();
 
 
         Send ();
 
     }
 
     public void Send(){
 
 
 
         byte[] data = System.Text.Encoding.UTF8.GetBytes (msg);
         stream.Write(data, 0, data.Length);
     
     }

I tried to close the client connection after the writing is done, but it doesnt close. It seems like the writing process does block the rest of the method? Maybe someone can help me on this one, im stuck now for a few days and cant find a proper solution. Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by thelghome · Jul 02, 2019 at 04:37 PM

There are some good networking example about TCP/UDP. You might be interested.

FM Exhibition Tool Pack | Forum

You can stream game view / webcam / audio between multiple platforms in local network. All written in C# and easy to modify.

Supported Platform: iOS/Android/Mac/PC

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

189 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 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

TCP/IP Unity Android ERROR Helfe 1 Answer

I need simple scripts to send a message from one unity build to another build. 0 Answers

Unity TCP Listener not working when built 0 Answers

Unity is only printing first message from server 0 Answers

Can you use a GazePoint eyetracking camera inside a Unity Project? Has it been done before? 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