Is there any way to detect that I am no longer receiving data on a TCP connection in C#. For example, I have the following program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace SocketTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
host.Connect(ip);
}
catch (SocketException se)
{
MessageBox.Show(se.ToString(),"ERROR!",MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string strTest = "help\r\n";
host.Send(Encoding.ASCII.GetBytes(strTest));
byte[] data = new byte[1024];
int receivedDataLength = host.Receive(data);
string stringData = Encoding.ASCII.GetString(data,0,receivedDataLength);
MessageBox.Show(stringData);
host.Shutdown(SocketShutdown.Both);
host.Close();
}
}
}
However, there is more data than what stringData is showing. Is there any to keep adding to the stringData? (short of increasing the size of byte[] data because that works as well) I guess what I'm trying to ask is, is there a way to tell when I'm done receiving data? I've tried using while or do/while, but I keep ending up in infinite loops.
Thanx,
~Atrus