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;
}
}
}