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 Sytto · Apr 17, 2012 at 08:32 PM · networkingserversockettcp

Why i can't receive 2 messages in a row from my server in Unity?

hi all, and sorry if the question looks a bit long, i'm having troubles with TCP sockets, i'm really new to networking and i'm doing some tests, i have a server that i've programmed in Java, Unity connects to it through tcp sockets, so when it connects i send some test message from the server and Unity receives it correctly, but when i send 2 messages in a row, Unity just reads the first one and seems to skip the second one, i don't know if this behaviour is normal because it's my very first time with sockets, the only way i found to fix this was to send an "ok" from unity to the server to make the server know that it can send the next message, but that way seems a bit slow for a multiplayer game, so my question is if it's possible to read all the data incoming to the client (Unity) without having to send to the server that the client has received the packet and it can send the following one.

i'm using this class i found here around the community

 using UnityEngine;
 using System.Collections;
 using System;
 using http://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

and i have this loop somewhere in my code

 while(true) {
      var serverResponse : String = TCP.readSocket();
      if(serverResponse != "") {
          Debug.Log("Server: "+serverResponse); // always skips second message
      }
      yield;
 }
Comment
Add comment · Show 1
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 GizmoMKD · Dec 23, 2012 at 10:22 PM 0
Share

Also i have this problem... when server send two messages in row i get only first one, to get the second one client have to write something back to server to receive second message from server...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by areca_chen · Jun 15, 2012 at 05:05 AM

I have the same problem. I insert 'Thread.Sleep(100);' into the two row, and it seems resolve this problem, but I do not know why.

Comment
Add comment · Show 2 · 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 GizmoMKD · Dec 23, 2012 at 10:22 PM 0
Share

Interesting... why sleep help in this situation.

avatar image JoelKatz · Dec 27, 2012 at 06:41 AM 0
Share

The sleep "helped" because it increased the chance that calling ReadLine would only read precisely that line from the underlying connection, increasing the chances that DataAvailable would later be true.

avatar image
0
Wiki

Answer by JoelKatz · Dec 27, 2012 at 12:04 AM

Your DataAvilable check is broken. Here's what happens:

  1. All data is sent.

  2. Data is available, you call ReadLine. It reads all the data and returns one line to you.

  3. No data is available now, since it has already been read from the connection.

It also serves no purpose. Just because some data is available, that doesn't mean a whole line is available. So what purpose does it serve? Just get rid of it.

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

7 People are following this question.

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

Related Questions

Unity networking tutorial? 6 Answers

Help with Java socket server 1 Answer

TCP and Sockets in unity 0 Answers

Unity sockets 1 Answer

What socket API will work correctly with Unity? 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