- Home /
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;
}
}
}
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613200604im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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