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 Joe_shaib · Jun 04, 2015 at 03:38 AM · networkingmultiplayerclientsockettcp

TCP responses are not receiving properly on client side.

I am working on a turn based online multiplayer game. I am using TCP communication to communicate with server. I can successfully connect to the server and send messages as well as I am also receiving the response from the server. Server used to send responses one after another but on client side messages are not received completely. Some of the responses are skipped.

This is the script I am using for client side.

using UnityEngine;

using System.Collections;

using System.Net.Sockets;

using System.Net;

using System;

using System.Text;

using System.Threading;

public class TCPClient : SingeltonBase {

private bool connecting = false;

private static TcpClient tcpClient = new TcpClient ();

private IPAddress address;

private int port;

private int failedConnectionCount;

private int maxFailedConnections;

private bool running = false;

private bool connected = false;

public CommParseManager CommParser = new CommParseManager ();

 public void Connect ()
 {
     IPHostEntry ipHostInfo = Dns.Resolve("....host.....");
     address = ipHostInfo.AddressList[0];
     port = 1111;
     failedConnectionCount = 0;
     maxFailedConnections = 10;
     running = true;

     if (connecting) {
         return;
     }
     
     connecting = true;
     tcpClient.BeginConnect (address.ToString (), port, ConnectCallback, null);
 }

 private void ConnectCallback (IAsyncResult result)
 {
     if (!tcpClient.Connected) {
         connecting = false;
         if (failedConnectionCount > maxFailedConnections) {
             running = false;
             Debug.Log ("Tcp: Too many failed connection attempts, aborting");
             return;
         }
         failedConnectionCount++;
         Connect ();
         return;
     }
     
     tcpClient.EndConnect (result);

     tcpClient.NoDelay = false;

     Read ();
 }

 public void Read ()
 {
     NetworkStream networkStream = tcpClient.GetStream ();

     byte[] buffer = new byte[1024];

     networkStream.BeginRead (buffer, 0, buffer.Length, ReadCallback, buffer    );
 }

 private void ReadCallback (IAsyncResult result)
 {
     int read;
     NetworkStream networkStream;

     try {
         networkStream = tcpClient.GetStream ();
         read = networkStream.EndRead (result);
     } catch {
         Debug.Log ("Error reading stream");
         return;
     }
     
     if (read == 0) {
         Debug.Log ("Connection closed");
         return;
     }

     byte[] buffer = result.AsyncState as byte[];

     networkStream.BeginRead (buffer, 0, buffer.Length, ReadCallback, buffer);


     string responseBulk = Encoding.UTF8.GetString (buffer);
     SendStringToParser (responseBulk);
 }

 void SendStringToParser (string responseBulk)
 {
     CommParser.DataExtractor (responseBulk);
 }
 
 public void Write (string data)
 {
     byte[] byteData = Encoding.ASCII.GetBytes(data);

     if (!tcpClient.Connected) {
         Debug.Log ("Not Connected");
         connected = false;
         if (!connecting) {
             Connect ();
         }
         return;
     }
     
     NetworkStream networkStream = tcpClient.GetStream ();
     networkStream.BeginWrite (byteData, 0, byteData.Length, WriteCallback, null);
 }

 private void WriteCallback (IAsyncResult result)
 {
     NetworkStream networkStream = tcpClient.GetStream ();
     networkStream.EndWrite (result);

     Debug.Log ("Total Bytes Sent");

 }

 public bool VerifyConnection ()
 {
     if (tcpClient == null) 
     {
         return false;
     }
     else
     {
          return tcpClient.Connected;
     }
 }

}

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
0

Answer by tomglenn · Aug 28, 2015 at 12:09 AM

You're only reading from the stream with a buffer length of 1024. You should prepend your messages with a length delimiter and read until you have read the appropriate length.

See how I do it in my open source TCP Server/Client: https://github.com/tomeglenn/FireLite

Specifically the SendPacket and ReadPacket methods: https://github.com/tomeglenn/FireLite/blob/develop/FireLite.Core/Extensions/NetworkStreamExtensions.cs

Comment
Add comment · Show 1 · 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

TCP and Sockets in unity 0 Answers

Unity networking tutorial? 6 Answers

Do i need to have 2 seperate apps communicating for server/client relationship? 2 Answers

Networking: Client Side Prediction with Inertia 0 Answers

Multiplayer client-detect 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